cclient

package module
v0.0.0-...-cf7a88a Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: MIT Imports: 14 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultClientHeader = http.Header{
	"Accept":          {"*/*"},
	"Accept-Language": {"en-US,en;q=0.7"},
	"Cache-Control":   {"no-cache"},
	"Pragma":          {"no-cache"},
	"User-Agent":      {"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36"},

	fhttp.HeaderOrderKey: {
		"Accept",
		"Accept-Language",
		"User-Agent",
	},
}
View Source
var NilClient = errors.New("client is nil")
View Source
var NilClientConfig = errors.New("config is nil")

Functions

This section is empty.

Types

type APIClient

type APIClient interface {
	Client() Client
	Logger() *slog.Logger
	SetLogger(logger *slog.Logger)
	Log(ctx context.Context, params clog.Params)

	BaseURL() string
	SetBaseURL(baseURL string)
	DefaultHeader() http.Header
	SetDefaultHeader(defaultHeader http.Header)

	Request(ctx context.Context, method string, endpointURI string, urlParams URLParams, headers http.Header, bodyData interface{}) (response APIResponse, err error)
	RawRequest(ctx context.Context, method string, url string, headers http.Header, body io.Reader) (response APIResponse, err error)
}

func NewAPIClient

func NewAPIClient(logger *slog.Logger, client Client, baseURL string, defaultHeader http.Header) (APIClient, error)

type APIError

type APIError struct {
	Response *APIResponse `json:"response"`
	Code     int          `json:"code"`
	Header   http.Header  `json:"header"`
	Message  string       `json:"message"`
	// contains filtered or unexported fields
}

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) Unwrap

func (e *APIError) Unwrap() error

func (*APIError) Wrap

func (e *APIError) Wrap(err error)

type APIResponse

type APIResponse interface {
	Close() error

	Raw() *fhttp.Response
	Header() http.Header
	StatusCode() int

	Body() (body []byte, err error)
	Decode(result any) error
}

type BasicClient

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

func NewClient

func NewClient(opts ...ClientConfigOption) (*BasicClient, error)

func (*BasicClient) CookieJar

func (c *BasicClient) CookieJar() fhttp.CookieJar

func (*BasicClient) DefaultHeader

func (c *BasicClient) DefaultHeader() http.Header

func (*BasicClient) HTTPClient

func (c *BasicClient) HTTPClient() tlsclient.HttpClient

func (*BasicClient) Log

func (c *BasicClient) Log(ctx context.Context, params clog.Params)

func (*BasicClient) Logger

func (c *BasicClient) Logger() *slog.Logger

func (*BasicClient) Request

func (c *BasicClient) Request(ctx context.Context, method string, url string, body io.Reader, headers http.Header) (*fhttp.Response, error)

func (*BasicClient) SetCookieJar

func (c *BasicClient) SetCookieJar(jar fhttp.CookieJar)

func (*BasicClient) SetCookies

func (c *BasicClient) SetCookies(u *url.URL, cookies []*fhttp.Cookie)

func (*BasicClient) SetDefaultHeader

func (c *BasicClient) SetDefaultHeader(header http.Header)

func (*BasicClient) SetLogger

func (c *BasicClient) SetLogger(logger *slog.Logger)

type Client

type Client interface {
	HTTPClient() tlsclient.HttpClient

	DefaultHeader() http.Header
	SetDefaultHeader(header http.Header)
	Logger() *slog.Logger
	SetLogger(logger *slog.Logger)
	Log(ctx context.Context, params clog.Params)

	CookieJar() fhttp.CookieJar
	SetCookieJar(jar fhttp.CookieJar)
	SetCookies(u *url.URL, cookies []*fhttp.Cookie)

	Request(ctx context.Context, method string, url string, body io.Reader, headers http.Header) (*fhttp.Response, error)
}

type ClientConfigOption

type ClientConfigOption func(cfg *clientConfig)

func WithDefaultHeader

func WithDefaultHeader(defaultHeader http.Header) ClientConfigOption

func WithHTTPClient

func WithHTTPClient(httpClient tlsclient.HttpClient) ClientConfigOption

func WithHTTPClientOptions

func WithHTTPClientOptions(httpClientOptions []tlsclient.HttpClientOption) ClientConfigOption

func WithLogger

func WithLogger(logger *slog.Logger) ClientConfigOption

type URLParams

type URLParams map[string][]string

func (URLParams) Encode

func (u URLParams) Encode() string

func (URLParams) Get

func (u URLParams) Get(key string) (value string)

func (URLParams) Set

func (u URLParams) Set(key, value string)

func (URLParams) SetMulti

func (u URLParams) SetMulti(key string, values []string)

Jump to

Keyboard shortcuts

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