Skip to content

A package that facilitates implementing go clients that communicate with HTTP services that return HTTP problems

License

Notifications You must be signed in to change notification settings

healthimation/go-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
        // ...
    }
}

About

A package that facilitates implementing go clients that communicate with HTTP services that return HTTP problems

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages