rclient: github.com/zpatrick/rclient Index | Files

package rclient

import "github.com/zpatrick/rclient"

Index

Package Files

client.go error.go options.go request_builder.go request_doer.go response_reader.go

func BuildJSONRequest Uses

func BuildJSONRequest(method, url string, body interface{}, options ...RequestOption) (*http.Request, error)

BuildJSONRequest creates a new *http.Request with the specified method, url and body in JSON format.

func ReadJSONResponse Uses

func ReadJSONResponse(resp *http.Response, v interface{}) error

ReadJSONResponse attempts to marshal the response body into v if and only if the response StatusCode is in the 200 range. Otherwise, an error is thrown. It assumes the response body is in JSON format.

type ClientOption Uses

type ClientOption func(client *RestClient)

A ClientOption configures a *RestClient.

func Builder Uses

func Builder(builder RequestBuilder) ClientOption

Builder sets the RequestBuilder field of a RestClient.

func Doer Uses

func Doer(doer RequestDoer) ClientOption

Doer sets the RequestDoer field of a RestClient.

func Reader Uses

func Reader(reader ResponseReader) ClientOption

Reader sets the ResponseReader field of a RestClient.

func RequestOptions Uses

func RequestOptions(options ...RequestOption) ClientOption

RequestOptions sets the RequestOptions field of a RestClient.

type RequestBuilder Uses

type RequestBuilder func(method, url string, body interface{}, options ...RequestOption) (*http.Request, error)

A RequestBuilder creates a *http.Request from the given parameters. It is important that each option gets added to the generated request:

req, _ := http.NewRequest(...)
for _, option := range options
    if err := option(req); err != nil {
        return nil, err
    }
}

type RequestDoer Uses

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

A RequestDoer sends a *http.Request and returns a *http.Response.

type RequestDoerFunc Uses

type RequestDoerFunc func(*http.Request) (*http.Response, error)

A RequestDoerFunc is a function that implements the RequestDoer interface.

func (RequestDoerFunc) Do Uses

func (d RequestDoerFunc) Do(req *http.Request) (*http.Response, error)

Do executes the RequestDoerFunc.

type RequestOption Uses

type RequestOption func(req *http.Request) error

A RequestOption configures a *http.Request.

func BasicAuth Uses

func BasicAuth(user, pass string) RequestOption

BasicAuth adds the specified username and password as basic auth to a request.

func Header(name, val string) RequestOption

Header adds the specified name and value as a header to a request.

func Headers Uses

func Headers(headers map[string]string) RequestOption

Headers adds the specified names and values as headers to a request

func Query Uses

func Query(query url.Values) RequestOption

Query adds the specified query to a request.

type ResponseError Uses

type ResponseError struct {
    Response *http.Response
    Message  string
}

func NewResponseError Uses

func NewResponseError(resp *http.Response, message string) *ResponseError

func NewResponseErrorf Uses

func NewResponseErrorf(resp *http.Response, format string, tokens ...interface{}) *ResponseError

func (*ResponseError) Error Uses

func (e *ResponseError) Error() string

type ResponseReader Uses

type ResponseReader func(resp *http.Response, v interface{}) error

A ResponseReader attempts to read a *http.Response into v.

type RestClient Uses

type RestClient struct {
    Host           string
    RequestBuilder RequestBuilder
    RequestDoer    RequestDoer
    ResponseReader ResponseReader
    RequestOptions []RequestOption
}

RestClient builds, executes, and reads http requests/responses.

func NewRestClient Uses

func NewRestClient(host string, options ...ClientOption) *RestClient

NewRestClient returns a new RestClient with all of the default fields. Any of the default fields can be changed with the options param.

func (*RestClient) Delete Uses

func (r *RestClient) Delete(path string, body, v interface{}, options ...RequestOption) error

Delete passes its params to RestClient.Do() with the "DELETE" method.

func (*RestClient) Do Uses

func (r *RestClient) Do(method, path string, body, v interface{}, options ...RequestOption) error

Do orchestrates building, performing, and reading http requests and responses.

func (*RestClient) Get Uses

func (r *RestClient) Get(path string, v interface{}, options ...RequestOption) error

Get passes its params to RestClient.Do() with the "GET" method.

func (*RestClient) Patch Uses

func (r *RestClient) Patch(path string, body, v interface{}, options ...RequestOption) error

Patch passes its params to RestClient.Do() with the "PATCH" method.

func (*RestClient) Post Uses

func (r *RestClient) Post(path string, body, v interface{}, options ...RequestOption) error

Post passes its params to RestClient.Do() with the "POST" method.

func (*RestClient) Put Uses

func (r *RestClient) Put(path string, body, v interface{}, options ...RequestOption) error

Put passes its params to RestClient.Do() with the "PUT" method.

Package rclient imports 5 packages (graph) and is imported by 5 packages. Updated 2018-09-28. Refresh now. Tools for package owners.