nougat

package module
v0.0.0-...-7edd032 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2020 License: MIT Imports: 9 Imported by: 0

README

Project Nougat

Godoc Go Report Card [License]

Project Nougat is an agnostic component we actively use at Chamaconekt Kenya to help us create and send Golang http API requests. It abstracts reimplementing logic common to all clients, by storing HTTP request properties to simplify sending requests and decoding responses.

Features

  • Method Setters: Get/Post/Put/Patch/Delete/Head
  • Add or Set Request Headers
  • Base/Path: Extend a Nougat for different endpoints
  • Encode structs into URL query parameters
  • Encode a form or JSON into the Request Body
  • Receive JSON success or failure responses

Install

go get github.com/wondenge/nougat

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Message string `json:"message"`
	Code    int    `json:"code"`
}

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)
}

type Doer

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

Doer 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 Nougat

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

Nougat is an HTTP Request builder and sender.

func New

func New() *Nougat

New returns a new Nougat with an http DefaultClient.

func (*Nougat) Add

func (r *Nougat) Add(key, value string) *Nougat

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

func (*Nougat) Base

func (r *Nougat) Base(rawURL string) *Nougat

Base sets the rawURL. If you intend to extend the url with Path, baseUrl should be specified with a trailing slash.

func (*Nougat) Body

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

Body sets the Nougat's body. The body value will be set as the Body on new requests (see Request()). If the provided body is also an io.Closer, the request Body will be closed by http.Client methods.

func (*Nougat) BodyForm

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

BodyForm sets the Nougat's bodyForm. The value pointed to by the bodyForm will be url encoded as the Body on new requests (see Request()). The bodyForm argument should be a pointer to a url tagged struct. See https://godoc.org/github.com/google/go-querystring/query for details.

func (*Nougat) BodyJSON

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

BodyJSON sets the Nougat's bodyJSON. The value pointed to by the bodyJSON will be JSON encoded as the Body on new requests (see Request()). The bodyJSON argument should be a pointer to a JSON tagged struct. See https://golang.org/pkg/encoding/json/#MarshalIndent for details.

func (*Nougat) BodyProvider

func (r *Nougat) BodyProvider(body BodyProvider) *Nougat

BodyProvider sets the Nougat's body provider.

func (*Nougat) Client

func (r *Nougat) Client(httpClient *http.Client) *Nougat

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

func (*Nougat) Connect

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

Connect sets the Nougat method to CONNECT and sets the given pathURL.

func (*Nougat) Delete

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

Delete sets the Nougat method to DELETE and sets the given pathURL.

func (*Nougat) Do

func (r *Nougat) 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), decoding is skipped. Any error sending the request or decoding the response is returned.

func (*Nougat) Doer

func (r *Nougat) Doer(doer Doer) *Nougat

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

func (*Nougat) Get

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

Get sets the Nougat method to GET and sets the given pathURL.

func (*Nougat) Head

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

Head sets the Nougat method to HEAD and sets the given pathURL.

func (*Nougat) New

func (r *Nougat) New() *Nougat

New returns a copy of a Nougat for creating a new Nougat with properties from a parent Nougat. For example,

parentNougat := Nougat.New().Client(client).Base("https://api.io/")
fooNougat := parentNougat.New().Get("foo/")
barNougat := parentNougat.New().Get("bar/")

fooNougat and barNougat will both use the same client, but send requests to https://api.io/foo/ and https://api.io/bar/ respectively.

Note that query and body values are copied so if pointer values are used, mutating the original value will mutate the value within the child Nougat.

func (*Nougat) Options

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

Options sets the Nougat method to OPTIONS and sets the given pathURL.

func (*Nougat) Patch

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

Patch sets the Nougat method to PATCH and sets the given pathURL.

func (*Nougat) Path

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

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

func (*Nougat) Post

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

Post sets the Nougat method to POST and sets the given pathURL.

func (*Nougat) Put

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

Put sets the Nougat method to PUT and sets the given pathURL.

func (*Nougat) QueryStruct

func (r *Nougat) QueryStruct(queryStruct interface{}) *Nougat

QueryStruct appends the queryStruct to the Nougat's queryStructs. The value pointed to by each queryStruct will be encoded as url query parameters on new requests (see Request()). The queryStruct argument should be a pointer to a url tagged struct. See https://godoc.org/github.com/google/go-querystring/query for details.

func (*Nougat) Receive

func (r *Nougat) Receive(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), 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 (*Nougat) ReceiveSuccess

func (r *Nougat) ReceiveSuccess(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 (*Nougat) Request

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

Request returns a new http.Request created with the Nougat properties. Returns any errors parsing the rawURL, encoding query structs, encoding the body, or creating the http.Request.

func (*Nougat) ResponseDecoder

func (r *Nougat) ResponseDecoder(decoder ResponseDecoder) *Nougat

ResponseDecoder sets the Nougat's response decoder.

func (*Nougat) Set

func (r *Nougat) Set(key, value string) *Nougat

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

func (*Nougat) SetBasicAuth

func (r *Nougat) SetBasicAuth(username, password string) *Nougat

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 (*Nougat) Trace

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

Trace sets the Nougat method to TRACE and sets the given pathURL.

type ResponseDecoder

type ResponseDecoder interface {
	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