Documentation
¶
Index ¶
- Constants
- Variables
- func ConvertRequestBody(contentType string, body interface{}) (data []byte, err error)
- func Delete(url string, options RequestOptions, v interface{}) error
- func Get(url string, options RequestOptions, v interface{}) error
- func ParseResponse(contentType string, body io.Reader, v interface{}) error
- func Post(url string, options RequestOptions, body, v interface{}) error
- func Put(url string, options RequestOptions, body, v interface{}) error
- func RegisterRequestParser(contentType string, parser RequestParser)
- func RegisterResponseParser(contentType string, parser ResponseParser)
- type Client
- func (c *Client) Delete(url string, options RequestOptions, v interface{}) error
- func (c *Client) Get(url string, options RequestOptions, v interface{}) error
- func (c *Client) Post(url string, options RequestOptions, body interface{}, v interface{}) error
- func (c *Client) Put(url string, options RequestOptions, body interface{}, v interface{}) error
- type Header
- type JsonRequestParser
- type JsonResponseParser
- type QueryParam
- type RequestOptions
- type RequestParser
- type ResponseParser
Constants ¶
const ApplicationJsonContentType = "application/json"
const ContentType = "Content-Type"
Variables ¶
var ( // A precreated client. Uses the http.Default client // All direct yahc.Get/Post/Put/Delete/Patch methods uses this // client. // // If another client should be used instead, request a custom // client instead using yahc.NewClient DefaultClient = NewClient(http.DefaultClient) // A default set of options // Will treat everything as json NoOptions = RequestOptions{ Headers: HeadersFromMap(map[string]string{ContentType: ApplicationJsonContentType}), } )
Functions ¶
func ConvertRequestBody ¶
Convert the body into bytes Will attempt to convert into the requested content type If no matching parser is registered, will return an error
func Delete ¶
func Delete(url string, options RequestOptions, v interface{}) error
Does a delete request using the default client
func Get ¶
func Get(url string, options RequestOptions, v interface{}) error
Does a GET request using the default client
func ParseResponse ¶
Parses the response body into the v object v should be a pointer
func Post ¶
func Post(url string, options RequestOptions, body, v interface{}) error
Does a POST request using the default client
func Put ¶
func Put(url string, options RequestOptions, body, v interface{}) error
Does a PUT request using the default client
func RegisterRequestParser ¶
func RegisterRequestParser(contentType string, parser RequestParser)
Registers a new parser for the given contentType If a parser is already registered, it's overwritten
func RegisterResponseParser ¶
func RegisterResponseParser(contentType string, parser ResponseParser)
Registers a new parser for the given content type If a parser is already registered for the content type, it is overwritten
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
A client that can interact with external services
func NewClient ¶
Gets a new client, based on the given client use this if you want provide e.g. a cookie jar for interacting with stateful services
func (*Client) Delete ¶
func (c *Client) Delete(url string, options RequestOptions, v interface{}) error
Does a delete to the given resource
func (*Client) Get ¶
func (c *Client) Get(url string, options RequestOptions, v interface{}) error
Does a get request to the given resource v should be a pointer to a struct that should be filled with values
type Header ¶
Headers to be passed along to the http request
func HeadersFromMap ¶
Creates a new header object based on the given map
type JsonRequestParser ¶
type JsonRequestParser struct { }
func (JsonRequestParser) Convert ¶
func (JsonRequestParser) Convert(v interface{}) ([]byte, error)
type JsonResponseParser ¶
type JsonResponseParser struct { }
type QueryParam ¶
Query params to send a long with requests These will be added to the query params given in the url string
func (QueryParam) Add ¶
func (p QueryParam) Add(key, value string)
Adds a value to the given parameter
type RequestOptions ¶
type RequestOptions struct { // Headers to attach to the request Headers Header // Query parameters to add to the request QueryParams QueryParam }
Headers and query parameters for the request
type RequestParser ¶
type RequestParser interface { // Should convert the given argument into a byte array Convert(v interface{}) ([]byte, error) }
A request parser will take to the request body struct, and turn it into a byte array that can be passed along to the actual request
type ResponseParser ¶
type ResponseParser interface { // Should convert the r into v // Closing the reader is handled by yahc Convert(r io.Reader, v interface{}) error }
A ResponseParser should convert a response body into an object