go-sawyer: github.com/lostisland/go-sawyer Index | Files | Directories

package sawyer

import "github.com/lostisland/go-sawyer"

Index

Package Files

cache.go request.go response.go sawyer.go

Constants

const (
    HeadMethod    = "HEAD"
    GetMethod     = "GET"
    PostMethod    = "POST"
    PutMethod     = "PUT"
    PatchMethod   = "PATCH"
    DeleteMethod  = "DELETE"
    OptionsMethod = "OPTIONS"
)

func UseApiError Uses

func UseApiError(status int) bool

UseApiError determines if the given status is considered an API error.

type CachedResponse Uses

type CachedResponse interface {
    Decode(*Request) *Response
    SetupRequest(*http.Request)
    IsFresh() bool
    IsExpired() bool
}

CachedResponse is an interface for the httpcache CachedResponseDecoder.

type Cacher Uses

type Cacher interface {
    // Get gets a CachedResponse for the given request.
    Get(*http.Request) (CachedResponse, error)

    // Set caches the response for the given request.
    Set(*http.Request, *Response) error

    // Reset removes the cached response and body, but leaves the cached relations.
    Reset(*http.Request) error

    // Clear removes all cached information for the request.
    Clear(*http.Request) error

    // UpdateCache updates the cache for the given request with the expiration from
    // the response.
    UpdateCache(*http.Request, *http.Response) error

    // SetRels caches the given relations for the request.
    SetRels(*http.Request, hypermedia.Relations) error

    // Rels gets the cached relations for the given request.
    Rels(*http.Request) (hypermedia.Relations, bool)
}

A Cacher has the ability to get and set caches for HTTP requests and resource relations. See the sawyer/httpcache package.

type Client Uses

type Client struct {
    HttpClient *http.Client
    Endpoint   *url.URL
    Header     http.Header
    Query      url.Values
    Cacher     Cacher
}

A Client wraps an *http.Client with a base url Endpoint and common header and query values.

func New Uses

func New(endpoint *url.URL, client *http.Client) *Client

New returns a new Client with a given a URL and an optional client.

func NewFromString Uses

func NewFromString(endpoint string, client *http.Client) (*Client, error)

NewFromString returns a new Client given a string URL and an optional client.

func (*Client) NewRequest Uses

func (c *Client) NewRequest(rawurl string) (*Request, error)

NewRequest creates a new sawyer.Request for the given relative url path, with any default headers or query parameters specified on Client. The Request URL is resolved to an absolute URL.

func (*Client) Rels Uses

func (c *Client) Rels(req *Request, value interface{}) (hypermedia.Relations, *Response)

Rels attempts to get the cached relations for the given request. If it hasn't been cached, send a GET to the request URL, decode the response body to the given value, and get the relations from the value.

func (*Client) ResolveReference Uses

func (c *Client) ResolveReference(u *url.URL) *url.URL

ResolveReference resolves a URI reference to an absolute URI from an absolute base URI. It also merges the query values.

func (*Client) ResolveReferenceString Uses

func (c *Client) ResolveReferenceString(rawurl string) (string, error)

ResolveReference resolves a string URI reference to an absolute URI from an absolute base URI. It also merges the query values.

type Request Uses

type Request struct {
    Client    *http.Client
    MediaType *mediatype.MediaType
    Query     url.Values
    Cacher    Cacher
    *http.Request
}

Request is a wrapped net/http Request with a pointer to the net/http Client, MediaType, parsed URI query, and the configured Cacher. Requests are capable of returning a sawyer Response with Do() or the HTTP verb helpers (Get(), Head(), Post(), etc).

func (*Request) Delete Uses

func (r *Request) Delete() *Response

Delete is a helper method for Do().

func (*Request) Do Uses

func (r *Request) Do(method string) *Response

Do completes the HTTP request, returning a response. The Request's Cacher is used to return a cached response if available. Otherwise, the request goes through and fills the cache for future requests.

func (*Request) Get Uses

func (r *Request) Get() *Response

Get is a helper method for Do().

func (*Request) Head Uses

func (r *Request) Head() *Response

Head is a helper method for Do().

func (*Request) Options Uses

func (r *Request) Options() *Response

Options is a helper method for Do().

func (*Request) Patch Uses

func (r *Request) Patch() *Response

Patch is a helper method for Do().

func (*Request) Post Uses

func (r *Request) Post() *Response

Post is a helper method for Do().

func (*Request) Put Uses

func (r *Request) Put() *Response

Put is a helper method for Do().

func (*Request) SetBody Uses

func (r *Request) SetBody(mtype *mediatype.MediaType, resource interface{}) error

SetBody encodes and sets the proper headers for the request body from the given resource. The resource is encoded in-memory, so be careful about passing a massive object. You can set the ContentLength and Body properties manually.

type Response Uses

type Response struct {
    // ResponseError stores any errors made making the HTTP request.  If set, then
    // AnyError() and IsError() will return true, and Error() will delegate to it.
    ResponseError error
    MediaType     *mediatype.MediaType
    BodyClosed    bool
    Cacher        Cacher

    *http.Response
    // contains filtered or unexported fields
}

Response is a wrapped net/http Response with a pointer to the MediaType and the cacher. It also doubles as a possible error object.

func ResponseError Uses

func ResponseError(err error) *Response

ResponseError returns an empty Response with the ResponseError set from the given error.

func (*Response) AnyError Uses

func (r *Response) AnyError() bool

AnyError returns true if the HTTP request returned an error, or if the response status is not a 2xx code.

func (*Response) CacheRels Uses

func (r *Response) CacheRels(rels hypermedia.Relations)

CacheRels will set the given relations for this resource.

func (*Response) Decode Uses

func (r *Response) Decode(resource interface{}) error

Decode will decode the body into the given resource, and parse the hypermedia relations. This is meant to be called after an HTTP request, and will close the response body. The decoder is set from the response's MediaType.

func (*Response) DecodeFrom Uses

func (r *Response) DecodeFrom(resource interface{}, body io.Reader) error

DecodeFrom decodes the resource from the given io.Reader, using the decoder from the response's MediaType.

func (*Response) Error Uses

func (r *Response) Error() string

Error returns the ResponseError's error string if set, or an empty string.

func (*Response) HypermediaRels Uses

func (r *Response) HypermediaRels(rels hypermedia.Relations)

HypermediaRels implements the hypermedia.HypermediaResource interface. The relations are parsed from the Link header.

func (*Response) IsApiError Uses

func (r *Response) IsApiError() bool

IsApiError returns true if the response status is not a 2xx code.

func (*Response) IsError Uses

func (r *Response) IsError() bool

IsError returns true if the HTTP request returned an error.

func (*Response) Rels Uses

func (r *Response) Rels() (hypermedia.Relations, bool)

Rels returns the cached relations if they have been set.

Directories

PathSynopsis
httpcachePackage httpcache provides facilities for caching HTTP responses and hypermedia for REST resources.
hypermediaPackage hypermedia provides helpers for parsing hypermedia links in resources and expanding the links to make further requests.
mediatypePackage mediatype contains helpers for parsing media type strings.

Package sawyer imports 8 packages (graph) and is imported by 4 packages. Updated 2016-07-29. Refresh now. Tools for package owners.