napping

package
v0.0.0-...-765978f Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2015 License: MIT Imports: 10 Imported by: 1

README

Napping: HTTP for Gophers

Package napping is a Go client library for interacting with RESTful APIs. Napping was inspired by Python's excellent Requests library.

Status

Drone Build Status Travis Build Status Coverage Status

Used by, and developed in conjunction with, Neoism.

Installation

Requirements

Napping requires Go 1.2 or later.

Development
go get github.com/jmcvetta/napping
Stable

Napping is versioned using gopkg.in.

Current release is v3.

go get gopkg.in/jmcvetta/napping.v3

Documentation

See GoDoc for automatically generated API documentation.

Check out github_auth_token for a working example showing how to retrieve an auth token from the Github API.

Production Note

If you decide to use Napping in a production system please let me know. All API changes will be made via Pull Request, so it's highly recommended you Watch the repo Issues. The API is fairly stable but there may be additions and small changes from time to time.

Contributing

Contributions in the form of Pull Requests or Issues are gladly accepted. Before submitting a Pull Request, please ensure your code passes all tests, and that your changes do not decrease test coverage. I.e. if you add new features, also add corresponding new tests.

When submitting an Issue, if possible please include a failing test case that demonstrates the problem.

License

This is Free Software, released under the terms of the GPL v3.

Documentation

Overview

Package napping is a client library for interacting with RESTful APIs.

Example:

type Foo struct {
	Bar string
}
type Spam struct {
	Eggs int
}
payload := Foo{
	Bar: "baz",
}
result := Spam{}
url := "http://foo.com/bar"
resp, err := napping.Post(url, &payload, &result, nil)
if err != nil {
	panic(err)
}
if resp.Status() == 200 {
	println(result.Eggs)
}

See the "examples" folder for a more complete example.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Params

type Params map[string]string

A Params is a map containing URL parameters.

func (Params) AsUrlValues

func (p Params) AsUrlValues() url.Values

type Request

type Request struct {
	Url     string      // Raw URL string
	Method  string      // HTTP method to use
	Params  *url.Values // URL query parameters
	Payload interface{} // Data to JSON-encode and POST

	// Can be set to true if Payload is of type *bytes.Buffer and client wants
	// to send it as-is
	RawPayload bool

	// Result is a pointer to a data structure.  On success (HTTP status < 300),
	// response from server is unmarshaled into Result.
	Result interface{}

	// CaptureResponseBody can be set to capture the response body for external use.
	CaptureResponseBody bool

	// ResponseBody exports the raw response body if CaptureResponseBody is true.
	ResponseBody *bytes.Buffer

	// Error is a pointer to a data structure.  On error (HTTP status >= 300),
	// response from server is unmarshaled into Error.
	Error interface{}

	// Optional
	Userinfo *url.Userinfo
	Header   *http.Header
	// contains filtered or unexported fields
}

A Request describes an HTTP request to be executed, data structures into which the result will be unmarshalled, and the server's response. By using a single object for both the request and the response we allow easy access to Result and Error objects without needing type assertions.

type Response

type Response Request

A Response is a Request object that has been executed.

func Delete

func Delete(url string, p *url.Values, result, errMsg interface{}) (*Response, error)

Delete sends a DELETE request.

func Get

func Get(url string, p *url.Values, result, errMsg interface{}) (*Response, error)

Get sends a GET request.

func Head(url string, result, errMsg interface{}) (*Response, error)

Head sends a HEAD request.

func Options

func Options(url string, result, errMsg interface{}) (*Response, error)

Options sends an OPTIONS request.

func Patch

func Patch(url string, payload, result, errMsg interface{}) (*Response, error)

Patch sends a PATCH request.

func Post

func Post(url string, payload, result, errMsg interface{}) (*Response, error)

Post sends a POST request.

func Put

func Put(url string, payload, result, errMsg interface{}) (*Response, error)

Put sends a PUT request.

func Send

func Send(r *Request) (*Response, error)

Send composes and sends and HTTP request.

func (*Response) HttpResponse

func (r *Response) HttpResponse() *http.Response

HttpResponse returns the underlying Response object from http package.

func (*Response) RawText

func (r *Response) RawText() string

RawText returns the body of the server's response as raw text.

func (*Response) Status

func (r *Response) Status() int

Status returns the HTTP status for the executed request, or 0 if request has not yet been sent.

func (*Response) Timestamp

func (r *Response) Timestamp() time.Time

Timestamp returns the time when HTTP request was sent.

func (*Response) Unmarshal

func (r *Response) Unmarshal(v interface{}) error

Unmarshal parses the JSON-encoded data in the server's response, and stores the result in the value pointed to by v.

type Session

type Session struct {
	Client *http.Client
	Log    bool // Log request and response

	// Optional
	Userinfo *url.Userinfo

	// Optional defaults - can be overridden in a Request
	Header *http.Header
	Params *url.Values
}

func (*Session) Delete

func (s *Session) Delete(url string, p *url.Values, result, errMsg interface{}) (*Response, error)

Delete sends a DELETE request.

func (*Session) Get

func (s *Session) Get(url string, p *url.Values, result, errMsg interface{}) (*Response, error)

Get sends a GET request.

func (*Session) Head

func (s *Session) Head(url string, result, errMsg interface{}) (*Response, error)

Head sends a HEAD request.

func (*Session) Options

func (s *Session) Options(url string, result, errMsg interface{}) (*Response, error)

Options sends an OPTIONS request.

func (*Session) Patch

func (s *Session) Patch(url string, payload, result, errMsg interface{}) (*Response, error)

Patch sends a PATCH request.

func (*Session) Post

func (s *Session) Post(url string, payload, result, errMsg interface{}) (*Response, error)

Post sends a POST request.

func (*Session) Put

func (s *Session) Put(url string, payload, result, errMsg interface{}) (*Response, error)

Put sends a PUT request.

func (*Session) Send

func (s *Session) Send(r *Request) (response *Response, err error)

Send constructs and sends an HTTP request.

Directories

Path Synopsis
examples
github_auth_token
Example demonstrating use of package napping, with HTTP Basic authentictation over HTTPS, to retrieve a Github auth token.
Example demonstrating use of package napping, with HTTP Basic authentictation over HTTPS, to retrieve a Github auth token.

Jump to

Keyboard shortcuts

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