client

package
v1.1.16 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: Apache-2.0 Imports: 15 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 * 10
	DefaultUserAgent          = "github.com/mutablelogic/go-server"
	PathSeparator             = string(os.PathSeparator)
	ContentTypeJson           = "application/json"
	ContentTypeTextXml        = "text/xml"
	ContentTypeApplicationXml = "application/xml"
	ContentTypeTextPlain      = "text/plain"
	ContentTypeTextHTML       = "text/html"
	ContentTypeBinary         = "application/octet-stream"
)

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) 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 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 string) ClientOpt

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

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 {
	Type() string
	Method() string
	Accept() string
}

func NewGetPayload

func NewGetPayload(accept string) Payload

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 string) RequestOpt

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

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

Directories

Path Synopsis
html implements a generic API client which parses HTML and XML files.
html implements a generic API client which parses HTML and XML files.
ipify implements a generic API client which parses a JSON response.
ipify implements a generic API client which parses a JSON response.

Jump to

Keyboard shortcuts

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