httpcall

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2021 License: MIT Imports: 6 Imported by: 0

README

httpcall

Calling HTTP endpoints is a bit easier now.

go get -u github.com/absurdlab/pkg/httpcall

Usage

var (
    payload = Greeting{Message: "hello"}
    success SuccessReply
    failure ErrorReply
    
    options = httpcall.Options().
    	POST("https://httpbin.org/anything").
    	JSON(payload).
        ToJSONSuccess(&success).
        ToJSONError(&failure)
)

_, _ = httpcall.Make(context.Background(), options)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Make

func Make(ctx context.Context, options *CallOptions) (*http.Response, error)

Make makes the http call according to the call context options and returns the http.Response.

Types

type CallOptions

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

func Options

func Options() *CallOptions

Options returns a new default call context, with the default http.Client; GET as http method; raw encoding for payload; 2XX status code to be considered successful response.

func (*CallOptions) AddHeaders

func (s *CallOptions) AddHeaders(kvs ...string) *CallOptions

AddHeaders adds the key value pairs as headers. If the supplied key values are not in pairs, this method panics.

func (*CallOptions) AddParams

func (s *CallOptions) AddParams(kvs ...string) *CallOptions

AddParams adds the key value pairs as query parameters. If the supplied key values are not in pairs, this method panics.

func (*CallOptions) DELETE

func (s *CallOptions) DELETE(url string) *CallOptions

DELETE is shortcut for WithMethod and WithURL

func (*CallOptions) Form

func (s *CallOptions) Form(payload interface{}) *CallOptions

Form sets the payload and also the "Content-Type" header to "application/x-www-form-urlencoded". If payload is nil, this method is noop. Check github.com/absurdlab/pkg/httpcodec for accepted types.

func (*CallOptions) GET

func (s *CallOptions) GET(url string) *CallOptions

GET is shortcut for WithMethod and WithURL

func (*CallOptions) IsSuccessWhenStatus

func (s *CallOptions) IsSuccessWhenStatus(statuses ...int) *CallOptions

IsSuccessWhenStatus sets the success criteria to that the response is only successful when it returns one of the supplied status code.

func (*CallOptions) IsSuccessWhenStatusInRange

func (s *CallOptions) IsSuccessWhenStatusInRange(lowerInclusive int, upperInclusive int) *CallOptions

IsSuccessWhenStatusInRange sets the success criteria to that the response is only successful when it returns a status code in the supplied range.

func (*CallOptions) JSON

func (s *CallOptions) JSON(payload interface{}) *CallOptions

JSON sets the payload and also the "Content-Type" header to "application/json". If payload is nil, this method is noop.

func (*CallOptions) PATCH

func (s *CallOptions) PATCH(url string) *CallOptions

PATCH is shortcut for WithMethod and WithURL

func (*CallOptions) POST

func (s *CallOptions) POST(url string) *CallOptions

POST is shortcut for WithMethod and WithURL

func (*CallOptions) PUT

func (s *CallOptions) PUT(url string) *CallOptions

PUT is shortcut for WithMethod and WithURL

func (*CallOptions) Plain

func (s *CallOptions) Plain(payload interface{}) *CallOptions

Plain sets the payload and also the "Content-Type" header to "text/plain". If payload is nil, this method is noop. Check github.com/absurdlab/pkg/httpcodec for accepted types.

func (*CallOptions) ToError

func (s *CallOptions) ToError(codec httpcodec.Decoder) *CallOptions

ToError sets error response codec. If codec is nil, this method is noop.

func (*CallOptions) ToFormError

func (s *CallOptions) ToFormError(destination *url.Values) *CallOptions

ToFormError sets error response codec to form decoder and sets "Accept" header to "application/x-www-form-urlencoded". If destination is nil, this method is noop.

func (*CallOptions) ToFormSuccess

func (s *CallOptions) ToFormSuccess(destination *url.Values) *CallOptions

ToFormSuccess sets success response codec to form decoder and sets "Accept" header to "application/x-www-form-urlencoded". If destination is nil, this method is noop.

func (*CallOptions) ToJSONError

func (s *CallOptions) ToJSONError(destination interface{}) *CallOptions

ToJSONError sets error response codec to json decoder and sets "Accept" header to "application/json". If destination is nil, this method is noop.

func (*CallOptions) ToJSONSuccess

func (s *CallOptions) ToJSONSuccess(destination interface{}) *CallOptions

ToJSONSuccess sets success response codec to json decoder and sets "Accept" header to "application/json". If destination is nil, this method is noop.

func (*CallOptions) ToPlainError

func (s *CallOptions) ToPlainError(destination io.Writer) *CallOptions

ToPlainError sets error response codec to form decoder and sets "Accept" header to "text/plain". If destination is nil, this method is noop.

func (*CallOptions) ToPlainSuccess

func (s *CallOptions) ToPlainSuccess(destination io.Writer) *CallOptions

ToPlainSuccess sets success response codec to form decoder and sets "Accept" header to "text/plain". If destination is nil, this method is noop.

func (*CallOptions) ToSuccess

func (s *CallOptions) ToSuccess(codec httpcodec.Decoder) *CallOptions

ToSuccess sets success response codec. If codec is nil, this method is noop.

func (*CallOptions) ToXMLError

func (s *CallOptions) ToXMLError(destination interface{}) *CallOptions

ToXMLError sets error response codec to xml decoder and sets "Accept" header to "application/xml". If destination is nil, this method is noop.

func (*CallOptions) ToXMLSuccess

func (s *CallOptions) ToXMLSuccess(destination interface{}) *CallOptions

ToXMLSuccess sets success response codec to xml decoder and sets "Accept" header to "application/xml". If destination is nil, this method is noop.

func (*CallOptions) WithClient

func (s *CallOptions) WithClient(client *http.Client) *CallOptions

WithClient configures a new http.Client.

func (*CallOptions) WithMethod

func (s *CallOptions) WithMethod(method string) *CallOptions

WithMethod configures a new HTTP method. If invalid HTTP method is supplied, this method is noop.

func (*CallOptions) WithPayload

func (s *CallOptions) WithPayload(payload interface{}, codec httpcodec.Encoder) *CallOptions

WithPayload sets custom payload and encoder. If either is nil, this method is noop.

func (*CallOptions) WithSuccessCriteria

func (s *CallOptions) WithSuccessCriteria(criteria func(resp *http.Response) bool) *CallOptions

WithSuccessCriteria sets the success criteria. If criteria is nil, this method is noop.

func (*CallOptions) WithURL

func (s *CallOptions) WithURL(url string) *CallOptions

WithURL sets the target url. If supplied url is empty, the method is noop.

func (*CallOptions) XML

func (s *CallOptions) XML(payload interface{}) *CallOptions

XML sets the payload and also the "Content-Type" header to "application/xml". If payload is nil, this method is noop.

Jump to

Keyboard shortcuts

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