httpdriver

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2021 License: ISC Imports: 5 Imported by: 1

Documentation

Overview

Package httpdriver provides interfaces and implementations of a simple HTTP client.

Index

Constants

View Source
const NoContent = 204

NoContent is the status code for HTTP 204, or http.StatusNoContent.

Variables

This section is empty.

Functions

func OptHeader

func OptHeader(from Response) http.Header

OptHeader returns the response header, or nil if from is nil.

Types

type Client

type Client interface {
	NewRequest(ctx context.Context, method, url string) (Request, error)
	Do(req Request) (Response, error)
}

Client is a generic interface used as an adapter allowing for custom HTTP client implementations, such as fasthttp.

func NewClient

func NewClient() Client

NewClient creates a new client around the standard library's http.Client. The client will have a timeout of 10 seconds.

func WrapClient

func WrapClient(client http.Client) Client

WrapClient wraps around the standard library's http.Client and returns an implementation that's compatible with the Client driver interface.

type DefaultClient

type DefaultClient http.Client

DefaultClient implements Client and wraps around the stdlib Client.

func (DefaultClient) Do

func (d DefaultClient) Do(req Request) (Response, error)

func (DefaultClient) NewRequest

func (d DefaultClient) NewRequest(ctx context.Context, method, url string) (Request, error)

type DefaultRequest

type DefaultRequest http.Request

DefaultRequest wraps around the stdlib Request and satisfies the Request interface.

func (*DefaultRequest) AddHeader

func (r *DefaultRequest) AddHeader(header http.Header)

func (*DefaultRequest) AddQuery

func (r *DefaultRequest) AddQuery(values url.Values)

func (*DefaultRequest) GetContext

func (r *DefaultRequest) GetContext() context.Context

func (*DefaultRequest) GetPath

func (r *DefaultRequest) GetPath() string

func (*DefaultRequest) WithBody

func (r *DefaultRequest) WithBody(body io.ReadCloser)

type DefaultResponse

type DefaultResponse http.Response

DefaultResponse wraps around the stdlib Response and satisfies the Response interface.

func (*DefaultResponse) GetBody

func (r *DefaultResponse) GetBody() io.ReadCloser

func (*DefaultResponse) GetHeader

func (r *DefaultResponse) GetHeader() http.Header

func (*DefaultResponse) GetStatus

func (r *DefaultResponse) GetStatus() int

type Request

type Request interface {
	// GetPath should return the URL path, for example "/endpoint/thing".
	GetPath() string
	// GetContext should return the same context that's passed into NewRequest.
	// For implementations that don't support this, it can remove a
	// context.Background().
	GetContext() context.Context
	// AddHeader appends headers.
	AddHeader(http.Header)
	// AddQuery appends URL query values.
	AddQuery(url.Values)
	// WithBody should automatically close the ReadCloser on finish. This is
	// similar to the stdlib's Request behavior.
	WithBody(io.ReadCloser)
}

Request is a generic interface for a normal HTTP request. It should be constructed using (Requester).NewRequest().

type Response

type Response interface {
	GetStatus() int
	GetHeader() http.Header
	// Body's ReadCloser will always be closed when done, unless DoContext()
	// returns an error.
	GetBody() io.ReadCloser
}

Response is returned from (Requester).DoContext().

Jump to

Keyboard shortcuts

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