http

package
v0.0.0-...-13fb019 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2018 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Overview

The http package provides common HTTP client building blocks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HeaderPreservingClient

func HeaderPreservingClient() *http.Client

HeaderPreservingClient produces an http.Client with CheckRedirect set to:

1) Pass headers from the initial request to the new request 2) Return an error if 5 redirects fail to result in a non 3xx response

func MakeHeaderPreserving

func MakeHeaderPreserving(client *http.Client) *http.Client

MakeHeaderPreserving updates a specified http.Client to have a redirect policy that:

1) Passes headers from the initial requset to the redirected request 2) Returns an error if 5 redirects fail to result in a non 3xx response

Types

type Endpoint

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

Endpoint is a effectively a net/http.Request factory, which configures the common concerns of requests to a given HTTP service.

func NewEndpoint

func NewEndpoint(protocol Protocol, hostPort string) (Endpoint, error)

NewEndpoint constructs a new HTTP Endpoint. This is used to configure the HTTP service implementation.

Parameters:

protocol
  specifies whether we should be using HTTP or HTTPS there is currently no
  special configuration for HTTPS (certificate pinning, custom root CAs,
  etc.)
hostPort - host:port for the service we should connect to

Returns a new Endpoint object and an error if there was a problem. Currently the only error possible is the result of a failed call to url.Parse which will be passed directly to the caller.

The Endpoint object is configured with no custom headers (see Endpoint.AddHeader), and the net/http.Client created by HeaderPreservingClient. You may specify an alternate client via Endpoint.SetClient.

func (*Endpoint) AddHeader

func (e *Endpoint) AddHeader(header, value string)

AddHeader adds a header to be added to all requests created via NewRequest. These headers are meant to be constant across all requests (e.g. a client identifier). Headers specific to a particular request should be added directly to the net/http.Request.

func (*Endpoint) Client

func (e *Endpoint) Client() *http.Client

Client returns the net/http.Client for this Endpoint.

func (*Endpoint) Copy

func (e *Endpoint) Copy() Endpoint

Copy makes a copy of the Endpoint. Insures that modifications to custom headers of the new Endpoint are not made to the original Endpoint and vice versa.

func (*Endpoint) NewRequest

func (e *Endpoint) NewRequest(
	method string,
	path string,
	queryParams Params,
	body io.Reader,
) (*http.Request, error)

NewRequest constructs a net/http.Request for this turbine Endpoint with the given method, path, (optional) query parameters and (optional) body. Headers previously configured via AddHeader are added automatically.

func (*Endpoint) SetClient

func (e *Endpoint) SetClient(c *http.Client)

SetClient sets an alternative net/http.Client for this Endpoint.

func (*Endpoint) URL

func (e *Endpoint) URL(path string, queryParams Params) string

URL constructs a URL to this turbine Endpoint.

type FromFlags

type FromFlags interface {
	// Validates the command line flags for an Endpoint.
	Validate() error

	// Makes an Endpoint based on command line flags.
	MakeEndpoint() (Endpoint, error)
}

FromFlags constructs an Endpoint from command line flags.

func NewFromFlags

func NewFromFlags(defaultHostPort string, flagset tbnflag.FlagSet) FromFlags

type HasDetails

type HasDetails interface {
	GetPayload() interface{}
	GetDetails() interface{}
}

HasDetails can be implemented by objects passed to WriteEnvelope in order to pass some data back in parallel to the result of a called endpoint. For example, it can be used to pass pagination context without altering the payload.

type MockFromFlags

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

MockFromFlags is a mock of FromFlags interface

func NewMockFromFlags

func NewMockFromFlags(ctrl *gomock.Controller) *MockFromFlags

NewMockFromFlags creates a new mock instance

func (*MockFromFlags) EXPECT

EXPECT returns an object that allows the caller to indicate expected use

func (*MockFromFlags) MakeEndpoint

func (m *MockFromFlags) MakeEndpoint() (Endpoint, error)

MakeEndpoint mocks base method

func (*MockFromFlags) Validate

func (m *MockFromFlags) Validate() error

