client

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

client impleemts a generic REST API client which can be used for creating gateway-specific clients

Index

Constants

View Source
const (
	DefaultTimeout            = time.Second * 30
	DefaultUserAgent          = "github.com/mutablelogic/go-client"
	PathSeparator             = string(os.PathSeparator)
	ContentTypeJson           = "application/json"
	ContentTypeTextXml        = "text/xml"
	ContentTypeApplicationXml = "application/xml"
	ContentTypeTextPlain      = "text/plain"
	ContentTypeTextHTML       = "text/html"
	ContentTypeBinary         = "application/octet-stream"
	ContentTypeForm           = "multipart/form-data"
)
View Source
const (
	Bearer = "Bearer"
)

Variables

This section is empty.

Functions

func NewLogTransport

func NewLogTransport(w io.Writer, parent http.RoundTripper, verbose bool) http.RoundTripper

NewLogTransport creates middleware into the request/response so you can log the transmission on the wire. Setting verbose to true also displays the body of each response

Types

type Client

type Client struct {
	sync.Mutex
	*http.Client
	// contains filtered or unexported fields
}

func New

func New(opts ...ClientOpt) (*Client, error)

New creates a new client with options. OptEndpoint is required as an option to set the endpoint for all requests.

func (*Client) Do

func (client *Client) Do(in Payload, out any, opts ...RequestOpt) error

Do a JSON request with a payload, populate an object with the response and return any errors

func (*Client) Request

func (client *Client) Request(req *http.Request, out any, opts ...RequestOpt) error

Do a HTTP request and decode it into an object

func (*Client) String

func (client *Client) String() string

type ClientOpt

type ClientOpt func(*Client) error

func OptEndpoint

func OptEndpoint(value string) ClientOpt

OptEndpoint sets the endpoint for all requests.

func OptHeader

func OptHeader(key, value string) ClientOpt

OptHeader appends a custom header to each request

func OptRateLimit

func OptRateLimit(value float32) ClientOpt

OptRateLimit sets the limit on number of requests per second and the API will sleep when exceeded. For account tokens this is 1 per second

func OptReqToken

func OptReqToken(value Token) ClientOpt

OptReqToken sets a request token for all client requests. This can be overridden by the client for individual requests using OptToken.

func OptSkipVerify

func OptSkipVerify() ClientOpt

OptSkipVerify skips TLS certificate domain verification

func OptStrict

func OptStrict() ClientOpt

OptStrict turns on strict content type checking on anything returned from the API

func OptTimeout

func OptTimeout(value time.Duration) ClientOpt

OptTimeout sets the timeout on any request. By default, a timeout of 10 seconds is used if OptTimeout is not set

func OptTrace

func OptTrace(w io.Writer, verbose bool) ClientOpt

OptTrace allows you to be the "man in the middle" on any requests so you can see traffic move back and forth. Setting verbose to true also displays the JSON response

func OptUserAgent

func OptUserAgent(value string) ClientOpt

OptUserAgent sets the user agent string on each API request It is set to the default if empty string is passed

type Payload

type Payload interface {
	io.Reader

	Method() string
	Accept() string
	Type() string
}

type Request

type Request struct {
	// contains filtered or unexported fields
}

func NewJSONRequest

func NewJSONRequest(payload any, accept string) (*Request, error)

Return a new request with a JSON payload which defaults to GET. The accept parameter is the accepted mime-type of the response.

func NewRequest

func NewRequest(accept string) *Request

Return a new empty request which defaults to GET. The accept parameter is the accepted mime-type of the response.

func (*Request) Accept

func (req *Request) Accept() string

Return the acceptable mimetype responses

func (*Request) Delete

func (req *Request) Delete() *Request

Set the HTTP method to DELETE

func (*Request) Method

func (req *Request) Method() string

Return the HTTP method

func (*Request) Post

func (req *Request) Post() *Request

Set the HTTP method to POST

func (*Request) Read

func (req *Request) Read(b []byte) (n int, err error)

Implements the io.Reader interface for a payload

func (*Request) String

func (req *Request) String() string

func (*Request) Type

func (req *Request) Type() string

Set the request mimetype

type RequestOpt

type RequestOpt func(*http.Request) error

func OptPath

func OptPath(value ...string) RequestOpt

OptPath appends path elements onto a request

func OptQuery

func OptQuery(value url.Values) RequestOpt

OptQuery adds query parameters to a request

func OptToken

func OptToken(value Token) RequestOpt

OptToken adds an authorization header. The header format is "Authorization: Bearer <token>"

type Token

type Token struct {
	Scheme string
	Value  string
}

func (Token) String

func (token Token) String() string

Stringify the token value

type Unmarshaler

type Unmarshaler interface {
	Unmarshal(mimetype string, r io.Reader) error
}

Unmarshaler is an interface which can be implemented by a type to unmarshal a response body

Jump to

Keyboard shortcuts

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