http

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

The http package contains convenience functions and some useful data definitions to support http-clients with additional capabilities, e.g., logging.

Index

Constants

View Source
const (
	// ContentTypePlain is the content type of a plain text response
	ContentTypePlain = "text/plain; charset=utf-8"
	// ContentTypeHTML is the content type of a HTML response
	ContentTypeHTML = "text/html; charset=utf-8"
	// ContentTypeJSON is the content type of a JSON response
	ContentTypeJSON = "application/json"
	// ContentTypeJWT is the content type of a JWT response
	ContentTypeJWT = "application/jwt"
	// ContentTypeXML is the content type of a XML response
	ContentTypeXML = "application/xml"
	// ContentTypeFormURLEncoded is the content type for form data sent in a single
	// block
	ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"
)
View Source
const (
	AuthTypeBasic  = "Basic"
	AuthTypeBearer = "Bearer"
	AuthTypeMAC    = "MAC"
)

auth types ...

Variables

View Source
var (
	// HeaderKeyAccept is the canonical "Accept" HTTP header key string
	HeaderKeyAccept = nethttp.CanonicalHeaderKey("Accept")
	// HeaderKeyAcceptEncoding is the canonical "Accept-Encoding" HTTP header key string
	HeaderKeyAcceptEncoding = nethttp.CanonicalHeaderKey("Accept-Encoding")
	// HeaderKeyAllow is the canonical "Allow" HTTP header key string
	//lint:ignore U1000 for future use
	HeaderKeyAllow = nethttp.CanonicalHeaderKey("Allow")
	// HeaderKeyAllowHeaders is the canonical "Access-Control-Allow-Headers" HTTP header key string
	//lint:ignore U1000 for future use
	HeaderKeyAllowHeaders = nethttp.CanonicalHeaderKey("Access-Control-Allow-Headers")
	// HeaderKeyAllowMethods is the canonical "Access-Control-Allow-Methods" HTTP header key string
	//lint:ignore U1000 for future use
	HeaderKeyAllowMethods = nethttp.CanonicalHeaderKey("Access-Control-Allow-Methods")
	// HeaderKeyAllowOrigin is the canonical "Access-Control-Allow-Origin" HTTP header key string
	//lint:ignore U1000 for future use
	HeaderKeyAllowOrigin = nethttp.CanonicalHeaderKey("Access-Control-Allow-Origin")
	// HeaderKeyAuthorization is the canonical "Authorization" HTTP header key string
	HeaderKeyAuthorization = nethttp.CanonicalHeaderKey("Authorization")
	// HeaderKeyContentLength is the canonical "Content-Length" HTTP header key string
	HeaderKeyContentLength = nethttp.CanonicalHeaderKey("Content-Length")
	// HeaderKeyContentType is the canonical "Content-Type" HTTP header key string
	HeaderKeyContentType = nethttp.CanonicalHeaderKey("Content-Type")
	// HeaderKeyCookie is the canonical "Cookie" HTTP header key string
	HeaderKeyCookie = nethttp.CanonicalHeaderKey("Cookie")
	// HeaderKeyCSRFToken is the canonical "X-CSRF-Token" HTTP header key string
	HeaderKeyCSRFToken = nethttp.CanonicalHeaderKey("X-CSRF-Token")
	// HeaderKeyLocation is the canonical "Location" HTTP header key string
	HeaderKeyLocation = nethttp.CanonicalHeaderKey("Location")
	// HeaderKeyOrigin is the canonical "Origin" HTTP header key string
	HeaderKeyOrigin = nethttp.CanonicalHeaderKey("Origin")
	// HeaderKeySetCookie is the canonical "Set-Cookie" HTTP header key string
	HeaderKeySetCookie = nethttp.CanonicalHeaderKey("Set-Cookie")
)
View Source
var (
	ErrResponseIsEmpty                = errors.New("HTTP response is empty")
	ErrResponseBodyReadingFailed      = errors.New("reading HTTP response failed")
	ErrResponseJSONUnmarshalingFailed = errors.New("JSON unmarshaling of HTTP response failed")
)

HTTP errors

View Source
var (
	LogKeyBody     = "_body"
	LogKeyDuration = "_duration"
	LogKeyHost     = "_host"
	LogKeyMethod   = "_method"
	LogKeyProto    = "_proto"
	LogKeyStatus   = "_status"
	LogKeyURI      = "_uri"

	LogKeyGroupHeaders = "_headers"
)

These log-field keys can be overridden to accommodate customized structured log-entries.

View Source
var LogTransportLogRequestIfContextScopeHTTPRequestOption = func(t *logTransport) {
	t.logRequestIf = func(ctx context.Context, req *nethttp.Request) bool {
		return kbase.BitFieldContains(klog.ContextLogConfigScope(ctx), klog.ContextScopeHTTPRequest)
	}
}

