cnet

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Backoff

func Backoff(operation func() (*Response, error), options ...Option) error

Backoff retries with increasing timeout duration up until X amount of retries (Default is 3 attempts, Override with option Retries(n))

func IntranetIP

func IntranetIP() string

func IsStringEmpty

func IsStringEmpty(str string) bool

func OutboundIP

func OutboundIP() string

func PublicIPs

func PublicIPs() (ips []string)

Types

type Client

type Client struct {
	BaseURL string
	Header  http.Header
	// contains filtered or unexported fields
}

func New

func New() *Client

func NewWithClient

func NewWithClient(hc *http.Client) *Client

func NewWithLocalAddr

func NewWithLocalAddr(localAddr net.Addr) *Client

NewWithLocalAddr method creates a new client with given Local Address to dial from.

func (*Client) R

func (c *Client) R() *Request

func (*Client) SetHeader

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

func (*Client) SetHeaders

func (c *Client) SetHeaders(headers map[string]string) *Client

func (*Client) SetTimeout

func (c *Client) SetTimeout(timeout time.Duration) *Client

type OnRetryFunc

type OnRetryFunc func(*Response, error)

OnRetryFunc is for side-effecting functions triggered on retry

type Option

type Option func(*Options)

Option is to create convenient retry options like wait time, max retries, etc.

func MaxWaitTime

func MaxWaitTime(value time.Duration) Option

MaxWaitTime sets the max wait time to sleep between requests

func Retries

func Retries(value int) Option

Retries sets the max number of retries

func RetryConditions

func RetryConditions(conditions []RetryConditionFunc) Option

RetryConditions sets the conditions that will be checked for retry.

func RetryHooks

func RetryHooks(hooks []OnRetryFunc) Option

RetryHooks sets the hooks that will be executed after each retry

func WaitTime

func WaitTime(value time.Duration) Option

WaitTime sets the default wait time to sleep between requests

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options struct is used to hold retry settings.

type Request

type Request struct {
	URL        string
	Method     string
	QueryParam url.Values
	FormData   url.Values
	Header     http.Header
	Time       time.Time
	Attempt    int
	RawRequest *http.Request
	// contains filtered or unexported fields
}

func (*Request) Execute

func (r *Request) Execute(method, url string) (resp *Response, err error)

Execute method performs the HTTP request with given HTTP method and URL for current `Request`.

func (*Request) ExecuteWithRetries

func (r *Request) ExecuteWithRetries(method, url string, options ...Option) (resp *Response, err error)

func (*Request) Get

func (r *Request) Get(url string) (*Response, error)

Get method does GET HTTP request. It's defined in section 4.3.1 of RFC7231.

func (*Request) GetWithRetries

func (r *Request) GetWithRetries(url string, options ...Option) (*Response, error)

func (*Request) Head

func (r *Request) Head(url string) (*Response, error)

Head method does HEAD HTTP request. It's defined in section 4.3.2 of RFC7231.

func (*Request) Send

func (r *Request) Send() (*Response, error)

func (*Request) SetContext

func (r *Request) SetContext(ctx context.Context) *Request

func (*Request) SetDoNotParseResponse

func (r *Request) SetDoNotParseResponse(parse bool) *Request

SetDoNotParseResponse

Note: Response middlewares are not applicable, if you use this option.

func (*Request) SetFormData

func (r *Request) SetFormData(data map[string]string) *Request

func (*Request) SetHeader

func (r *Request) SetHeader(header, value string) *Request

func (*Request) SetHeaders

func (r *Request) SetHeaders(headers map[string]string) *Request

func (*Request) SetQueryParam

func (r *Request) SetQueryParam(param, value string) *Request

SetQueryParam method sets single parameter and its value in the current request. It will be formed as query string for the request.

For Example: `search=kitchen%20papers&size=large` in the URL after `?` mark.

client.R().
	SetQueryParam("search", "kitchen papers").
	SetQueryParam("size", "large")

Also you can override query params value, which was set at client instance level.

func (*Request) SetQueryParams

func (r *Request) SetQueryParams(params map[string]string) *Request

type RequestMiddleware

type RequestMiddleware func(*Client, *Request) error

RequestMiddleware type is for request middleware, called before a request is sent

type Response

type Response struct {
	Request     *Request
	RawResponse *http.Response
	// contains filtered or unexported fields
}

Response struct holds response values of executed request.

func (*Response) Body

func (r *Response) Body() []byte

func (*Response) IsError

func (r *Response) IsError() bool

IsError method returns true if HTTP status `code >= 400` otherwise false.

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

IsSuccess method returns true if HTTP status `code >= 200 and <= 299` otherwise false.

func (*Response) RawBody

func (r *Response) RawBody() io.ReadCloser

RawBody method exposes the HTTP raw response body. Use this method in-conjunction with `SetDoNotParseResponse` option otherwise you get an error as `read err: http: read on closed response body`.

Do not forget to close the body, otherwise you might get into connection leaks, no connection reuse.

func (*Response) ReceivedAt

func (r *Response) ReceivedAt() time.Time

ReceivedAt method returns when response got received from server for the request.

func (*Response) StatusCode

func (r *Response) StatusCode() int

func (*Response) String

func (r *Response) String() string

type ResponseMiddleware

type ResponseMiddleware func(*Client, *Response) error

ResponseMiddleware type is for response middleware, called after a response has been received

type RetryConditionFunc

type RetryConditionFunc func(*Response, error) bool

RetryConditionFunc type is for retry condition function input: non-nil Response OR request execution error

Jump to

Keyboard shortcuts

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