Documentation ¶
Overview ¶
Copyright 2020 Nametag, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Index ¶
- func HandleJSONError(respBodyFactory func() error) func(r *http.Response) error
- func JSONHandler(f interface{}) http.Handler
- func SplitAuthorizationHeader(r *http.Request) (kind string, value string)
- func SplitHeaderOnSpace(h string) (prefix, value string)
- func TestRequest(ctx context.Context, vars ...string) *http.Request
- type AppendHeaderTransport
- type BasicAuthTransport
- type JSONClient
- func (c JSONClient) DoJSON(ctx context.Context, method string, uri string, request interface{}, ...) error
- func (c JSONClient) HandleResponse(resp *http.Response, responseBody interface{}) error
- func (c JSONClient) NewRequest(ctx context.Context, method string, uri string, requestBody interface{}) (*http.Request, error)
- type URLPrefixTransport
- type UserAgentTransport
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HandleJSONError ¶
HandleJSONError is a function that can be assigned to JSONClient.OnError to handle error bodies that are returned by an API
func JSONHandler ¶
JSONHandler returns an http handler that accepts JSON as input and emits JSON as output. The input and output are serialized.
Example usage:
mux.Post("/someendpoint", JSONHandler(func(r *http.Request, in InputType) (*OutputType, error) { /* implementation */ }))
InputType and OutputType must be structs.
The function must have one of the following signatures:
func(r *http.Request, in InputType) (*OutputType, error) func(r *http.Request, in *InputType) (*OutputType, error) func(r *http.Request) (*OutputType, error) func(r *http.Request, in InputType) (error) func(r *http.Request, in *InputType) (error) func(r *http.Request) (error) func(r *http.Request)
func SplitAuthorizationHeader ¶
SplitAuthorizationHeader splits the parts of an Authorization header of the form "Method Token OtherStuff"
func SplitHeaderOnSpace ¶
SplitHeaderOnSpace returns the prefix and remaining value of a header at the first set of consecutive whitespace.
Types ¶
type AppendHeaderTransport ¶
type AppendHeaderTransport struct { Next http.RoundTripper Header http.Header }
AppendHeaderTransport is an http.RoundTripper that adds fixed headers to a request
type BasicAuthTransport ¶
type BasicAuthTransport struct { Next http.RoundTripper Username string Password string }
BasicAuthTransport is an http.RoundTripper that adds a fixed basic auth to the request.
type JSONClient ¶
JSONClient extends http.Client which adds a method that makes handling JSON requests / responses easier.
Example:
type APIError struct { Code int `json:"code"` Message string `json:"message"` } func (e APIError) Error() string { return fmt.Sprintf("%s (%d)", e.Message, e.String) c := JSONClient{Client: http.DefaultClient} c.OnError = HandleJSONError(func() error { return &APIError{} }) req := struct { Foo string }{} resp := string { Bar string }{} if err := c.DoJSON(ctx, "POST", "https://api.example.com/foo", req, &resp); err != nil { return err }
func (JSONClient) DoJSON ¶
func (c JSONClient) DoJSON(ctx context.Context, method string, uri string, request interface{}, response interface{}) error
DoJSON performs an HTTP request. If request is provided, it marshalled into the request body. If response is provided, the response body is marshalled into it. If the server returns an error, then the response is passed through OnError, or if OnError is not provided, an httperr.Response error is returned.
func (JSONClient) HandleResponse ¶
func (c JSONClient) HandleResponse(resp *http.Response, responseBody interface{}) error
HandleResponse handles an HTTP response containing a JSON object. If responseBody is provided, then the response is unmarshalled into it. If the HTTP status code is >= 400, then OnError is invoked if provided, otherwise an httperr.Response error is returned.
func (JSONClient) NewRequest ¶
func (c JSONClient) NewRequest(ctx context.Context, method string, uri string, requestBody interface{}) (*http.Request, error)
NewRequest returns a new request having the requestBody as the HTTP request body serialized in JSON format.
type URLPrefixTransport ¶
type URLPrefixTransport struct { Next http.RoundTripper Server string }
URLPrefixTransport is an http.RoundTripper that prepends Server to each URL.
e.g.
transport := URLPrefixTransport{Next: http.DefaultTransport, Server: "https://example.com/api/v1"}
type UserAgentTransport ¶
type UserAgentTransport struct { Next http.RoundTripper UserAgent string }
UserAgentTransport is an http.RoundTripper that adds a fixed user agent to the request if it is not already set.