client

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: MIT Imports: 14 Imported by: 0

README

http-client

A small wrapper around the HTTP Client in Go.

The client is not necessarily feature complete and is designed to work with simple requests to an HTTP server.

It has two modes of operation:

  1. Normal HTTP method function calls
  2. A reusable client

In most cases, the HTTP method function calls are probably what you are looking for.

The reusable client is typically more useful in scenarios where you need to work with cookies, or in scenarios where it will be useful to keep track of past requests. An example could be for writing an API test that might require different headers/cookies to be set and used as part of the testing of each end point.

HTTP method calls use the http.DefaultClient, while the reusable client establishes its own internal *http.Client or you can configure your own *http.Client which can be used within the reusable client.

Documentation

Index

Constants

View Source
const (
	SchemeHTTP  string = "http://"
	SchemeHTTPS string = "https://"
	SchemeWS    string = "ws://"
	SchemeWSS   string = "wss://"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client represents an HTTP client.

func New

func New(options ...RequestOptions) *Client

New returns a reusable Client. It is possible to include a global RequestOptions which will be used on all subsequent requests.

func NewCustom

func NewCustom(client *http.Client, options ...RequestOptions) *Client

NewCustom returns a reusable client with a custom defined *http.Client This is useful in scenarios where you want to change any configurations for the http.Client

func (*Client) AddGlobalOptions added in v0.4.0

func (c *Client) AddGlobalOptions(options RequestOptions)

AddGlobalOptions adds the provided options to the client's global options

func (*Client) Clear

func (c *Client) Clear()

Clear clears any Responses that have already been made and kept.

func (*Client) CloneGlobalOptions

func (c *Client) CloneGlobalOptions() RequestOptions

CloneGlobalOptions clones the global RequestOptions of the client.

func (*Client) Connect

func (c *Client) Connect(url string, opt ...RequestOptions) (Response, error)

Connect performs an HTTP CONNECT to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func (*Client) Custom

func (c *Client) Custom(method string, url string, payload []byte, opt ...RequestOptions) (Response, error)

Custom performs a custom HTTP method to the specified URL with the given payload. It accepts the HTTP method as its first argument, the URL string as the second argument, the payload as the third argument, and optionally additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func (*Client) Delete

func (c *Client) Delete(url string, opt ...RequestOptions) (Response, error)

Delete performs an HTTP DELETE to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func (*Client) FormData added in v0.3.0

func (c *Client) FormData(url string, payload map[string]string, opt ...RequestOptions) (Response, error)

FormPost performs an HTTP POST as an x-www-form-urlencoded payload to the specified URL. It accepts the URL string as its first argument and a map[string]string the payload. The map is converted to a url.QueryEscaped k/v pair that is sent to the server. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func (*Client) Get

func (c *Client) Get(url string, opt ...RequestOptions) (Response, error)

Get performs an HTTP GET to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func (*Client) GetGlobalOptions

func (c *Client) GetGlobalOptions() RequestOptions

GetGlobalOptions returns the global RequestOptions of the client.

func (*Client) Head

func (c *Client) Head(url string, opt ...RequestOptions) (Response, error)

Head performs an HTTP HEAD to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func (*Client) Options

func (c *Client) Options(url string, opt ...RequestOptions) (Response, error)

Options performs an HTTP OPTIONS to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func (*Client) Patch

func (c *Client) Patch(url string, payload []byte, opt ...RequestOptions) (Response, error)

Patch performs an HTTP PATCH to the specified URL with the given payload. It accepts the URL string as its first argument and the payload as the second argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func (*Client) Post

func (c *Client) Post(url string, payload []byte, opt ...RequestOptions) (Response, error)

Post performs an HTTP POST to the specified URL with the given payload. It accepts the URL string as its first argument and the payload as the second argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func (*Client) Put

func (c *Client) Put(url string, payload []byte, opt ...RequestOptions) (Response, error)

Put performs an HTTP PUT to the specified URL with the given payload. It accepts the URL string as its first argument and the payload as the second argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func (*Client) Responses added in v0.2.0

func (c *Client) Responses() []Response

Responses returns a slice of responses made by this Client

func (*Client) Trace

func (c *Client) Trace(url string, opt ...RequestOptions) (Response, error)

Trace performs an HTTP TRACE to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func (*Client) UpdateGlobalOptions

func (c *Client) UpdateGlobalOptions(options RequestOptions)

UpdateGlobalOptions updates the global RequestOptions of the client.

type Header struct {
	Key   string
	Value string
}

type RequestOptions

type RequestOptions = request.Options

Alias to request.Options

type Response

type Response = response.Response

Alias to response.Response

func Connect

func Connect(url string, opt ...RequestOptions) (Response, error)

Connect performs an HTTP CONNECT to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func Custom

func Custom(method string, url string, payload []byte, opt ...RequestOptions) (Response, error)

Custom performs a custom HTTP method to the specified URL with the given payload. It accepts the HTTP method as its first argument, the URL string as the second argument, the payload as the third argument, and optionally additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func Delete

func Delete(url string, opt ...RequestOptions) (Response, error)

Delete performs an HTTP DELETE to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func FormPost added in v0.3.0

func FormPost(url string, payload map[string]string, opt ...RequestOptions) (Response, error)

FormPost performs an HTTP POST as an x-www-form-urlencoded payload to the specified URL. It accepts the URL string as its first argument and a map[string]string the payload. The map is converted to a url.QueryEscaped k/v pair that is sent to the server. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func Get

func Get(url string, opt ...RequestOptions) (Response, error)

Get performs an HTTP GET to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func Head(url string, opt ...RequestOptions) (Response, error)

Head performs an HTTP HEAD to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func Options

func Options(url string, opt ...RequestOptions) (Response, error)

Options performs an HTTP OPTIONS to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func Patch

func Patch(url string, payload []byte, opt ...RequestOptions) (Response, error)

Patch performs an HTTP PATCH to the specified URL with the given payload. It accepts the URL string as its first argument and the payload as the second argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func Post

func Post(url string, payload []byte, opt ...RequestOptions) (Response, error)

Post performs an HTTP POST to the specified URL with the given payload. It accepts the URL string as its first argument and the payload as the second argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func Put

func Put(url string, payload []byte, opt ...RequestOptions) (Response, error)

Put performs an HTTP PUT to the specified URL with the given payload. It accepts the URL string as its first argument and the payload as the second argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

func Trace

func Trace(url string, opt ...RequestOptions) (Response, error)

Trace performs an HTTP TRACE to the specified URL. It accepts the URL string as its first argument. Optionally, you can provide additional RequestOptions to customize the request. Returns the HTTP response and an error if any.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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