httputil

package
v0.0.0-...-58998d9 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2019 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Overview

Package httputil provides utilities on top of the net/http package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckOKResponse

func CheckOKResponse(r *http.Response) error

CheckOKResponse checks if the status code is within 2XX range

func CheckResponse

func CheckResponse(r *http.Response) error

CheckResponse checks the API response for errors, and returns them if present. A response is considered an error if it has a status code outside the 200 range or equal to 202 Accepted. API error responses are expected to have either no response body, or a JSON response body that maps to ErrorResponse. Any other response body will be silently ignored.

The error type will be *TwoFactorAuthError for two-factor authentication errors.

func EncodeJSONResponse

func EncodeJSONResponse(encodeErr httptransport.ErrorEncoder) httptransport.EncodeResponseFunc

EncodeJSONResponse encodes a response using the appropriate serializer function

func JSONErrorEncoder

func JSONErrorEncoder(statusCoder func(err error) int) httptransport.ErrorEncoder

JSONErrorEncoder takes in a status coder and returns an HTTP error encoder

func NewServer

func NewServer(addr string, h http.Handler, opts ...Option) *http.Server

NewServer creates an HTTP Server with pre-configured timeouts and a secure TLS Config.

Types

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware is a chainable decorator for HTTP Handlers.

func Chain

func Chain(outer Middleware, others ...Middleware) Middleware

Chain is a helper function for composing middlewares. Requests will traverse them in the order they're declared. That is, the first middleware is treated as the outermost middleware.

Chain is identical to the go-kit helper for Endpoint Middleware.

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
)

func main() {
	h := Chain(
		annotate("one"),
		annotate("two"),
		annotate("three"),
	)(myHandler())

	srv := httptest.NewServer(h)
	defer srv.Close()

	if _, err := http.Get(srv.URL); err != nil {
		panic(err)
	}

}

func annotate(s string) Middleware {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			fmt.Println("annotate: ", s)
			next.ServeHTTP(w, r)
		})
	}
}

func myHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	})
}
Output:

annotate:  one
annotate:  two
annotate:  three

type Option

type Option func(*http.Server)

Option configures an HTTP Server.

type SerializerFunc

type SerializerFunc func(w http.ResponseWriter, v interface{}) error

SerializerFunc serializes a specific response

Jump to

Keyboard shortcuts

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