http_client

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2021 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 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
	UserAgent     string
	Authorization 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) NewRequest

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

type ClientOpt

type ClientOpt func(*Client) error

func SetAuthorization

func SetAuthorization(token, tokenType string) ClientOpt

func SetBaseURL

func SetBaseURL(bu string) ClientOpt

func SetInterceptor added in v0.2.0

func SetInterceptor(inter Interceptor) ClientOpt

func SetUserAgent

func SetUserAgent(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