client

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 17 Imported by: 11

Documentation

Index

Constants

View Source
const (
	DefaultUserAgent      string = "go-rest-utility/v1"
	DefaultAcceptEncoding string = "gzip"
)

Variables

View Source
var (
	ErrBadRequest                   = newHTTPError(http.StatusBadRequest)
	ErrUnauthorized                 = newHTTPError(http.StatusUnauthorized)
	ErrPaymentRequired              = newHTTPError(http.StatusPaymentRequired)
	ErrForbidden                    = newHTTPError(http.StatusForbidden)
	ErrNotFound                     = newHTTPError(http.StatusNotFound)
	ErrMethodNotAllowed             = newHTTPError(http.StatusMethodNotAllowed)
	ErrNotAcceptable                = newHTTPError(http.StatusNotAcceptable)
	ErrProxyAuthRequired            = newHTTPError(http.StatusProxyAuthRequired)
	ErrRequestTimeout               = newHTTPError(http.StatusRequestTimeout)
	ErrConflict                     = newHTTPError(http.StatusConflict)
	ErrGone                         = newHTTPError(http.StatusGone)
	ErrLengthRequired               = newHTTPError(http.StatusLengthRequired)
	ErrPreconditionFailed           = newHTTPError(http.StatusPreconditionFailed)
	ErrRequestEntityTooLarge        = newHTTPError(http.StatusRequestEntityTooLarge)
	ErrRequestURITooLong            = newHTTPError(http.StatusRequestURITooLong)
	ErrUnsupportedMediaType         = newHTTPError(http.StatusUnsupportedMediaType)
	ErrRequestedRangeNotSatisfiable = newHTTPError(http.StatusRequestedRangeNotSatisfiable)
	ErrExpectationFailed            = newHTTPError(http.StatusExpectationFailed)
	ErrTeapot                       = newHTTPError(http.StatusTeapot)
	ErrMisdirectedRequest           = newHTTPError(http.StatusMisdirectedRequest)
	ErrUnprocessableEntity          = newHTTPError(http.StatusUnprocessableEntity)
	ErrLocked                       = newHTTPError(http.StatusLocked)
	ErrFailedDependency             = newHTTPError(http.StatusFailedDependency)
	ErrTooEarly                     = newHTTPError(http.StatusTooEarly)
	ErrUpgradeRequired              = newHTTPError(http.StatusUpgradeRequired)
	ErrPreconditionRequired         = newHTTPError(http.StatusPreconditionRequired)
	ErrTooManyRequests              = newHTTPError(http.StatusTooManyRequests)
	ErrRequestHeaderFieldsTooLarge  = newHTTPError(http.StatusRequestHeaderFieldsTooLarge)
	ErrUnavailableForLegalReasons   = newHTTPError(http.StatusUnavailableForLegalReasons)

	ErrInternalServerError           = newHTTPError(http.StatusInternalServerError)
	ErrNotImplemented                = newHTTPError(http.StatusNotImplemented)
	ErrBadGateway                    = newHTTPError(http.StatusBadGateway)
	ErrServiceUnavailable            = newHTTPError(http.StatusServiceUnavailable)
	ErrGatewayTimeout                = newHTTPError(http.StatusGatewayTimeout)
	ErrHTTPVersionNotSupported       = newHTTPError(http.StatusHTTPVersionNotSupported)
	ErrVariantAlsoNegotiates         = newHTTPError(http.StatusVariantAlsoNegotiates)
	ErrInsufficientStorage           = newHTTPError(http.StatusInsufficientStorage)
	ErrLoopDetected                  = newHTTPError(http.StatusLoopDetected)
	ErrNotExtended                   = newHTTPError(http.StatusNotExtended)
	ErrNetworkAuthenticationRequired = newHTTPError(http.StatusNetworkAuthenticationRequired)
)

Functions

func DecompressResponse added in v0.10.1

func DecompressResponse(resp http.Response) (io.ReadCloser, http.Header, error)

DecompressResponse takes a http response and returns a decompressed http.Body and a set of headers that matches the decompressed result. If the content-length header is 0, return the body and the header without decompressing.

Types

type BasicProblemDecoder added in v0.6.0

type BasicProblemDecoder struct{}

func (*BasicProblemDecoder) DecodeProblem added in v0.6.0

func (d *BasicProblemDecoder) DecodeProblem(_ context.Context, resp *http.Response) (problems.Problem, error)

type Client

