Documentation ¶
Overview ¶
The http package provides common HTTP client building blocks.
Index ¶
- func HeaderPreservingClient() *http.Client
- func MakeHeaderPreserving(client *http.Client) *http.Client
- type Endpoint
- func (e *Endpoint) AddHeader(header, value string)
- func (e *Endpoint) Client() *http.Client
- func (e *Endpoint) Copy() Endpoint
- func (e *Endpoint) NewRequest(method string, path string, queryParams Params, body io.Reader) (*http.Request, error)
- func (e *Endpoint) SetClient(c *http.Client)
- func (e *Endpoint) URL(path string, queryParams Params) string
- type FromFlags
- type HasDetails
- type MockFromFlags
- type MockFromFlagsMockRecorder
- type Params
- type Protocol
- type RequestHandler
- type RichRequest
- type RichResponseWriter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HeaderPreservingClient ¶
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 ¶
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 ¶
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 ¶
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) Copy ¶
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.
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.
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 ¶
func (m *MockFromFlags) EXPECT() *MockFromFlagsMockRecorder
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 Protocol ¶
type Protocol string
Protocol indicates which transport we should use for communicating with our service.
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.
Source Files ¶
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 |