client

package
v0.0.0-...-51a7f25 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2021 License: Apache-2.0 Imports: 12 Imported by: 3

Documentation

Overview

Package client contains functions required to make HTTP calls.

Index

Constants

View Source
const (
	HeaderAuth                  = "Authorization"
	HeaderBear                  = "Bearer "
	HTTPContentType             = "Content-Type"
	ContentTypeApplicationJSON  = "application/json"
	ContentTypeURLEncoded       = "application/x-www-form-urlencoded; param=value"
	ErrMsgUnableToCreateReq     = "unable to create request"
	ErrMsgUnableToParseReqBody  = "unable to parse request body"
	ErrMsgUnableToParseRespBody = "unable to parse response body, context: %s "
	ErrMsgUnableInitiateReq     = "unable to initiate request: %s"
	ErrMsgUnsuccessfulAPICall   = "unsuccessful API call: %s response Code: %s URL: %s"
	ErrMsgUnableToCloseBody     = "unable to close the body"
)

Variables

View Source
var ErrInvalidParameters = errors.New("invalid parameters")

Functions

func B64BasicAuth

func B64BasicAuth(u, p string) (string, error)

B64BasicAuth returns a base64 encoded value of "u:p" string and any error encountered.

func BodyReader

func BodyReader(v interface{}) (io.ReadSeeker, error)

BodyReader returns the byte buffer representation of the provided struct and any error encountered.

func Configure

func Configure(c *config.Client)

Configure overrides the default client values. This function should be called before calling Invoke method.

func Invoke

func Invoke(context string, req *HTTPRequest, body interface{}, expectedRespCode int) error

Invoke the request and parse the response body to the given struct. context parameter is used to maintain the request context in the log. resCode parameter is used to determine the desired response code. Returns any error encountered.

func ParseBody

func ParseBody(res *http.Response, v interface{}) error

ParseBody parse response body into the given struct. Must send the pointer to the response body. Returns any error encountered.

Types

type BackOffPolicy

type BackOffPolicy func(min, max time.Duration, attempt int) time.Duration

BackOffPolicy policy determines the duration between two retires

type Client

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

Client represent the state of the HTTP client.

type HTTPRequest

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

HTTPRequest wraps the http.request and the Body. Body is wrapped with io.ReadSeeker which allows to reset the body buffer reader to initial state in retires.

func CreateHTTPDELETERequest

func CreateHTTPDELETERequest(token, url string) (*HTTPRequest, error)

CreateHTTPDELETERequest returns a DELETE HTTP request with a Bearer token header and any error encountered.

func CreateHTTPGETRequest

func CreateHTTPGETRequest(token, url string) (*HTTPRequest, error)

CreateHTTPGETRequest returns a GET HTTP request with a Bearer token header and any error encountered.

func CreateHTTPPOSTRequest

func CreateHTTPPOSTRequest(token, url string, body io.ReadSeeker) (*HTTPRequest, error)

CreateHTTPPOSTRequest returns a POST HTTP request with a Bearer token header with the content type to application/json and any error encountered.

func CreateHTTPPUTRequest

func CreateHTTPPUTRequest(token, url string, body io.ReadSeeker) (*HTTPRequest, error)

CreateHTTPPUTRequest returns a PUT HTTP request with a Bearer token header with the content type to application/json and any error encountered.

func CreateHTTPRequest

func CreateHTTPRequest(method, url string, body io.ReadSeeker) (*HTTPRequest, error)

CreateHTTPRequest returns client.HTTPRequest struct which wraps the http.request, request Body, and any error encountered.

func (*HTTPRequest) HTTPRequest

func (r *HTTPRequest) HTTPRequest() *http.Request

HTTPRequest returns the HTTP request.

func (*HTTPRequest) SetHeader

func (r *HTTPRequest) SetHeader(k, v string)

SetHeader method set the given header key and value to the HTTP request.

type InvokeError

type InvokeError struct {
	StatusCode int
	// contains filtered or unexported fields
}

InvokeError wraps more information about the error.

func (*InvokeError) Error

func (e *InvokeError) Error() string

type RetryPolicy

type RetryPolicy func(resp *http.Response) bool

RetryPolicy defines a function which validate the response and apply desired policy to determine whether to retry the particular request or not.

Jump to

Keyboard shortcuts

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