Skip to content

WalkerGriggs/okapi

Repository files navigation

okapi

noun, plural o·ka·pis, (especially collectively) o·ka·pi

  1. An African mammal, Okapia johnstoni, closely related to and resembling the giraffe, but smaller and with a much shorter neck.
  2. A small framework for (OK) composable API clients.

Features

  • Transparent error handling
  • Flexible encoding / decoding
  • Configurable, with sensible defaults
  • Adheres to standard "net/http" interfaces

Examples

The Okapi client was desinged to be wrapped inside various helper structs, though it still can be used without any additional structs or fields.

type films struct {
    client *okapi.Client
}

func Films(client *okapi.Client) *films {
    return &films{client}
}

func (c *films) Get(name string) (*FilmsResponse, error) {
    var res FilmsResponse

    opts := &okapi.QueryOptions{
        Params: map[string]string{
            "search": name,
        },
        Out: &res,
    }

    if _, err := c.client.Get("/api/films/", opts); err != nil {
        return nil, err
    }
    return &res, nil
}

Using the client:

func main() {
    client, _ := okapi.NewClient(&okapi.Config{
        Address: BASE_URL,
    })

    res, _ := Films(client).Get("Dune")
}

For a complete example implementation, see examples/starwars

License

Okapi is available under the terms of the MIT License.

About

A framework for composable API clients

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages