httpman

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2021 License: MIT Imports: 9 Imported by: 1

README

httpman

httpman is Lightweight and fast fluent interface wrapper over Http Client to make REST calls easier

Features
  • Fluent request building, Add or Set Request Headers/Query
  • Extendability to different endpoints
  • Encode structs and key value pair into URL query
  • Encode JSON payload into request
  • Decode JSON success or failure responses
  • Request retries [To Do]
  • Fake HTTP responses for testing [To Do]
  • File Message Store [To Do]
Usage
import "github.com/kunal-saini/httpman"
Examples
        resMap := make(map[string]interface{})
	errMap := make(map[string]interface{})

	type QueryParams struct {
		Foo string `url:"foo"`
	}

	client := httpman.
		New("https://example.com/").
		AddQueryStruct(&QueryParams{Foo: "bar"}).
		AddHeader("X-Key", "value")

	req, err := client.
		NewRequest().
		Get("path/to/resource").
		AddQueryParam("resource_key", "value").
		SetHeader("X-Resource-Key", "value").
		Decode(&resMap, &errMap)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BodyProvider

type BodyProvider interface {
	// ContentType returns the Content-Type of the body.
	ContentType() string
	// Body returns the io.Reader body.
	Body() (io.Reader, error)
}

BodyProvider provides Body content for http.Request attachment.

type Executor

type Executor interface {
	Do(req *http.Request) (*http.Response, error)
}

Executor executes http requests. It is implemented by *http.Client. You can wrap *http.Client with layers of Doers to form a stack of client-side middleware.

type Httpman

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

Httpman is an HTTP Request builder and sender.

func New

func New(baseURL string) *Httpman

New returns a new instance with an http DefaultClient.

func (*Httpman) AddHeader

func (h *Httpman) AddHeader(key, value string) *Httpman

AddHeader adds the key, value pair in Headers, appending values for existing keys to the key's values. Header keys are canonicalize.

func (*Httpman) AddQueryParam

func (h *Httpman) AddQueryParam(key, value string) *Httpman

AddQueryParam appends query param to the queryStruct. The value pointed to by each queryStruct will be encoded as url query parameters on

func (*Httpman) AddQueryStruct

func (h *Httpman) AddQueryStruct(queryStruct interface{}) *Httpman

QueryStruct appends the queryStruct to the queryStructs. The value pointed to by each queryStruct will be encoded as url query parameters on The queryStruct argument should be a pointer to a url tagged struct.

func (*Httpman) Client

func (h *Httpman) Client(httpClient *http.Client) *Httpman

Client sets the http Client used to do requests. If a nil client is given, the http.DefaultClient will be used.

func (*Httpman) Doer

func (h *Httpman) Doer(doer Executor) *Httpman

Doer sets the custom Doer implementation used to do requests. If a nil client is given, the http.DefaultClient will be used.

func (*Httpman) NewRequest

func (h *Httpman) NewRequest() *Request

initiates a new request with defaults

func (*Httpman) SetBasicAuth

func (h *Httpman) SetBasicAuth(username, password string) *Httpman

SetBasicAuth sets the Authorization header to use HTTP Basic Authentication with the provided username and password. With HTTP Basic Authentication the provided username and password are not encrypted.

func (*Httpman) SetHeader

func (h *Httpman) SetHeader(key, value string) *Httpman

SetHeader sets the key, value pair in Headers, replacing existing values associated with key. Header keys are canonicalize.

type Request

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

Request

func (*Request) AddQueryParam

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

AddQueryParam appends query param to the queryStruct. The value pointed to by each queryStruct will be encoded as url query parameters on

func (*Request) AddQueryStruct

func (r *Request) AddQueryStruct(queryStruct interface{}) *Request

QueryStruct appends the queryStruct to the queryStructs. The value pointed to by each queryStruct will be encoded as url query parameters on The queryStruct argument should be a pointer to a url tagged struct.

func (*Request) Body

func (r *Request) Body(body io.Reader) *Request

Body sets the body. The body value will be set as the Body on new If the provided body is also an io.Closer, the request Body will be closed by http.Client methods.

func (*Request) BodyForm

func (r *Request) BodyForm(bodyForm interface{}) *Request

BodyForm sets the bodyForm. The value pointed to by the bodyForm will be url encoded as the Body on new requests. The bodyForm argument should be a pointer to a url tagged struct.

func (*Request) BodyJSON

func (r *Request) BodyJSON(bodyJSON interface{}) *Request

BodyJSON sets the bodyJSON. The value pointed to by the bodyJSON will be JSON encoded as the Body on new requests. The bodyJSON argument should be a pointer to a JSON tagged struct.

func (*Request) BodyProvider

func (r *Request) BodyProvider(body BodyProvider) *Request

BodyProvider sets the body provider.

func (*Request) Connect

func (r *Request) Connect(pathURL string) *Request

Connect sets method to CONNECT and sets the given pathURL.

func (*Request) Decode

func (r *Request) Decode(successV, failureV interface{}) (*http.Response, error)

Receive creates a new HTTP request and returns the response. Success responses (2XX) are JSON decoded into the value pointed to by successV and other responses are JSON decoded into the value pointed to by failureV. If the status code of response is 204(no content) or the Content-Length is 0, decoding is skipped. Any error creating the request, sending it, or decoding the response is returned. Receive is shorthand for calling Request and Do.

func (*Request) DecodeSuccess

func (r *Request) DecodeSuccess(successV interface{}) (*http.Response, error)

ReceiveSuccess creates a new HTTP request and returns the response. Success responses (2XX) are JSON decoded into the value pointed to by successV. Any error creating the request, sending it, or decoding a 2XX response is returned.

func (*Request) Delete

func (r *Request) Delete(pathURL string) *Request

Delete sets method to DELETE and sets the given pathURL.

func (*Request) Do

func (r *Request) Do(req *http.Request, successV, failureV interface{}) (*http.Response, error)

Do sends an HTTP request and returns the response. Success responses (2XX) are JSON decoded into the value pointed to by successV and other responses are JSON decoded into the value pointed to by failureV. If the status code of response is 204(no content) or the Content-Length is 0, decoding is skipped. Any error sending the request or decoding the response is returned.

func (*Request) Get

func (r *Request) Get(pathURL string) *Request

Get sets method to GET and sets the given pathURL.

func (*Request) Head

func (r *Request) Head(pathURL string) *Request

Head sets method to HEAD and sets the given pathURL.

func (*Request) Options

func (r *Request) Options(pathURL string) *Request

Options sets method to OPTIONS and sets the given pathURL.

func (*Request) Patch

func (r *Request) Patch(pathURL string) *Request

Patch sets method to PATCH and sets the given pathURL.

func (*Request) Path

func (r *Request) Path(path string) *Request

Path extends the rawURL with the given path by resolving the reference to an absolute URL. If parsing errors occur, the baseURL is left unmodified.

func (*Request) Post

func (r *Request) Post(pathURL string) *Request

Post sets method to POST and sets the given pathURL.

func (*Request) Put

func (r *Request) Put(pathURL string) *Request

Put sets method to PUT and sets the given pathURL.

func (*Request) Send

func (r *Request) Send() (*http.Request, error)

func (*Request) SetHeader

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

SetHeader sets the key, value pair in Headers, replacing existing values associated with key. Header keys are canonicalize.

func (*Request) Trace

func (r *Request) Trace(pathURL string) *Request

Trace sets method to TRACE and sets the given pathURL.

type ResponseDecoder

type ResponseDecoder interface {
	// Decode decodes the response into the value pointed to by v.
	Decode(resp *http.Response, v interface{}) error
}

ResponseDecoder decodes http responses into struct values.

Jump to

Keyboard shortcuts

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