Documentation
¶
Index ¶
- Constants
- func CloneRequestBody(r *http.Request) ([]byte, error)
- func IsSuccessResponse(resp *http.Response) bool
- type BodyMarshaler
- type BodyUnmarshaler
- type EnrichedError
- type HttpClient
- func (c *HttpClient) Clone() *HttpClient
- func (c *HttpClient) NewConnectRequest(ctx context.Context, path string) *Request
- func (c *HttpClient) NewDeleteRequest(ctx context.Context, path string) *Request
- func (c *HttpClient) NewGetRequest(ctx context.Context, path string) *Request
- func (c *HttpClient) NewHeadRequest(ctx context.Context, path string) *Request
- func (c *HttpClient) NewOptionsRequest(ctx context.Context, path string) *Request
- func (c *HttpClient) NewPatchRequest(ctx context.Context, path string, body interface{}) *Request
- func (c *HttpClient) NewPostRequest(ctx context.Context, path string, body interface{}) *Request
- func (c *HttpClient) NewPutRequest(ctx context.Context, path string, body interface{}) *Request
- func (c *HttpClient) NewRequest(ctx context.Context, method, path string, body interface{}) *Request
- func (c *HttpClient) NewTraceRequest(ctx context.Context, path string) *Request
- func (c *HttpClient) SetBodyMarshaler(marshaler BodyMarshaler) *HttpClient
- func (c *HttpClient) SetBodyUnmarshaler(unmarshaler BodyUnmarshaler) *HttpClient
- func (c *HttpClient) SetDumpOnError() *HttpClient
- func (c *HttpClient) SetHeader(key, value string) *HttpClient
- func (c *HttpClient) SetHeaders(headers map[string]string) *HttpClient
- func (c *HttpClient) SetOnRequestReady(onRequestReady OnRequestReadyHook) *HttpClient
- func (c *HttpClient) SetOnResponseReady(onResponseReady OnResponseReadyHook) *HttpClient
- func (c *HttpClient) SetStackTraceEnabled(enabled bool) *HttpClient
- func (c *HttpClient) SetTimeout(timeout time.Duration) *HttpClient
- type JSONBodyMarshaler
- type JSONBodyUnmarshaler
- type NoopBodyMarshaler
- type NoopBodyUnmarshaler
- type OnRequestReadyHook
- type OnResponseReadyHook
- type Request
- func (r *Request) Do() (*http.Response, error)
- func (r *Request) SetBodyMarshaler(marshaler BodyMarshaler) *Request
- func (r *Request) SetBodyUnmarshaler(unmarshaler BodyUnmarshaler) *Request
- func (r *Request) SetDumpOnError() *Request
- func (r *Request) SetHeader(key, value string) *Request
- func (r *Request) SetHeaders(headers map[string]string) *Request
- func (r *Request) SetOnRequestReady(onRequestReady OnRequestReadyHook) *Request
- func (r *Request) SetOnResponseReady(onResponseReady OnResponseReadyHook) *Request
- func (r *Request) SetStackTraceEnabled(enabled bool) *Request
- func (r *Request) WriteBodyTo(result interface{}) *Request
- type RequestOptions
- func (o *RequestOptions) Clone() *RequestOptions
- func (o *RequestOptions) SetBodyMarshaler(marshaler BodyMarshaler)
- func (o *RequestOptions) SetBodyUnmarshaler(unmarshaler BodyUnmarshaler)
- func (o *RequestOptions) SetDumpOnError()
- func (o *RequestOptions) SetHeader(key, value string)
- func (o *RequestOptions) SetHeaders(headers map[string]string)
- func (o *RequestOptions) SetOnRequestReady(onRequestReady OnRequestReadyHook)
- func (o *RequestOptions) SetOnResponseReady(onResponseReady OnResponseReadyHook)
- func (o *RequestOptions) SetStackTraceEnabled(enabled bool)
Constants ¶
const ( HeaderAccept = "Accept" HeaderAcceptEncoding = "Accept-Encoding" HeaderAcceptLanguage = "Accept-Language" HeaderAuthorization = "Authorization" HeaderCacheControl = "Cache-Control" HeaderContentLength = "Content-Length" HeaderContentType = "Content-Type" HeaderCookie = "Cookie" HeaderHost = "Host" HeaderOrigin = "Origin" HeaderReferer = "Referer" HeaderUserAgent = "User-Agent" HeaderSetCookie = "Set-Cookie" HeaderLocation = "Location" HeaderETag = "ETag" HeaderIfModifiedSince = "If-Modified-Since" HeaderIfNoneMatch = "If-None-Match" HeaderXRequestedWith = "X-Requested-With" HeaderXForwardedFor = "X-Forwarded-For" HeaderXFrameOptions = "X-Frame-Options" HeaderStrictTransportSec = "Strict-Transport-Security" )
Variables ¶
This section is empty.
Functions ¶
func IsSuccessResponse ¶
Types ¶
type BodyMarshaler ¶
type BodyMarshaler interface { Marshal(body interface{}, writer io.Writer) error OnRequestReady(req *http.Request) error }
BodyMarshaler is an interface for marshaling request bodies. Allows to configure the request body according to the marshaling type via the OnRequestReady method.
func NewJSONBodyMarshaler ¶
func NewJSONBodyMarshaler() BodyMarshaler
NewJSONBodyMarshaler creates a BodyMarshaler that marshals the body to JSON format. It automatically sets the Content-Type header to application/json. The body can be any type that is supported by the json.Marshal function. Marshaling is done using the json.NewEncoder function, that uses streaming encoding. A caveat is that a new line is added at the end of the body, which is a requirement for the JSON format (see https://go.dev/src/encoding/json/stream.go, (enc *Encoder) Encode method, line 221).
func NewNoopBodyMarshaler ¶
func NewNoopBodyMarshaler() BodyMarshaler
NewNoopBodyMarshaler creates a BodyMarshaler that does not modify the request body. It allows to create requests by passing the same type as the standard http.NewRequestWithContext accepts, with some additions for convenience. The modifications are: - Automatically converts string to strings.Reader if the body is a string.
type BodyUnmarshaler ¶
type BodyUnmarshaler interface { Unmarshal(result interface{}, reader io.Reader) error OnRequestReady(req *http.Request) error }
BodyUnmarshaler is an interface for unmarshalling response bodies. Allows to configure the request body according to the unmarshalling type via the OnRequestReady method.
func NewJSONBodyUnmarshaler ¶
func NewJSONBodyUnmarshaler() BodyUnmarshaler
NewJSONBodyUnmarshaler creates a BodyUnmarshaler that unmarshalls the response body as JSON format. It automatically sets the Accept header to application/json. Unmarshalling is done via the json.NewDecoder function, that uses streaming decoding.
func NewNoopBodyUnmarshaler ¶
func NewNoopBodyUnmarshaler() BodyUnmarshaler
NewNoopBodyUnmarshaler creates a BodyUnmarshaler that does not modify the response body. It simply writes the response body to the destination without any additional processing. Allowed result destinations are: - io.Writer - *[]byte - *string
type EnrichedError ¶
func (*EnrichedError) Error ¶
func (e *EnrichedError) Error() string
type HttpClient ¶
type HttpClient struct {
// contains filtered or unexported fields
}
func NewHttpClient ¶
func NewHttpClient() *HttpClient
NewHttpClient creates a new HttpClient with default settings. Default settings are: - Timeout: 20 seconds - BodyMarshaler: NoopBodyMarshaler - this marshaler does not modify the request body. Allows to create requests by passing the same type as the standard http.NewRequestWithContext accepts with some additions for convenience (see NewNoopBodyMarshaler). - BodyUnmarshaler: NoopBodyUnmarshaler - this unmarshaler does not modify the response body, just writes it to the destination with some added handling for convenience (see NewNoopBodyUnmarshaler).
func (*HttpClient) Clone ¶
func (c *HttpClient) Clone() *HttpClient
Clone creates a new HttpClient with the same settings as the original one. The cloned client can be modified independently without affecting the original client.
func (*HttpClient) NewConnectRequest ¶
func (c *HttpClient) NewConnectRequest(ctx context.Context, path string) *Request
func (*HttpClient) NewDeleteRequest ¶
func (c *HttpClient) NewDeleteRequest(ctx context.Context, path string) *Request
func (*HttpClient) NewGetRequest ¶
func (c *HttpClient) NewGetRequest(ctx context.Context, path string) *Request
func (*HttpClient) NewHeadRequest ¶
func (c *HttpClient) NewHeadRequest(ctx context.Context, path string) *Request
func (*HttpClient) NewOptionsRequest ¶
func (c *HttpClient) NewOptionsRequest(ctx context.Context, path string) *Request
func (*HttpClient) NewPatchRequest ¶
func (c *HttpClient) NewPatchRequest(ctx context.Context, path string, body interface{}) *Request
func (*HttpClient) NewPostRequest ¶
func (c *HttpClient) NewPostRequest(ctx context.Context, path string, body interface{}) *Request
func (*HttpClient) NewPutRequest ¶
func (c *HttpClient) NewPutRequest(ctx context.Context, path string, body interface{}) *Request
func (*HttpClient) NewRequest ¶
func (c *HttpClient) NewRequest(ctx context.Context, method, path string, body interface{}) *Request
NewRequest creates a new Request with the specified method, path, and body. It is mostly used internally. For convenience, you can use the NewGetRequest, NewPostRequest, etc. methods to create requests with common HTTP methods.
func (*HttpClient) NewTraceRequest ¶
func (c *HttpClient) NewTraceRequest(ctx context.Context, path string) *Request
func (*HttpClient) SetBodyMarshaler ¶
func (c *HttpClient) SetBodyMarshaler(marshaler BodyMarshaler) *HttpClient
SetBodyMarshaler sets the BodyMarshaler at the HttpClient level. This will affect all requests made with this client unless overridden at the request level.
func (*HttpClient) SetBodyUnmarshaler ¶
func (c *HttpClient) SetBodyUnmarshaler(unmarshaler BodyUnmarshaler) *HttpClient
SetBodyUnmarshaler sets the BodyUnmarshaler at the HttpClient level. This will affect all requests made with this client unless overridden at the request level.
func (*HttpClient) SetDumpOnError ¶
func (c *HttpClient) SetDumpOnError() *HttpClient
SetDumpOnError configures logging of the request, response and error when an error occurs. http.Request and http.Response bodies will be logged as well, if they are set. Original body passed by the caller code will be logged as well, if it is set. This method will also enable the StackTraceEnabled option, which will add a stack trace to the error if it occurs. This will affect all requests made with this client unless overridden at the request level.
func (*HttpClient) SetHeader ¶
func (c *HttpClient) SetHeader(key, value string) *HttpClient
SetHeader sets a single header at the HttpClient level. Headers merging and override precedence is the same as with SetHeaders.
func (*HttpClient) SetHeaders ¶
func (c *HttpClient) SetHeaders(headers map[string]string) *HttpClient
SetHeaders sets the headers at the HttpClient level. Headers will affect all requests made with this client. When headers are set at the request level, they will be merged with the ones set at the client level. Headers set at the request level will override the ones set at the client level for that specific request.
func (*HttpClient) SetOnRequestReady ¶
func (c *HttpClient) SetOnRequestReady(onRequestReady OnRequestReadyHook) *HttpClient
SetOnRequestReady sets a hook that will be called right after an http.Request is created and all headers and body are set. This hook will be called for all requests made with this client unless overridden at the request level.
func (*HttpClient) SetOnResponseReady ¶
func (c *HttpClient) SetOnResponseReady(onResponseReady OnResponseReadyHook) *HttpClient
SetOnResponseReady sets a hook that will be called right after the response is received and before it is processed. This hook will be called for all requests made with this client unless overridden at the request level.
func (*HttpClient) SetStackTraceEnabled ¶
func (c *HttpClient) SetStackTraceEnabled(enabled bool) *HttpClient
SetStackTraceEnabled enables or disables the stack trace in the error if it occurs. This will affect all requests made with this client unless overridden at the request level.
func (*HttpClient) SetTimeout ¶
func (c *HttpClient) SetTimeout(timeout time.Duration) *HttpClient
SetTimeout sets the timeout for the underlying http.Client. This timeout will apply to all requests made with this client.
type JSONBodyMarshaler ¶
type JSONBodyMarshaler struct{}
func (*JSONBodyMarshaler) Marshal ¶
func (m *JSONBodyMarshaler) Marshal(body interface{}, writer io.Writer) error
func (*JSONBodyMarshaler) OnRequestReady ¶
func (m *JSONBodyMarshaler) OnRequestReady(req *http.Request) error
type JSONBodyUnmarshaler ¶
type JSONBodyUnmarshaler struct{}
func (*JSONBodyUnmarshaler) OnRequestReady ¶
func (u *JSONBodyUnmarshaler) OnRequestReady(req *http.Request) error
type NoopBodyMarshaler ¶
type NoopBodyMarshaler struct{}
func (*NoopBodyMarshaler) Marshal ¶
func (m *NoopBodyMarshaler) Marshal(body interface{}, writer io.Writer) error
func (*NoopBodyMarshaler) OnRequestReady ¶
func (m *NoopBodyMarshaler) OnRequestReady(_ *http.Request) error
type NoopBodyUnmarshaler ¶
type NoopBodyUnmarshaler struct{}
func (*NoopBodyUnmarshaler) OnRequestReady ¶
func (u *NoopBodyUnmarshaler) OnRequestReady(_ *http.Request) error
type OnRequestReadyHook ¶
type OnResponseReadyHook ¶
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
func (*Request) SetBodyMarshaler ¶
func (r *Request) SetBodyMarshaler(marshaler BodyMarshaler) *Request
SetBodyMarshaler sets the BodyMarshaler at the request level. Does not affect the client.
func (*Request) SetBodyUnmarshaler ¶
func (r *Request) SetBodyUnmarshaler(unmarshaler BodyUnmarshaler) *Request
SetBodyUnmarshaler sets the BodyUnmarshaler at the request level. Does not affect the client.
func (*Request) SetDumpOnError ¶
SetDumpOnError configures logging of the request, response and error when an error occurs. http.Request and http.Response bodies will be logged as well, if they are set. Original body passed by the caller code will be logged as well, if it is set. This method will also enable the StackTraceEnabled option, which will add a stack trace to the error if it occurs.
func (*Request) SetHeader ¶
SetHeader sets a single header for the request. This will override header with the same name set at the client level but only for this request.
func (*Request) SetHeaders ¶
SetHeaders sets the headers for the request. This will override headers with the same name set at the client level but only for this request.
func (*Request) SetOnRequestReady ¶
func (r *Request) SetOnRequestReady(onRequestReady OnRequestReadyHook) *Request
SetOnRequestReady sets a hook that will be called right after an http.Request is created and all headers and body are set. This method will override any hooks set at the client level, without affecting the client, but only for this request.
func (*Request) SetOnResponseReady ¶
func (r *Request) SetOnResponseReady(onResponseReady OnResponseReadyHook) *Request
SetOnResponseReady sets a hook that will be called right after the response is received and before it is processed. This method will override any hooks set at the client level, without affecting the client, but only for this request.
func (*Request) SetStackTraceEnabled ¶
SetStackTraceEnabled enables or disables the stack trace in the error if it occurs.
func (*Request) WriteBodyTo ¶
WriteBodyTo sets the destination for unmarshalling the response body. This method will consume the response body and close it after reading. This is the recommended way to consume the response body as it prevents resource leaks, provides type safety and a unified way to work with body. In case this method in not used, the caller must close the response body manually after reading it to prevent resource leaks!
type RequestOptions ¶
type RequestOptions struct { BodyMarshaler BodyMarshaler BodyUnmarshaler BodyUnmarshaler Headers map[string]string OnRequestReady OnRequestReadyHook OnResponseReady OnResponseReadyHook OnErrorHooks []onErrorHook StackTraceEnabled bool }
func (*RequestOptions) Clone ¶
func (o *RequestOptions) Clone() *RequestOptions
func (*RequestOptions) SetBodyMarshaler ¶
func (o *RequestOptions) SetBodyMarshaler(marshaler BodyMarshaler)
func (*RequestOptions) SetBodyUnmarshaler ¶
func (o *RequestOptions) SetBodyUnmarshaler(unmarshaler BodyUnmarshaler)
func (*RequestOptions) SetDumpOnError ¶
func (o *RequestOptions) SetDumpOnError()
func (*RequestOptions) SetHeader ¶
func (o *RequestOptions) SetHeader(key, value string)
func (*RequestOptions) SetHeaders ¶
func (o *RequestOptions) SetHeaders(headers map[string]string)
func (*RequestOptions) SetOnRequestReady ¶
func (o *RequestOptions) SetOnRequestReady(onRequestReady OnRequestReadyHook)
func (*RequestOptions) SetOnResponseReady ¶
func (o *RequestOptions) SetOnResponseReady(onResponseReady OnResponseReadyHook)
func (*RequestOptions) SetStackTraceEnabled ¶
func (o *RequestOptions) SetStackTraceEnabled(enabled bool)