Documentation ¶
Index ¶
- Variables
- func Connect(uri string, options ...Option) error
- func Delete(uri string, options ...Option) error
- func Get(uri string, options ...Option) error
- func Head(uri string, options ...Option) error
- func Options(uri string, options ...Option) error
- func Patch(uri string, options ...Option) error
- func Post(uri string, options ...Option) error
- func Put(uri string, options ...Option) error
- func Trace(uri string, options ...Option) error
- type Client
- type Method
- type Option
- func WithBody(body RequestBody) Option
- func WithClient(client Client) Option
- func WithCtx(ctx context.Context) Option
- func WithExpectedCode(statusCode int) Option
- func WithHandlerFunc(f http.HandlerFunc) Option
- func WithHeader(key, value string) Option
- func WithLogger(logger *slog.Logger) Option
- func WithParam(key, value string) Option
- func WithReader(f ResponseReader) Option
- type Request
- type RequestBody
- type ResponseReader
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnexpectedCode = goerr.New("unexpected status code") ErrInvalidOption = goerr.New("invalid option") )
Functions ¶
Types ¶
type Option ¶
type Option func(req *Request)
func WithBody ¶
func WithBody(body RequestBody) Option
WithBody sets body of HTTP request as io.Reader.
func WithClient ¶
WithClient sets Client interface to handle http.Request.
func WithCtx ¶
WithCtx will set given context.Context to http.Request.
func WithExpectedCode ¶
WithExpectedCode sets expected status code. If returned status code from server is not matched with *statusCode*, Emit method will return ErrUnexpectedCode. Default value is 200 (HTTP OK). If you don't want to handle status code, set 0 and Emit method will return no error with any status code.
func WithHandlerFunc ¶
func WithHandlerFunc(f http.HandlerFunc) Option
WithHandlerFunc is a option for testing. The f http.HandlerFunc will be injected as Client and record response with httptest.ResponseRecorder.
func WithHeader ¶
WithHeader adds a pair of key and value as HTTP header. This option can be set multiply.
func WithLogger ¶
WithLogger sets *slog.Logger as logging interface.
func WithParam ¶
WithParam adds a pair of key and value for Query String Parameter. This method will not overwrite existing query string in original URL.
func WithReader ¶
func WithReader(f ResponseReader) Option
WithReader is a handler of response body. It sets a functions to be called with Body of http.Response. The function must call r.Close() to terminate communication explicitly.
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
type RequestBody ¶
func ByReader ¶
func ByReader(r io.Reader) RequestBody
func EncodeAsJSON ¶
func EncodeAsJSON(data any) RequestBody
EncodeAsJSON will send body as JSON format and add Content-Type header with "application/json" automatically if Content-Type does not exist.
func EncodeAsURL ¶
func EncodeAsURL(data map[string]any) RequestBody
EncodeAsURL will sends body as URL encoded format and add Content-Type header with "application/x-www-form-urlencoded" automatically if Content-Type does not exist. A value will be converted to string with %v of fmt.Sprintf.
type ResponseReader ¶
type ResponseReader func(r io.ReadCloser) error
func CopyStream ¶
func CopyStream(w io.Writer) ResponseReader
func DecodeAsJSON ¶
func DecodeAsJSON(out any) ResponseReader