http_client

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2023 License: MIT Imports: 10 Imported by: 2

README

http-client

http client helper

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckResponse

func CheckResponse(r *http.Response) error

func DoRequestWithClient

func DoRequestWithClient(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error)

func DumpInterceptor added in v0.2.0

func DumpInterceptor(req *http.Request, handler Handler) (*http.Response, error)

DumpInterceptor logs dump request and response

func Get added in v0.4.0

func Get(url string, out interface{}) error

func Post added in v0.4.0

func Post(url string, in, out interface{}) error

func ResponseInterceptor added in v0.2.0

func ResponseInterceptor(req *http.Request, handler Handler) (*http.Response, error)

ResponseInterceptor replaces 'NaN' with 'null' in Response.Body {"name":NaN} - incorrect json to {"name":null} - correct json

Types

type Client

type Client struct {
	BaseURL *url.URL
	Headers map[string]string
	// contains filtered or unexported fields
}

func New

func New(httpClient *http.Client, opts ...ClientOpt) (*Client, error)

func NewClient

func NewClient(httpClient *http.Client) *Client

func (*Client) AddInterceptor added in v0.2.0

func (c *Client) AddInterceptor(inter Interceptor) error

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*http.Response, error)

func (*Client) Get added in v0.4.0

func (c *Client) Get(url string, out interface{}) error

func (*Client) NewRequest

func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error)

func (*Client) Post added in v0.4.0

func (c *Client) Post(url string, in, out interface{}) error

func (*Client) SetAuthorization added in v0.4.0

func (c *Client) SetAuthorization(auth string)

func (*Client) SetHeader added in v0.5.0

func (c *Client) SetHeader(key, value string)

type ClientOpt

type ClientOpt func(*Client) error

func WithAuthorization added in v0.4.0

func WithAuthorization(auth string) ClientOpt

func WithBaseURL added in v0.4.0

func WithBaseURL(bu string) ClientOpt

func WithInterceptor added in v0.4.0

func WithInterceptor(inter Interceptor) ClientOpt

func WithUserAgent added in v0.4.0

func WithUserAgent(ua string) ClientOpt

type ErrorResponse

type ErrorResponse struct {
	Response  *http.Response
	Message   string
	RequestID string
}

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

type Handler added in v0.2.0

type Handler func(*http.Request) (*http.Response, error)

type Interceptor added in v0.2.0

type Interceptor func(*http.Request, Handler) (*http.Response, error)
var DefaultInterceptor Interceptor = func(req *http.Request, handler Handler) (*http.Response, error) {
	return handler(req)
}

Jump to

Keyboard shortcuts

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