Validate mocks base method

type MockFromFlagsMockRecorder

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

MockFromFlagsMockRecorder is the mock recorder for MockFromFlags

func (*MockFromFlagsMockRecorder) MakeEndpoint

func (mr *MockFromFlagsMockRecorder) MakeEndpoint() *gomock.Call

MakeEndpoint indicates an expected call of MakeEndpoint

func (*MockFromFlagsMockRecorder) Validate

func (mr *MockFromFlagsMockRecorder) Validate() *gomock.Call

Validate indicates an expected call of Validate

type Params

type Params map[string]string

Params holds URL query arg name -> value mappings

type Protocol

type Protocol string

Protocol indicates which transport we should use for communicating with our service.

const (
	// HTTP is the Protocol constant for HTTP requests
	HTTP Protocol = "http"

	// HTTPS is the Protocol constant for HTTP requests
	HTTPS Protocol = "https"
)

type RequestHandler

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

func NewRequestHandler

func NewRequestHandler(client *http.Client) RequestHandler

func (RequestHandler) Do

func (rh RequestHandler) Do(
	mkReq func() (*http.Request, error),
	response interface{},
) error

Given a request and response container make the request and populate the response object. If the server returns an error (an encoded service.error) or there are problems decoding the response return an error.

type RichRequest

type RichRequest interface {
	// Examines a request's URL.Query() for a parameter matching the specified
	// name.  If the query arg has multiple specified values this will return
	// only the first value.
	//
	// Returns the value (or "") and a bool indicating if the variable was set.
	QueryArgOk(name string) (string, bool)

	// Returns the first value found for name (as QueryArgOk) or "" if no value
	// was found.
	QueryArg(name string) string

	// Returns the first value found in the URL (as QueryArgOk) or the defaultValue
	// if none was found.
	QueryArgOr(name, defaultValue string) string

	// Extract any body available in the request into a []byte. Returns a
	// service.http.error.Error if no body is available or if there are errors
	// reading the body.
	//
	// Important: Once read the body content will no longer be available.
	//
	// Important: This does not have any safety around the size of the memory
	// allocated for consumption of the request body.
	GetBody() ([]byte, error)

	// Consume the body and attempt to unmarshal it as JSON into an interface{}.
	// In addition to the error conditions of GetBody this will also return a
	// service.http.error.Error if there is an error returned from the unmarshal
	// process.
	//
	// Important: Once read the body content will no longer be available.
	//
	// Important: This does not have any safety around the size of the memory
	// allocated for consumption of the request body.
	GetBodyObject(resp interface{}) error

	// Access the underlying request that this is wrapping.
	Underlying() *http.Request
}

RichRequest provides easy access to the body, path args, and query args of an underlying net/http.Request

func NewRichRequest

func NewRichRequest(request *http.Request) RichRequest

type RichResponseWriter

type RichResponseWriter struct {
	http.ResponseWriter
}

func (RichResponseWriter) WriteEnvelope

func (rrw RichResponseWriter) WriteEnvelope(e error, result interface{}) int

Writes out an error / result pair to a waiting HTTP response. Parameter e will be forced into a httperr.Error. If it is already a httperr.Error or nil it will not be modified. If it is non-nil but also not already a httperr.Error it is encapsulated into an IntervalServerError with the error code 'UnknownUnclassifiedCode'.

If the processed e is non-nil the HTTP response code is drawn from the error otherwise it is 200.

The actual body of the response is the json marshaled envelope.Response object containing the processed e and the result.

Returns the HTTP result code that was sent.

Directories

Path Synopsis
Package envelope contains the Response envelope used by the Turbine Labs public API to encapsulate server behavior.
Package envelope contains the Response envelope used by the Turbine Labs public API to encapsulate server behavior.
Package error contains an error definition intended to be serialized and sent over the wire between the api-server and clients.
Package error contains an error definition intended to be serialized and sent over the wire between the api-server and clients.
Package header defines constants for HTTP headers used by the Turbine Labs API
Package header defines constants for HTTP headers used by the Turbine Labs API

Jump to

Keyboard shortcuts

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