rest

package
v0.0.0-...-8be0c7b Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2019 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Sending request:

client := NewClient()
var foo = struct {
	Bar string
}{}
var apiErr = struct {
	Message string
}{}
resp, err := client.Do(request, &foo, &apiErr)

Index

Constants

View Source
const (
	//ErrCodeEmptyResponse ...
	ErrCodeEmptyResponse = "EmptyResponseBody"
)

Variables

View Source
var ErrEmptyResponseBody = bmxerror.New(ErrCodeEmptyResponse, "empty response body")

ErrEmptyResponseBody ...

Functions

This section is empty.

Types

type Client

type Client struct {
	// The HTTP client to be used. Default is HTTP's defaultClient.
	HTTPClient *http.Client
	// Defaualt header for all outgoing HTTP requests.
	DefaultHeader http.Header
}

Client is a REST client. It's recommend that a client be created with the NewClient() method.

func NewClient

func NewClient() *Client

NewClient creates a new REST client.

func (*Client) Do

func (c *Client) Do(r *Request, respV interface{}, errV interface{}) (*http.Response, error)

Do sends an request and returns an HTTP response. The resp.Body will be consumed and closed in the method.

For 2XX response, it will be JSON decoded into the value pointed to by respv.

For non-2XX response, an attempt will be made to unmarshal the response into the value pointed to by errV. If unmarshal failed, an ErrorResponse error with status code and response text is returned.

type File

type File struct {
	// File name
	Name string
	// File content
	Content io.Reader
	// Mime type, defaults to "application/octet-stream"
	Type string
}

File represents a file upload in the POST request

type Request

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

Request is a REST request. It also acts like a HTTP request builder.

func DeleteRequest

func DeleteRequest(rawUrl string) *Request

DeleteRequest creates a REST request with DELETE method and the given rawUrl.

func GetRequest

func GetRequest(rawUrl string) *Request

GetRequest creates a REST request with GET method and the given rawUrl.

func HeadRequest

func HeadRequest(rawUrl string) *Request

HeadRequest creates a REST request with HEAD method and the given rawUrl.

func NewRequest

func NewRequest(rawUrl string) *Request

NewRequest creates a new REST request with the given rawUrl.

func OptionsRequest

func OptionsRequest(rawUrl string) *Request

Creates a request with HTTP OPTIONS.

func PatchRequest

func PatchRequest(rawUrl string) *Request

PatchRequest creates a REST request with PATCH method and the given rawUrl.

func PostRequest

func PostRequest(rawUrl string) *Request

PostRequest creates a REST request with POST method and the given rawUrl.

func PutRequest

func PutRequest(rawUrl string) *Request

PutRequest creates a REST request with PUT method and the given rawUrl.

func (*Request) Add

func (r *Request) Add(key string, value string) *Request

Add adds the key, value pair to the request header. It appends to any existing values associated with key.

func (*Request) Body

func (r *Request) Body(body interface{}) *Request

Body sets the request body. Accepted types are string, []byte, io.Reader, or structs to be JSON encodeded.

func (*Request) Build

func (r *Request) Build() (*http.Request, error)

Build builds a HTTP request according to the settings in the REST request.

func (*Request) Del

func (r *Request) Del(key string) *Request

Del deletes the header as specified by the key.

func (*Request) Field

func (r *Request) Field(key string, value string) *Request

Field appends the key, value pair to the form fields in the POST request.

func (*Request) File

func (r *Request) File(name string, file File) *Request

File appends a file upload item in the POST request. The file content will be consumed when building HTTP request (see Build()) and closed if it's also a ReadCloser type.

func (*Request) Method

func (r *Request) Method(method string) *Request

Method sets HTTP method of the request.

func (*Request) Query

func (r *Request) Query(key string, value string) *Request

Query appends the key, value pair to the request query which will be encoded as url query parameters on HTTP request's url.

func (*Request) Set

func (r *Request) Set(key string, value string) *Request

Set sets the header entries associated with key to the single element value. It replaces any existing values associated with key.

Jump to

Keyboard shortcuts

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