go-client

module
v0.0.0-...-81dacae Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2020 License: Apache-2.0

README

go-client

This package works in concert with go-glitch to encourage code based error handling during inter-service communication. If a service returns a problem detail or http problem with a code field this client will facilitate calling that service and parsing the response into a glitch.DataError or a successful response.

Note that this package looks up the service using the provided finder every time a request is made. This allows it to work in more ephemeral environments where services might move frequently. If you have performance concerns about looking up service urls we suggest implementing a short cache in the ServiceFinder function.

Usage

The below usage is a quick guide. We recommend creating a service specific client that uses the base client under the covers to actually Do the request. This will make it easy to setup the client one time in the main of your service and pass it to your handlers as needed.

finder := func(serviceName string, useTLS bool) (url.URL, error) {
    u, err := url.Parse("http://example.com/")
    return *u, err
}
bc := NewBaseClient(finder, "example-service", false, 10*time.Second)

type user struct {
    ID int `json:"id"`
    Name string `json:"name"`
}
u := user{}
err := tc.client.Do(r.Context(), "GET", "v1/user/1", nil, nil, &u)
if err != nil {
    switch err.Code() {
    case "USER_NOT_FOUND":
        w.WriteHeader(http.StatusNotFound)
        // ...
    case "PERMISSION_DENIED": 
        w.WriteHeader(http.StatusForbidden)
        // ...
    case "USER_SETTING_PRIVATE"
        w.WriteHeader(http.StatusForbidden)
        // ...
    }
}

Directories

Path Synopsis
clientmock
Package clientmock is a generated GoMock package.
Package clientmock is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL