Documentation ¶
Overview ¶
Package httphelper contains net/http package helpers.
Index ¶
- Variables
- func AuthBasic(userid, password string) string
- func ErrorOptionWithMessage() func(o *ErrorOptions)
- func ErrorOptionWithObject() func(o *ErrorOptions)
- func IsNotStatusOK(rs *http.Response) bool
- func JsonBody(in any) (io.ReadCloser, error)
- func ReqBody[E any](client *http.Client, rq *http.Request, isError func(*http.Response) bool) ([]byte, error)
- func ReqJson[O, E any](client *http.Client, rq *http.Request, isError func(*http.Response) bool) (*O, error)
- type Error
- type ErrorOptions
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyResponseBody = errors.New("empty response body")
Functions ¶
func ErrorOptionWithMessage ¶ added in v0.12.0
func ErrorOptionWithMessage() func(o *ErrorOptions)
ErrorOptionWithMessage turns on Error's Message string deserialization from HTTP response body.
func ErrorOptionWithObject ¶ added in v0.5.0
func ErrorOptionWithObject() func(o *ErrorOptions)
ErrorOptionWithObject turns on Error's Object JSON deserialization from HTTP response body.
func IsNotStatusOK ¶ added in v0.12.0
IsNotStatusOK determines whether r.StatusCode is not http.StatusOK.
func JsonBody ¶ added in v0.10.0
func JsonBody(in any) (io.ReadCloser, error)
JsonBody creates http.Request.Body with JSON-encoded 'in', if not nil.
func ReqBody ¶ added in v0.14.0
func ReqBody[E any](client *http.Client, rq *http.Request, isError func(*http.Response) bool) ([]byte, error)
ReqBody sends an http.Request by http.Client and returns the contents of the response body.
If 'isError' is not nil and returns 'true', httphelper.Error is returned. httphelper.Error.Object of type 'E' is JSON-decoded from the response body. If response body does not contain JSON-encoded 'E', but contains a string, this string is returned in httphelper.Error.Message field.
Pass generichelper.NoType as 'E' to omit processing of httphelper.Error.Object.
func ReqJson ¶ added in v0.14.0
func ReqJson[O, E any](client *http.Client, rq *http.Request, isError func(*http.Response) bool) (*O, error)
ReqJson sends an http.Request by http.Client and returns output object of type 'O' passed JSON-encoded as the response body.
If 'isError' is not nil and returns 'true', httphelper.Error is returned. httphelper.Error.Object of type 'E' is JSON-decoded from the response body. If response body does not contain JSON-encoded 'E', but contains a string, this string is returned in httphelper.Error.Message field.
Pass generichelper.NoType as corresponding type argument to omit processing of either object.
Types ¶
type Error ¶ added in v0.5.0
type Error[T any] struct { StatusCode int `json:"status_code"` Status string `json:"status"` Object T `json:"object,omitempty"` Message string `json:"message,omitempty"` }
Error represents a HTTP error. Object (if turned on in options) is deserialized from JSON read from HTTP response body (if any).
func NewError ¶ added in v0.5.0
NewError creates Error from http.Response.
type ErrorOptions ¶ added in v0.5.0
type ErrorOptions struct {
// contains filtered or unexported fields
}
ErrorOptions contains Error options.