LogTransportLogRequestIfContextScopeHTTPRequestOption enables writing of a request log-entries if klog.ContextScopeHTTPRequest is set in a request's context.

View Source
var LogTransportLogResponseIfContextScopeHTTPResponseOption = func(t *logTransport) {
	t.logResponseIf = func(ctx context.Context, req *nethttp.Response) bool {
		return kbase.BitFieldContains(klog.ContextLogConfigScope(ctx), klog.ContextScopeHTTPResponse)
	}
}

ClientLogRequestIfContextScopeHTTPResponseOption enables writing of a response log-entries if klog.ContextScopeHTTPResponse is set in a request's context.

Functions

func IsJSONType

func IsJSONType(ct string) bool

IsJSONType method is to check JSON content type or not

func IsXMLType

func IsXMLType(ct string) bool

IsXMLType method is to check XML content type or not

func NewLogTransport

func NewLogTransport(rt nethttp.RoundTripper, options ...LogTransportOption) *logTransport

NewLogTransport wraps rt to add the capability of logging HTTP-requests and/or responses. If rt is nil, http.DefaultTransport is wrapped instead.

func RestyLogRequestFormatter

func RestyLogRequestFormatter(flds ...zap.Field) string

func RestyLogRequestFormatter returns a formatted string mimicking Resty's log output for requests.

func RestyLogResponseFormatter

func RestyLogResponseFormatter(flds ...zap.Field) string

func RestyLogRequestFormatter returns a formatted string mimicking Resty's log output for responses.

Types

type LogFunc

type LogFunc = func(context.Context, ...zap.Field)

A LogFunc receives a context and an array logging data for an HTTP request or response.

func RestyLogRequestWriter

func RestyLogRequestWriter(w io.Writer) LogFunc

RestyLogRequestWriter writes a resty-formatted response string to w.

func RestyLogResponseWriter

func RestyLogResponseWriter(w io.Writer) LogFunc

RestyLogResponseWriter writes a resty-formatted response string to w.

type LogRequestPredicateFunc

type LogRequestPredicateFunc = func(context.Context, *nethttp.Request) bool

A LogRequestPredicateFunc determines, whether a log entry is written for a request.

type LogResponsePredicateFunc

type LogResponsePredicateFunc = func(context.Context, *nethttp.Response) bool

A LogResponsePredicateFunc determines, whether a log entry is written for a response.

type LogTransportOption

type LogTransportOption func(*logTransport)

A LogTransportOption allows for a granular configuration of an HTTP client.

func LogTransportLogRequestIfOption

func LogTransportLogRequestIfOption(logIf LogRequestPredicateFunc) LogTransportOption

LogTransportLogRequestIfOption determines, whether a log entry for a request is written,

func LogTransportLogResponseIfOption

func LogTransportLogResponseIfOption(logIf LogResponsePredicateFunc) LogTransportOption

LogTransportLogResponseIfOption determines, whether a log entry for a request is written,

func LogTransportMaxBodySizeOption

func LogTransportMaxBodySizeOption(maxBodySize int) LogTransportOption

LogTransportMaxBodySizeOption sets the maximal number of bytes for a request or response body to be logged.

func LogTransportRequestLogFuncOption

func LogTransportRequestLogFuncOption(logF LogFunc) LogTransportOption

LogTransportRequestLogFuncOption adds logF as a request logging function.

func LogTransportResponseLogFuncOption

func LogTransportResponseLogFuncOption(logF LogFunc) LogTransportOption

LogTransportResponseLogFuncOption adds logF as a response logging function.

func LogTransportWrapOption

func LogTransportWrapOption(rt nethttp.RoundTripper) LogTransportOption

LogTransportWrapOption sets the RoundTripper to be wrapped. Otherwise DefaultTransport is used.

type ResponseWrapper

type ResponseWrapper http.Response

ResponseWrapper ...

func (*ResponseWrapper) CheckError

func (rw *ResponseWrapper) CheckError(errorObject error) error

CheckError ...

func (*ResponseWrapper) Code

func (rw *ResponseWrapper) Code() int

Code method returns the HTTP status code for the executed request.

Example: 200

func (*ResponseWrapper) IsError

func (rw *ResponseWrapper) IsError() bool

IsError method returns true if HTTP status `code >= 400` otherwise false.

func (*ResponseWrapper) IsSuccess

func (rw *ResponseWrapper) IsSuccess() bool

IsSuccess method returns true if HTTP status `code >= 200 and <= 299` otherwise false.

Jump to

Keyboard shortcuts

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