Documentation
¶
Overview ¶
Package httpclient hosts a simple HTTP client which supports sending and receiving JSON data using GET, POST, PUT, PATCH, and DELETE requests, with configurable timeouts.
To create a new client, you have to call the following code:
client := httpclient.NewClient()
If you want to adjust the timeouts:
timeouts := httpclient.NewDefaultTimeouts() // adjust any timeouts here client := httpclient.NewClient(httpclient.WithTimeouts(timeouts))
Then, to make a request, call one of the service methods, e.g.:
resp := client.GetJSON("http://site/path")
Once you have an user and a corresponding public API key, you can issue authenticated requests, by constructing a new client with the appropriate credentials:
client := httpclient.NewClient(httpclient.WithDigestAuthentication(username, password))
Index ¶
- Constants
- func CloseResponseBodyIfNotNil(resp HTTPResponse)
- func WithAcceptedStatusCodes(acceptedStatusCodes []int) func(*basicClient)
- func WithDefaultTimeouts() func(*basicClient)
- func WithDigestAuthentication(username string, password string) func(*basicClient)
- func WithTimeouts(t *RequestTimeouts) func(*basicClient)
- type BasicClient
- type HTTPResponse
- type RequestTimeouts
- type URLResolver
Constants ¶
const ( // ContentTypeJSON defines the JSON content type ContentTypeJSON = "application/json; charset=UTF-8" // PreferJSON signal that we are accepting JSON responses, but do not reject non-JSON data PreferJSON = "application/json;q=0.9, */*;q=0.8" )
Variables ¶
This section is empty.
Functions ¶
func CloseResponseBodyIfNotNil ¶
func CloseResponseBodyIfNotNil(resp HTTPResponse)
CloseResponseBodyIfNotNil simple helper which can ensure a response's body is correctly closed, if one exists
func WithAcceptedStatusCodes ¶
func WithAcceptedStatusCodes(acceptedStatusCodes []int) func(*basicClient)
WithAcceptedStatusCodes configures a client with a list of accepted HTTP response status codes
func WithDefaultTimeouts ¶
func WithDefaultTimeouts() func(*basicClient)
WithDefaultTimeouts configures a client with default timeouts
func WithDigestAuthentication ¶
WithDigestAuthentication configures a client with digest authentication credentials
func WithTimeouts ¶
func WithTimeouts(t *RequestTimeouts) func(*basicClient)
WithTimeouts configures a client with the specified timeouts
Types ¶
type BasicClient ¶
type BasicClient interface {
GetJSON(url string) HTTPResponse
PostJSON(url string, body io.Reader) HTTPResponse
PatchJSON(url string, body io.Reader) HTTPResponse
PutJSON(url string, body io.Reader) HTTPResponse
Delete(url string) HTTPResponse
}
BasicClient defines a contract for this client's API
func NewClient ¶
func NewClient(configs ...func(*basicClient)) BasicClient
NewClient builds a new client, allowing for dynamic configuration the order of the passed function matters, as they will be applied sequentially
type HTTPResponse ¶
HTTPResponse wrapper for HTTP response objects
func (HTTPResponse) Error ¶
func (r HTTPResponse) Error() string
Error implementation for error responses
func (HTTPResponse) IsError ¶
func (r HTTPResponse) IsError() bool
IsError returns true if the associated error is not nil
type RequestTimeouts ¶
type RequestTimeouts struct {
DialTimeout time.Duration
ExpectContinueTimeout time.Duration
IdleConnectionTimeout time.Duration
ResponseHeaderTimeout time.Duration
TLSHandshakeTimeout time.Duration
// GlobalTimeout the maximum allowed duration to complete a single HTTP request and response
GlobalTimeout time.Duration
}
RequestTimeouts allows users of this code to tweak all necessary timeouts used during an HTTP request-response
func NewDefaultTimeouts ¶
func NewDefaultTimeouts() *RequestTimeouts
NewDefaultTimeouts initializes the timeouts struct using default values
type URLResolver ¶
type URLResolver interface {
Of(path string, v ...interface{}) string
OfUnprefixed(path string, v ...interface{}) string
}
URLResolver contract for resolving any paths against the given base URL
func NewURLResolver ¶
func NewURLResolver(base string) URLResolver
NewURLResolver builds a new API URL which can be used to build any path
func NewURLResolverWithPrefix ¶
func NewURLResolverWithPrefix(base string, prefix string) URLResolver
NewURLResolverWithPrefix builds a new API URL using a prefix for all other paths