httprutils

package
v0.0.0-...-5999da7 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2022 License: MIT Imports: 8 Imported by: 56

Documentation

Overview

The httputils package holds functions and structs for making RESTful API calls

Index

Constants

This section is empty.

Variables

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

DefaultClient is used if no custom HTTP client is defined http.Client configures a timeout that short-circuits long-running connections. The default for this value is 0, which is interpreted as “no timeout”, meaning if no custom client with a TimeOut value specified is defined, in case of API outage the GO program will continue to hang A custom TimeoutClient is defined below.

View Source
var JSONHeader = map[string]string{"content-Type": "application/json"}
View Source
var NetClient = &http.Client{
	Timeout:   time.Second * 8,
	Transport: tr,
}

Custom client with time out after 8 seconds

View Source
var TimeoutClient = &Client{HTTPClient: NetClient}
View Source
var URLEncodedHeader = map[string]string{"content-Type": "application/x-www-form-urlencoded"}

Functions

func AddQueryParams

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

AddQueryParams takes a map of query params and appends each to the URL

func BuildRequestObject

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

BuildRequestObject creates the HTTP request object.

func EncodeBody

func EncodeBody(body interface{}) (*bytes.Buffer, error)

EncodeBody takes an interface and returns a *bytes.Buffer suitable for making RESTful requests with

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
const (
	Get    Method = "GET"
	Post   Method = "POST"
	Put    Method = "PUT"
	Delete Method = "DELETE"
)

type Request

type Request struct {
	Method      Method
	URL         string
	Headers     map[string]string
	QueryParams map[string]string
	Body        *bytes.Buffer
}

Request holds the request to an API Call.

type Response

type Response struct {
	StatusCode int
	Body       string
	Headers    map[string][]string
	OrigBody   []byte
}

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)

Jump to

Keyboard shortcuts

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