Documentation
¶
Overview ¶
package rest is a small package for sending requests to a RESTful API and unmarshaling the response. It compiles to javascript via gopherjs and is intended to run in the browser.
Rest sends requests using CRUD semantics. It supports requests with a Content-Type of either application/x-www-form-urlencoded or application/json and parses json responses from the server.
Version 0.2.0
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// ContentType is used to determine the Content-Type header and encoding
// the the client will use when sending requests. By default, the value
// is ContentURLEncoded, which corresponds to the Content-Type header
// "application/x-www-form-urlencoded". To send requests encoded as JSON,
// you can set this to ContentJSON, which corresponds to the Content-Type
// header "application/json".
ContentType ContentType
}
A client is capable of sending RESTful requests to some server and unmarshalling the response into an arbitrary struct type.
func NewClient ¶
func NewClient() *Client
NewClient returns a new client with all the default settings.
func (*Client) Create ¶
Create sends an http request to create the given model. It uses reflection to convert the fields of model to url-encoded data. Then it sends a POST request to model.RootURL() with the encoded data in the body and the appropriate Content-Type header. It expects a JSON response containing the created object from the server if the request was successful, in which case it will mutate model by setting the fields to the values in the JSON response. Since model may be mutated, it should be a pointer.
func (*Client) Delete ¶
Delete sends an http request to delete an existing model. It sends a DELETE request to model.RootURL() + "/" + model.ModelId(). DELETE will not do anything with the response from the server and will not mutate model.
func (*Client) Read ¶
Read sends an http request to read (or fetch) the model with the given id from the server. It sends a GET request to model.RootURL() + "/" + model.ModelId(). Read expects a JSON response containing the data for the requested model if the request was successful, in which case it will mutate model by setting the fields to the values in the JSON response. Since model may be mutated, it should be a pointer.
func (*Client) ReadAll ¶
ReadAll sends an http request to get all the models of a particular type from the server (e.g. get all the todos). It sends a GET request to model.RootURL(). ReadAll expects a JSON response containing an array of objects, where each object contains data for one model. models must be a pointer to a slice of some type which implements Model. ReadAll will mutate models by growing or shrinking the slice as needed, and by setting the fields of each element to the values in the JSON response.
func (*Client) Update ¶
Update sends an http request to update an existing model, i.e. to change some or all of the fields. It uses reflection to convert the fields of model to the proper encoding. Then it sends a PATCH request to model.RootURL() with the encoded data in the body and the appropriate Content-Type header. Update expects a JSON response containing the data for the updated model if the request was successful, in which case it will mutate model by setting the fields to the values in the JSON response. Since model may be mutated, it should be a pointer.
type ContentType ¶
type ContentType string
ContentType represents a Content-Type header.
const ( ContentJSON ContentType = "application/json" ContentURLEncoded ContentType = "application/x-www-form-urlencoded" )
type DefaultId ¶
type DefaultId struct {
Id string
}
DefaultId is a struct with an Id property and a getter called ModelId. You can embed it to satisfy the ModelId method of rest.Model.
type HTTPError ¶
type HTTPError struct {
// URL is the url that the request was sent to
URL string
// Body is the body of the response
Body []byte
// StatusCode is the http status code of the response
StatusCode int
}
HTTPError is returned whenever rest gets a non-2xx response from the server.
type Model ¶
type Model interface {
// ModelId returns a unique identifier for the model. It is used for determining
// which URL to send a request to.
ModelId() string
// RootURL returns the url for the REST resource corresponding to this model.
// If you want to send requests to the same server, it should look something
// like "/todos". If you want to send requests to a different server, you can
// include the entire domain in the url, e.g. "http://example.com/todos". Note
// that the trailing slash should not be included.
RootURL() string
}
Model must be satisfied by all models. Satisfying this interface allows you to use the helper methods which send http requests to a REST API. They are used for e.g., creating a new model or getting an existing model from the server. Because of the way reflection is used to encode the data, a Model must have an underlying type of a struct, and all fields you wish to be included in requests and responses must be exported.