Documentation
¶
Overview ¶
Package requester provides and interface and implementation for creating and executing requests to APIs.
Index ¶
- type API
- type APIOption
- type Client
- type ClientOption
- type ContentType
- type Discoverer
- type MockClient
- func (m *MockClient) AddAPI(apiName string, discoverer Discoverer, options ...APIOption) error
- func (m *MockClient) Execute(req *Request, successData, errorData interface{}) (bool, error)
- func (m *MockClient) NewRequest(ctx context.Context, apiName, method, url string, body io.Reader, ...) (*Request, error)
- type Request
- type RequestOption
- type Requester
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type API ¶
type API struct { Discoverer // contains filtered or unexported fields }
API defines an API and is embedded in a Client via AddAPI.
type APIOption ¶
type APIOption func(*API)
APIOption defines configuration options for an API.
func WithContentType ¶
func WithContentType(ct ContentType) APIOption
WithContentType sets the ContentType for an API. If not specified the default ApplicationJSON is used.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client implements the Requester interface.
func NewClient ¶
func NewClient(name string, options ...ClientOption) *Client
NewClient creates a new Client with sane defaults and applies any given ClientOption methods.
func (*Client) AddAPI ¶
func (c *Client) AddAPI(name string, discoverer Discoverer, options ...APIOption) error
AddAPI adds an API with the given name and Discover to the Client applying any given APIOption methods.
type ClientOption ¶
type ClientOption func(*Client)
ClientOption defines configuration options for a Client.
func WithClient ¶
func WithClient(hc *http.Client) ClientOption
WithClient sets the underlying *http.Client for a Client. Replaces any existing *http.Client.
func WithDatadog ¶
func WithDatadog() ClientOption
WithDatadog ensures the underlying *http.Client is wrapped with datadog tracing when executing requests.
func WithRetry ¶
func WithRetry() ClientOption
WithRetry sets the underlying *http.Client with one configured for automated retry. Replaces any existing *http.Client.
func WithTimeout ¶
func WithTimeout(t time.Duration) ClientOption
WithTimeout sets the *http.Client.Timeout to the provided value. Be sure to call this after configuring any *http.Client (e.g. WithClient, WithRetry, ...).
type ContentType ¶
type ContentType string
ContentType is an http Content-Type value.
var ( // ApplicationJSON is the application/json Content-Type. ApplicationJSON ContentType = "application/json" // TextCSV is the text/csv Content-Type. TextCSV ContentType = "text/csv" )
func (ContentType) String ¶
func (c ContentType) String() string
String implements the Stringer interface returning the Contenty-Type string.
type Discoverer ¶
type Discoverer interface {
URL() string
}
Discoverer defines an interface that allows for dynamic was of discovering a URL for a request.
type MockClient ¶
type MockClient struct { AddAPIFn func(apiName string, discoverer Discoverer, options ...APIOption) error AddAPIFnCalled bool NewRequestFn func( ctx context.Context, apiName, method, url string, body io.Reader, options ...RequestOption) (*Request, error) NewRequestFnCalled bool ExecuteFn func(req *Request, successData, errorData interface{}) (bool, error) ExecuteFnCalled bool }
MockClient implements the Requester interface allowing for complete control over the returned values.
func (*MockClient) AddAPI ¶
func (m *MockClient) AddAPI(apiName string, discoverer Discoverer, options ...APIOption) error
AddAPI implements the Requester.MustAddAPI method.
func (*MockClient) Execute ¶
func (m *MockClient) Execute(req *Request, successData, errorData interface{}) (bool, error)
Execute implements the Requester.Execute method.
func (*MockClient) NewRequest ¶
func (m *MockClient) NewRequest( ctx context.Context, apiName, method, url string, body io.Reader, options ...RequestOption) (*Request, error)
NewRequest implements the Requester.NewRequest method.
type RequestOption ¶
type RequestOption func(*Request)
RequestOption defines configuration options for a Request.
func WithUserAgent ¶
func WithUserAgent(ua string) RequestOption
WithUserAgent sets the user agent to be used on the Request.
type Requester ¶
type Requester interface { AddAPI(apiName string, discoverer Discoverer, options ...APIOption) error NewRequest( ctx context.Context, apiName, method, url string, body io.Reader, options ...RequestOption) (*Request, error) Execute(req *Request, successData, errorData interface{}) (bool, error) }
Requester defines an interface for creating and executing requests to an API.