httpx

package module
v0.0.0-...-dbede5b Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

httpx

httpx is a library of useful HTTP helper functions for Go, especially when used with a Goji mux.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleJSONError

func HandleJSONError(respBodyFactory func() error) func(r *http.Response) error

HandleJSONError is a function that can be assigned to JSONClient.OnError to handle error bodies that are returned by an API

func JSONHandler

func JSONHandler(f interface{}) http.Handler

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

func SplitAuthorizationHeader(r *http.Request) (kind string, value string)

SplitAuthorizationHeader splits the parts of an Authorization header of the form "Method Token OtherStuff"

func SplitHeaderOnSpace

func SplitHeaderOnSpace(h string) (prefix, value string)

SplitHeaderOnSpace returns the prefix and remaining value of a header at the first set of consecutive whitespace.

func TestRequest

func TestRequest(ctx context.Context, vars ...string) *http.Request

TestRequest returns a request suitable for use with an httpx.JSONHandler, optionally injecting goji variables

Types

type AppendHeaderTransport

type AppendHeaderTransport struct {
	Next   http.RoundTripper
	Header http.Header
}

AppendHeaderTransport is an http.RoundTripper that adds fixed headers to a request

func (AppendHeaderTransport) RoundTrip

func (t AppendHeaderTransport) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

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.

func (BasicAuthTransport) RoundTrip

func (t BasicAuthTransport) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

type JSONClient

type JSONClient struct {
	*http.Client
	OnError func(r *http.Response) error
}

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"}

func (URLPrefixTransport) RoundTrip

func (t URLPrefixTransport) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

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.

func (UserAgentTransport) RoundTrip

func (t UserAgentTransport) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

Directories

Path Synopsis
Package httperr implements an error object that speaks HTTP.
Package httperr implements an error object that speaks HTTP.

Jump to

Keyboard shortcuts

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