client

package
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: MPL-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package client provides an HTTP client.

Index

Constants

View Source
const (
	DefaultUserAgent             = service.DefaultApplicationName
	DefaultTimeout               = 20 * time.Second
	DefaultKeepAlive             = 30 * time.Second
	DefaultTLSHandshakeTimeout   = 5 * time.Second
	DefaultExpectContinueTimeout = 1 * time.Second
	DefaultIdleConnectionTimeout = 60 * time.Second
	DefaultResponseHeaderTimeout = 5 * time.Second
	DefaultMaxIdleConnections    = 10
	DefaultMaxConnectionsPerHost = 10
	DefaultWriteBufferSize       = 4 * 1024
	DefaultReadBufferSize        = 4 * 1024
	DefaultQPS                   = 10
	DefaultBurst                 = 100
)

Variables

This section is empty.

Functions

func Download added in v0.0.13

func Download(ctx context.Context, dsn types.URI, filename string) error

Download downloads a file.

Types

type CharsetRoundTripper

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

CharsetRoundTripper is a tripper that applies charset to response.

func (*CharsetRoundTripper) RoundTrip

func (t *CharsetRoundTripper) RoundTrip(req *http.Request) (response *http.Response, err error)

RoundTrip executes the HTTP request and applies charset to response.

type Client

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

Client is an HTTP client.

func NewClient

func NewClient(options *Options) *Client

NewClient creates a new client.

func (*Client) Do

func (c *Client) Do(ctx context.Context, endpoint *schema.Endpoint) (*Response, error)

Do sends an HTTP request and returns an HTTP response.

type EncoderRoundTripper

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

EncoderRoundTripper is a tripper that applies encoding to response.

func (*EncoderRoundTripper) RoundTrip

func (t *EncoderRoundTripper) RoundTrip(req *http.Request) (response *http.Response, err error)

RoundTrip executes the HTTP request and applies encoding to response.

type LimiterRoundTripper

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

LimiterRoundTripper is a tripper that applies rate limiting to requests.

func (*LimiterRoundTripper) RoundTrip

func (t *LimiterRoundTripper) RoundTrip(req *http.Request) (response *http.Response, err error)

RoundTrip executes the HTTP request and applies rate limiting.

type Option

type Option func(o *Options)

Option represents the service option.

func WithBaseUri added in v0.0.13

func WithBaseUri(value string) Option

WithBaseUri sets the dsn.

func WithBurst

func WithBurst(value int) Option

WithBurst sets the burst.

func WithExpectContinue

func WithExpectContinue(value time.Duration) Option

WithExpectContinue sets the expect continue.

func WithHeaders

func WithHeaders(value http.Header) Option

WithHeaders sets the headers.

func WithIdleConnection

func WithIdleConnection(value time.Duration) Option

WithIdleConnection sets the idle connection.

func WithKeepAlive

func WithKeepAlive(value time.Duration) Option

WithKeepAlive sets the keep alive.

func WithMaxConnectionsPerHost

func WithMaxConnectionsPerHost(value int) Option

WithMaxConnectionsPerHost sets the max connections per host.

func WithMaxIdleConnections

func WithMaxIdleConnections(value int) Option

WithMaxIdleConnections sets the max idle connections.

func WithProxyURL

func WithProxyURL(value string) Option

WithProxyURL sets the proxy url.

func WithQPS

func WithQPS(value float32) Option

WithQPS sets the qps.

func WithReadBufferSize

func WithReadBufferSize(value int) Option

WithReadBufferSize sets the read buffer size.

func WithResponseHeader

func WithResponseHeader(value time.Duration) Option

WithResponseHeader sets the response header.

func WithTLSHandshake

func WithTLSHandshake(value time.Duration) Option

WithTLSHandshake sets the tls handshake.

func WithTimeout

func WithTimeout(value time.Duration) Option

WithTimeout sets the timeout.

func WithWriteBufferSize

func WithWriteBufferSize(value int) Option

WithWriteBufferSize sets the write buffer size.

type Options

type Options struct {
	BaseUri               types.URI      `json:"base_uri"`
	Headers               nethttp.Header `json:"headers"`
	ProxyURL              types.URI      `json:"proxy_url"`
	MaxIdleConnections    int            `json:"max_idle_connections"`
	MaxConnectionsPerHost int            `json:"max_connections_per_host"`
	WriteBufferSize       int            `json:"write_buffer_size"`
	ReadBufferSize        int            `json:"read_buffer_size"`
	Timeout               time.Duration  `json:"timeout"`
	KeepAlive             time.Duration  `json:"keep_alive"`
	TLSHandshake          time.Duration  `json:"tls_handshake"`
	ExpectContinue        time.Duration  `json:"expect_continue"`
	IdleConnection        time.Duration  `json:"idle_connection"`
	ResponseHeader        time.Duration  `json:"response_header"`
	QPS                   float32        `json:"qps"`
	Burst                 int            `json:"burst"`
}

Options is an HTTP client options.

func NewOptions

func NewOptions(options ...Option) *Options

NewOptions creates a new options.

type Progress

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

Progress is a progress.

func NewProgress

func NewProgress(name string, total uint64) *Progress

NewProgress creates a new progress for a download.

func (*Progress) Start

func (p *Progress) Start(frequency time.Duration)

Start starts the progress.

func (*Progress) Stop

func (p *Progress) Stop()

Stop stops the progress.

func (*Progress) Write

func (p *Progress) Write(b []byte) (int, error)

Write writes the progress.

type Response

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

Response is an HTTP response.

func (*Response) Body

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

func (*Response) Close

func (r *Response) Close() error

Close closes the response body.

func (*Response) ContentLength

func (r *Response) ContentLength() int64

func (*Response) Cookies

func (r *Response) Cookies() []*nethttp.Cookie

func (*Response) Dump

func (r *Response) Dump(body bool) ([]byte, error)

Dump dumps the response.

func (*Response) Headers

func (r *Response) Headers() http.Headers

func (*Response) HtmlQuery

func (r *Response) HtmlQuery(expression string) []*html.Node

HtmlQuery returns the html.Node of the response body.

func (*Response) Save

func (r *Response) Save(filename string, progressWriter io.Writer) error

Save saves the response body to the given filename.

func (*Response) Status

func (r *Response) Status() http.Status

func (*Response) Unmarshal

func (r *Response) Unmarshal(v any) error

Unmarshal unmarshals the response body into the given value.

Jump to

Keyboard shortcuts

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