api

package module
v0.0.0-...-b89d9e8 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2014 License: MIT Imports: 6 Imported by: 0

README

API

GoDoc

Installation:
go get github.com/xlab/api
Use case:
svc, _ := api.New("http://example.com")
args := url.Values{}
args.Set("filter", "1")
args.Set("price", "200")
req, _ := svc.Request(api.POST, "/categories/1", args)

// req.URL is now http://example.com/categories/1
// req.Body is now filter=1&price=200
// req.Header now has Content-Type: application/x-www-form-urlencoded

var cli http.Client
resp, err := cli.Do(req)
License

MIT

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 MustNew

func MustNew(uri string) *Api

MustNew is like New, but panics if any error has occured.

func New

func New(uri string) (a *Api, err error)

New creates a new api instance with given base uri.

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.

func (*Api) RequestBytes

func (a *Api) RequestBytes(method Method, resource string, contentType string, data []byte) (req *http.Request, err error)

type Method

type Method int

Method represents an HTTP method.

const (
	GET Method = iota
	POST
	HEAD
	PUT
	DELETE
	PATCH
)

func (Method) String

func (m Method) String() string

Jump to

Keyboard shortcuts

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