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
- Variables
- func IsJSONType(ct string) bool
- func IsXMLType(ct string) bool
- func NewLogTransport(rt nethttp.RoundTripper, options ...LogTransportOption) *logTransport
- func RestyLogRequestFormatter(flds ...zap.Field) string
- func RestyLogResponseFormatter(flds ...zap.Field) string
- type LogFunc
- type LogRequestPredicateFunc
- type LogResponsePredicateFunc
- type LogTransportOption
- func LogTransportLogRequestIfOption(logIf LogRequestPredicateFunc) LogTransportOption
- func LogTransportLogResponseIfOption(logIf LogResponsePredicateFunc) LogTransportOption
- func LogTransportMaxBodySizeOption(maxBodySize int) LogTransportOption
- func LogTransportRequestLogFuncOption(logF LogFunc) LogTransportOption
- func LogTransportResponseLogFuncOption(logF LogFunc) LogTransportOption
- func LogTransportWrapOption(rt nethttp.RoundTripper) LogTransportOption
- type ResponseWrapper
Constants ¶
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" )
const ( AuthTypeBasic = "Basic" AuthTypeBearer = "Bearer" AuthTypeMAC = "MAC" )
auth types ...
Variables ¶
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") )
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
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.
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.
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 ¶
IsJSONType method is to check JSON 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 returns a formatted string mimicking Resty's log output for requests.
func RestyLogResponseFormatter ¶
func RestyLogRequestFormatter returns a formatted string mimicking Resty's log output for responses.
Types ¶
type LogFunc ¶
A LogFunc receives a context and an array logging data for an HTTP request or response.
func RestyLogRequestWriter ¶
RestyLogRequestWriter writes a resty-formatted response string to w.
func RestyLogResponseWriter ¶
RestyLogResponseWriter writes a resty-formatted response string to w.
type LogRequestPredicateFunc ¶
A LogRequestPredicateFunc determines, whether a log entry is written for a request.
type LogResponsePredicateFunc ¶
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 ¶
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.