Documentation
¶
Overview ¶
Package api is a helper that simplifies the process of REST APIs bindings creation in Go. Rather than composing URLs and HTTP requests by hand, one can use the api.Request method in order to automatically create such a request. The use case may be as following:
svc, _ := api.New("http://example.com") args := url.Values{} args.Set("filter", "1") args.Set("price", "200") req, _ := svc.Request(api.GET, "/categories/1", args) // URL is now http://example.com/categories/1?filter=1&price=200 var cli http.Client resp, err := cli.Do(req)
In the case of POST, the arguments will be presented in the Body of request:
req, _ := svc.Request(api.POST, "/categories/1", args) // URL is now http://example.com/categories/1 // Body is now filter=1&price=200 // Header is now has Content-Type: application/x-www-form-urlencoded var cli http.Client resp, err := cli.Do(req)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Api ¶
type Api struct { // BaseURI is the base URI of an API. BaseURI *url.URL // Header is a custom header that will be used for communtication with API (e.g. Authorization). Header http.Header }
Api represents a REST API connection.
func (*Api) Request ¶
func (a *Api) Request(method Method, resource string, args url.Values) (req *http.Request, err error)
Request creates an http request instance properly initialized with the given parameters. In a special case for the POST method it will create a body buffer, in other cases it will just store the parameters in the URL.
Click to show internal directories.
Click to hide internal directories.