Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { Client IHTTPClient Limiter *rate.Limiter Trace *httptrace.ClientTrace // contains filtered or unexported fields }
API Client
Like a finely-crafted sword, it can be wielded with skill if instructions are followed.
1) Create your config:
conf := &apiclient.Config{ RequestsPerSecond: 5, // 5 requests per second. Timeout: 5, // 5 seconds. }
2) Create your client
api := apiclient.NewClient(conf)
3) ???
params := &Params{ Url: "http://www.example.com/underpants", }
4) Profit
data, code, err := api.Get(params) // check `err` and `code` here. // `data` will need to be converted from `[]byte`.
func (*Client) Get ¶
Perform a HTTP GET using the given API parameters.
Returns the response body as an array of bytes, the HTTP status code, and an error if one is triggered.
You will need to remember to check the error *and* the status code.
type Config ¶
type Config struct { RequestsPerSecond int `json:"requests_per_second"` Timeout int `json:"timeout"` }
API client configuration.
`RequstsPerSecond` is the number of requests per second rate limiting. `Timeout` is obvious.
func NewDefaultConfig ¶
func NewDefaultConfig() *Config
Return a new default API client configuration.
type ContentType ¶
type IApiClient ¶
type IApiClient interface { Get(data *Params) ([]byte, int, error) Post(data *Params) ([]byte, int, error) }
API client interface.
type IHTTPClient ¶
HTTP client interface.
This is to allow mocking of `net/http`'s `http.Client` in unit tests. You could also, if drunk enough, provide your own HTTP client, as long as it conforms to the interface. But you wouldn't want to do that, would you.
type MockClient ¶ added in v0.1.1
type MockClient struct { GetFn MockCallbackFn PostFn MockCallbackFn // contains filtered or unexported fields }
func NewMockClient ¶ added in v0.1.1
func NewMockClient(config *Config, lgr logger.ILogger) *MockClient
type Params ¶
type Params struct { Url string // API URL. UseBasic bool UseToken bool Content ContentType Token AuthToken Basic AuthBasic Queries []*QueryParam }
API client request parameters
func (*Params) AddQueryParam ¶
func (p *Params) AddQueryParam(name, content string) *QueryParam
Add a new query parameter.
func (*Params) SetUseBasic ¶
Enable/disable basic authentication.
func (*Params) SetUseToken ¶
Enable/disable authentication token.
type QueryParam ¶
API URL query parameter.
func NewQueryParam ¶
func NewQueryParam(name, content string) *QueryParam
Create a new query parameter.