requester

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2021 License: MIT Imports: 11 Imported by: 1

Documentation

Overview

Package requester provides and interface and implementation for creating and executing requests to APIs.

Index

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 added in v0.2.0

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 added in v0.3.0

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.

func (*Client) Execute

func (c *Client) Execute(req *Request, successData, errorData interface{}) (bool, error)

Execute makes the given Request optionally decoding the response into given successData and/or errorData. The bool value returned indicates if the request was made successfully or not regardless of the response.

func (*Client) NewRequest

func (c *Client) NewRequest(ctx context.Context, apiName, method, url string, body io.Reader, options ...RequestOption) (*Request, error)

NewRequest creates a new Request for the given inputs applying any given RequestOption methods.

type ClientOption

type ClientOption func(*Client)

ClientOption defines configuration options for a Client.

func WithClient added in v0.2.0

func WithClient(hc *http.Client) ClientOption

WithClient sets the underlying *http.Client for a Client. Replaces any existing *http.Client.

func WithDatadog added in v0.4.0

func WithDatadog() ClientOption

WithDatadog ensures the underlying *http.Client is wrapped with datadog tracing when executing requests.

func WithRetry added in v0.1.0

func WithRetry() ClientOption

WithRetry sets the underlying *http.Client with one configured for automated retry. Replaces any existing *http.Client.

func WithTimeout added in v0.2.0

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 added in v0.3.0

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 Request

type Request struct {
	*http.Request
	// contains filtered or unexported fields
}

Request defines a http request to be made to an API.

type RequestOption

type RequestOption func(*Request)

RequestOption defines configuration options for a Request.

func WithUserAgent added in v0.2.0

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.

Jump to

Keyboard shortcuts

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