rest

package
v0.0.0-...-05adf4c Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2014 License: BSD-2-Clause Imports: 17 Imported by: 0

Documentation

Overview

Utility package for Go that makes working with web services even easier.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidPrefix is returned when attemping to create a New() client with
	// an invalid prefix.
	ErrInvalidPrefix = errors.New(`Expecting a valid canonical URL (http://...).`)

	// ErrCouldNotCreateMultipart is returned when attemping to create a
	// multipart request with a nil body.
	ErrCouldNotCreateMultipart = errors.New(`Couldn't create a multipart request without a body.`)

	// ErrCouldNotConvert is returned when the request response can't be
	// converted to the expected datatype.
	ErrCouldNotConvert = errors.New(`Could not convert response %s to %s.`)

	// ErrDestinationNotAPointer is returned when attemping to provide a
	// destination that is not a pointer.
	ErrDestinationNotAPointer = errors.New(`Destination is not a pointer.`)
)
View Source
var DefaultClient = new(Client)

DefaulClient is the default client used on top level functions like rest.Get(), rest.Post(), rest.Delete() and rest.Put().

Functions

func Delete

func Delete(dest interface{}, uri string, data url.Values) error

Delete performs a HTTP DELETE request using the default client and, when complete, attempts to convert the response body into the datatype given by dst (a pointer to a struct, map or []byte array).

func Get

func Get(dest interface{}, uri string, data url.Values) error

Get performs a HTTP GET request using the default client and, when complete, attempts to convert the response body into the datatype given by dst (a pointer to a struct, map or []byte array).

func Post

func Post(dest interface{}, uri string, data url.Values) error

Post performs a HTTP POST request using the default client and, when complete, attempts to convert the response body into the datatype given by dst (a pointer to a struct, map or []byte array).

func PostMultipart

func PostMultipart(dest interface{}, uri string, data *MultipartBody) error

PostMultipart performs a HTTP POST multipart request using the default client and, when complete, attempts to convert the response body into the datatype given by dst (a pointer to a struct, map or []byte array).

func Put

func Put(dest interface{}, uri string, data url.Values) error

Put performs a HTTP PUT request using the default client and, when complete, attempts to convert the response body into the datatype given by dst (a pointer to a struct, map or []byte array).

func PutMultipart

func PutMultipart(dest interface{}, uri string, data *MultipartBody) error

PutMultipart performs a HTTP PUT multipart request using the default client and, when complete, attempts to convert the response body into the datatype given by dst (a pointer to a struct, map or []byte array).

Types

type Client

type Client struct {
	Header    http.Header
	Prefix    string
	CookieJar *cookiejar.Jar
}

Client is useful in case you need to communicate with an API and you'd like to use the same prefix for all of your requests or in scenarios where it would be handy to keep a session cookie.

func New

func New(prefix string) (*Client, error)

New creates a new client, in all following GET, POST, PUT and DELETE requests given paths will be prefixed with the given client's prefix value.

func (*Client) Delete

func (self *Client) Delete(dst interface{}, path string, data url.Values) error

Delete performs a HTTP DELETE request and, when complete, attempts to convert the response body into the datatype given by dst (a pointer to a struct, map or []byte array).

func (*Client) Get

func (self *Client) Get(dst interface{}, path string, data url.Values) error

Get performs a HTTP GET request and, when complete, attempts to convert the response body into the datatype given by dst (a pointer to a struct, map or []byte array).

func (*Client) GetHeader

func (client *Client) GetHeader(header_name string) string

Sets the request's basic authorization header to be used in all requests.

func (*Client) GetHeaders

func (client *Client) GetHeaders(path string) error

We don't need any GET vars We also don't care about the response body

func (*Client) Post

func (self *Client) Post(dst interface{}, path string, data url.Values) error

Post performs a HTTP POST request and, when complete, attempts to convert the response body into the datatype given by dst (a pointer to a struct, map or []byte array).

func (*Client) PostMultipart

func (self *Client) PostMultipart(dst interface{}, uri string, data *MultipartBody) error

PostMultipart performs a HTTP POST multipart request and, when complete, attempts to convert the response body into the datatype given by dst (a pointer to a struct, map or []byte array).

func (*Client) PostRaw

func (self *Client) PostRaw(dst interface{}, path string, body []byte) error

PostRaw performs a HTTP POST request with a custom body and, when complete, attempts to convert the response body into the datatype given by dst (a pointer to a struct, map or []byte array).

func (*Client) Put

func (self *Client) Put(dst interface{}, path string, data url.Values) error

Put performs a HTTP PUT request and, when complete, attempts to convert the response body into the datatype given by dst (a pointer to a struct, map or []byte array).

func (*Client) PutMultipart

func (self *Client) PutMultipart(dst interface{}, uri string, data *MultipartBody) error

PutMultipart performs a HTTP PUT multipart request and, when complete, attempts to convert the response body into the datatype given by dst (a pointer to a struct, map or []byte array).

func (*Client) SetBasicAuth

func (self *Client) SetBasicAuth(username string, password string)

Sets the request's basic authorization header to be used in all requests.

type File

type File struct {
	Name string
	io.Reader
}

File can be used to represent a file that you'll later upload within a multipart request.

type MultipartBody

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

MultipartBody struct for multipart requests, you can't generate a MultipartBody directly, use rest.NewMultipartBody() instead.

func NewMultipartBody

func NewMultipartBody(params url.Values, filemap map[string][]File) (*MultipartBody, error)

NewMultipartBody creates a *MultipartBody based on the given parameters. This is useful for PostMultipart() and PutMultipart().

type Response

type Response struct {
	Status        string
	StatusCode    int
	Proto         string
	ProtoMajor    int
	ProtoMinor    int
	ContentLength int64
	http.Header
	Body []byte
}

Response can be used as a response value, useful when you need to work with headers, status codes and such.

Jump to

Keyboard shortcuts

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