httpclient

package
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Overview

Package httpclient provides a simple Golang Client for JSON Http Microservices

Acknowledgement:

Index

Constants

View Source
const (
	DefaultInitialInterval     = 50 * time.Millisecond
	DefaultRandomizationFactor = 0.5
	DefaultMultiplier          = 1.5
	DefaultMaxInterval         = 5 * time.Second
	DefaultMaxElapsedTime      = 60 * time.Second
)

Default values for ExponentialBackOff.

Variables

This section is empty.

Functions

func NewExponentialBackOff

func NewExponentialBackOff() *backoff.ExponentialBackOff

NewExponentialBackOff creates an instance of ExponentialBackOff using default values.

Types

type Client

type Client struct {

	// Base URL for API requests.
	BaseURL string

	// User agent for client
	UserAgent string
	// contains filtered or unexported fields
}

func Default

func Default() *Client

func New

func New(client *http.Client, headers map[string]string, b backoff.BackOff) *Client

func (*Client) Call

func (c *Client) Call(method, path string, reqBody, resType interface{}, needAuth bool) (*http.Response, error)

func (*Client) Do

func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error)

Do sends an API request and returns the API response. The API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface, the raw response will be written to v, without attempting to decode it.

func (*Client) NewRequest

func (c *Client) NewRequest(method, path string, request interface{}) (*http.Request, error)

NewRequest creates an API request. A relative URL can be provided in urlStr, which will be resolved to the BaseURL of the Client. Relative UfaRLS should always be specified without a preceding slash. If specified, the value pointed to by body is JSON encoded and included in as the request body.

func (*Client) WithBaseURL

func (c *Client) WithBaseURL(baseURL string) *Client

func (*Client) WithBasicAuth

func (c *Client) WithBasicAuth(username, password string) *Client

func (*Client) WithBearerToken

func (c *Client) WithBearerToken(token string) *Client

func (*Client) WithInsecureSkipVerify

func (c *Client) WithInsecureSkipVerify() *Client

func (*Client) WithTLSConfig

func (c *Client) WithTLSConfig(caCert []byte, clientPair ...[]byte) *Client

func (*Client) WithTimeout

func (c *Client) WithTimeout(timeout time.Duration) *Client

func (*Client) WithUserAgent

func (c *Client) WithUserAgent(ua string) *Client

type ErrorResponse

type ErrorResponse struct {
	// HTTP response that caused this error
	Response *http.Response

	// Error message
	Message string `json:"message"`
}

An ErrorResponse reports the error caused by an API request

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

Jump to

Keyboard shortcuts

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