quest

package module
v0.0.0-...-05c16d4 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2021 License: MIT Imports: 13 Imported by: 0

README

Quest

GoDoc A request (http) library for Go with a convenient, chain-able API.

Errors

If any method in the request's life-cycle errors, the every subsequent method will short circut and not be called. The Done method will return the first error that occured so that it can be handled.

Example

func GetUser(ctx context.Context, userId string) (user.User, error) {
	var user user.User
	err := quest.Post("/api/to/user/endpoint").
		WitContext(ctx). // used for open tracing
		QueryParam("includeDetails", "true").
		JSONBody(userId).
		Send().
		ExpectSuccess().
		GetJSON(&user).
		Done()

  return  user, err
}

Open Tracing

Documentation

Overview

Example
var body interface{}
err := Get("path/to/some/resource").
	Header("X-Some-Header", "value").
	Send().
	ExpectSuccess().
	GetJSON(&body).
	Done()

if err != nil {
	// handle error
}

// do something with body
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Next

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

Next is used to chain requests together

func (*Next) Delete

func (n *Next) Delete(path string) *Request

Delete creates a new http "Delete" request for path (uri) and is used when chaining requests together

func (*Next) Get

func (n *Next) Get(path string) *Request

Get creates a new http "GET" request for path (uri) and is used when chaining requests together

func (*Next) New

func (n *Next) New(method, path string) *Request

New creates a new request with given http method and path (uri) and is used when chaining requests together

func (*Next) Post

func (n *Next) Post(path string) *Request

Post creates a new http "POST" request for path (uri) and is used when chaining requests together

func (*Next) Put

func (n *Next) Put(path string) *Request

Put creates a new http "Put" request for path (uri) and is used when chaining requests together

type Request

type Request struct {
	*url.URL
	// contains filtered or unexported fields
}

Request is the HTTP request to be sent

func Delete

func Delete(path string) *Request

Delete creates a new http "Delete" request for path (uri)

func Get

func Get(path string) *Request

Get creates a new http "GET" request for path (uri)

func New

func New(method, path string) *Request

New creates a new request with given http method and path (uri)

func Post

func Post(path string) *Request

Post creates a new http "POST" request for path (uri)

func Put

func Put(path string) *Request

Put creates a new http "Put" request for path (uri)

func (*Request) BasicAuth

func (r *Request) BasicAuth(username, password string) *Request

BasicAuth sets a header on request with given key and value

func (*Request) Body

func (r *Request) Body(value *bytes.Buffer) *Request

Body sets the body for the request

func (*Request) Header

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

Header sets a header on request with given key and value

func (*Request) JSONBody

func (r *Request) JSONBody(value interface{}) *Request

JSONBody sets the given value as a JSON encoded string as the body of the request

func (*Request) MarshalJSON

func (r *Request) MarshalJSON() ([]byte, error)

MarshalJSON implements `jsoniter.Marshaler` interface

func (*Request) MultipartBody

func (r *Request) MultipartBody(form *questmultipart.Form) *Request

MultipartBody will set a multipart form as the body of the request

func (*Request) Param

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

Param replaces url param (denoted with `:key`) with given value

func (*Request) QueryParam

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

QueryParam adds a query param to the url

func (*Request) Send

func (r *Request) Send() *Response

Send sends the request and returns the response

func (*Request) UnmarshalJSON

func (r *Request) UnmarshalJSON(b []byte) error

UnmarshalJSON implements `jsoniter.Unmarshaler` interface

func (*Request) WithContext

func (r *Request) WithContext(ctx context.Context) *Request

WithContext sets up a context for this request

func (*Request) WithTransport

func (r *Request) WithTransport(transport *http.Transport) *Request

WithTransport sets the transport for the http client

type Response

type Response struct {
	*http.Response
	// contains filtered or unexported fields
}

Response is the HTTP response

func (*Response) Done

func (r *Response) Done() error

Done will return the first error that occured durring the request's life-cycle

It is important to note that if any method errors, all subsequest methods will short circut and not be execuited

func (*Response) ExpectHeader

func (r *Response) ExpectHeader(key, value string) *Response

ExpectHeader will error if given header is not set with given value

func (*Response) ExpectStatusCode

func (r *Response) ExpectStatusCode(code int) *Response

ExpectStatusCode will error if StatusCode is not specified code

func (*Response) ExpectSuccess

func (r *Response) ExpectSuccess() *Response

ExpectSuccess will error if StatusCode is not in 200 range

func (*Response) ExpectType

func (r *Response) ExpectType(value string) *Response

ExpectType will error if header "Content-Type" is not specified value

func (*Response) GetBody

func (r *Response) GetBody(into *string) *Response

GetBody stores the response body into into param

func (*Response) GetHeader

func (r *Response) GetHeader(key string, into *string) *Response

GetHeader stores header value with key into into paramiter

func (*Response) GetJSON

func (r *Response) GetJSON(into interface{}) *Response

GetJSON decodes and stores the response body

func (*Response) MarshalJSON

func (r *Response) MarshalJSON() ([]byte, error)

MarshalJSON implements `jsoniter.Marshaler` interface

func (*Response) Next

func (r *Response) Next() *Next

Next allows a new request to be chained onto this request, assuming the first request did not fail

func (*Response) Proxy

func (r *Response) Proxy(w io.Writer) *Response

Proxy copies the body of the response to a given writer

func (*Response) UnmarshalJSON

func (r *Response) UnmarshalJSON(b []byte) error

UnmarshalJSON implements `jsoniter.Unmarshaler` interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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