rest

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package rest allows for quick and easy access any REST or REST-like API.

Index

Constants

View Source
const Version = "2.6.0"

Version represents the current version of the rest library

Variables

View Source
var DefaultClient = &Client{HTTPClient: http.DefaultClient}

DefaultClient is used if no custom HTTP client is defined

Functions

func AddQueryParameters

func AddQueryParameters(baseURL string, queryParams map[string]string) string

AddQueryParameters adds query parameters to the URL.

func BuildRequestObject

func BuildRequestObject(request Request) (*http.Request, error)

BuildRequestObject creates the HTTP request object.

func MakeRequest

func MakeRequest(req *http.Request) (*http.Response, error)

MakeRequest makes the API call.

Types

type Client

type Client struct {
	HTTPClient *http.Client
}

Client allows modification of client headers, redirect policy and other settings See https://golang.org/pkg/net/http

func (*Client) MakeRequest

func (c *Client) MakeRequest(req *http.Request) (*http.Response, error)

MakeRequest makes the API call.

func (*Client) Send

func (c *Client) Send(request Request) (*Response, error)

Send will build your request, make the request, and build your response.

type Method

type Method string

Method contains the supported HTTP verbs.

const (
	Get    Method = "GET"
	Post   Method = "POST"
	Put    Method = "PUT"
	Patch  Method = "PATCH"
	Delete Method = "DELETE"
)

Supported HTTP verbs.

type Request

type Request struct {
	Method      Method
	BaseURL     string // e.g. https://api.sendgrid.com
	Headers     map[string]string
	QueryParams map[string]string
	Body        []byte
}

Request holds the request to an API Call.

type Response

type Response struct {
	StatusCode int                 // e.g. 200
	Body       string              // e.g. {"result: success"}
	Headers    map[string][]string // e.g. map[X-Ratelimit-Limit:[600]]
}

Response holds the response from an API call.

func BuildResponse

func BuildResponse(res *http.Response) (*Response, error)

BuildResponse builds the response struct.

func Send

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

Send uses the DefaultClient to send your request

type RestError

type RestError struct {
	Response *Response
}

RestError is a struct for an error handling.

func (*RestError) Error

func (e *RestError) Error() string

Error is the implementation of the error interface.

Jump to

Keyboard shortcuts

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