type Client struct {
	BaseURL       *url.URL
	TokenProvider auth.TokenProvider
	// contains filtered or unexported fields
}

func NewClient

func NewClient(opts ...Option) *Client

NewClient will create a new REST Client.

func (*Client) Do

func (c *Client) Do(ctx context.Context, r *Request) (*Response, error)

Do Executes the http request, don't forget to call response.Close() if no error is returned

func (*Client) DoAndUnmarshal

func (c *Client) DoAndUnmarshal(ctx context.Context, r *Request, v interface{}) error

type GzipReader added in v0.10.1

type GzipReader struct {
	*gzip.Reader
	// contains filtered or unexported fields
}

func (*GzipReader) Close added in v0.10.1

func (r *GzipReader) Close() error

type HTTPError added in v0.6.0

type HTTPError struct {
	StatusCode int
	Status     string

	Instance string
	Body     string
}

func (HTTPError) Error added in v0.6.0

func (e HTTPError) Error() string

func (HTTPError) Is added in v0.6.0

func (e HTTPError) Is(target error) bool

type Option

type Option func(*Client)

func WithBaseURL

func WithBaseURL(baseURL string) Option

func WithCustomTransport added in v0.13.0

func WithCustomTransport(transport http.RoundTripper) Option

WithCustomTransport overwrites the default or otherwise configured http.RoundTripper transport on the underlying http.Client.

Not that WithOpenCensusTracing also sets a custom http.RoundTripper transport, you may not use both.

func WithDatadogTracing added in v0.2.0

func WithDatadogTracing(opts ...dd_http.RoundTripperOption) Option

WithDatadogTracing will add a Datadog transport to the client so that it will automatically inject trace-headers.

Should be used when you trace your application with Datadog.

Note, when used in AWS Lambda, make sure to set the service name.

client.WithDatadogTracing(
    dd_http.RTWithServiceName("<service_name>"),
)

func WithDefaultHeader added in v0.5.1

func WithDefaultHeader(header, value string) Option

func WithOpenCensusTracing added in v0.2.0

func WithOpenCensusTracing() Option

WithOpenCensusTracing will add an OpenCensus transport to the client so that it will automatically inject trace-headers.

Should be used when you trace your application with OpenCensus.

func WithProblemDecoder added in v0.6.0

func WithProblemDecoder(decoder ProblemDecoder) Option

func WithTimeout added in v0.10.2

func WithTimeout(timeout time.Duration) Option

WithTimeout sets http client timeout

The default timeout of zero means no timeout.

The Client cancels requests to the underlying Transport as if the Request's Context ended.

func WithTokenProvider

func WithTokenProvider(provider auth.TokenProvider) Option

type ProblemDecoder added in v0.6.0

type ProblemDecoder interface {
	DecodeProblem(context.Context, *http.Response) (problems.Problem, error)
}

type Request

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

func Connect

func Connect(uriTemplate string) *Request

func Delete

func Delete(uriTemplate string) *Request

func Get

func Get(uriTemplate string) *Request
func Head(uriTemplate string) *Request

func NewRequest

func NewRequest(method, uriTemplate string) *Request

func Options

func Options(uriTemplate string) *Request

func Patch

func Patch(uriTemplate string) *Request

func Post

func Post(uriTemplate string) *Request

func Put

func Put(uriTemplate string) *Request

func Trace

func Trace(uriTemplate string) *Request

func (*Request) Assign

func (r *Request) Assign(variable string, value interface{}) *Request

func (*Request) ExpandURL

func (r *Request) ExpandURL(baseURL *url.URL) (*url.URL, error)

ExpandURL combines the baseURL with the expanded URI template to form the final URL to be used for this Request. If no baseURL is provided the returned URL is just the expanded URI template

func (*Request) SetHeader

func (r *Request) SetHeader(key, value string) *Request

func (*Request) WithFollowRedirects added in v0.12.0

func (r *Request) WithFollowRedirects(follow bool) *Request

func (*Request) WithJSONPayload

func (r *Request) WithJSONPayload(payload interface{}) *Request

func (*Request) WithPayload added in v0.10.0

func (r *Request) WithPayload(contentType string, payload io.Reader) *Request

type Response

type Response struct {
	http.Response
}

func (*Response) Close added in v0.14.0

func (r *Response) Close() error

Close reads all of the body stream and closes it to make sure that tcp connections can be reused properly

func (*Response) Unmarshal

func (r *Response) Unmarshal(v interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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