httpx

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2022 License: Apache-2.0 Imports: 11 Imported by: 4

Documentation

Index

Constants

View Source
const (
	HeaderBodyHash    = "X-Auth-BHash"
	HeaderSignature   = "X-Auth-Signature"
	HeaderSigner      = "X-Auth-Signer"
	HeaderService     = "X-Auth-Service"
	HeaderJWTParsed   = "jwt"
	HeaderHeadersList = "X-Custom-Headers"
)

Variables

This section is empty.

Functions

func DeleteJSON

func DeleteJSON(url string, headers map[string]string) (*http.Response, error)

DeleteJSON sets passed `headers` and executes RequestJSON with DELETE method.

func GetJSON

func GetJSON(url string, headers map[string]string) (*http.Response, error)

GetJSON sets passed `headers` and executes RequestJSON with GET method.

func ParseJSONBody

func ParseJSONBody(r *http.Request, dest interface{}) error

ParseJSONBody decodes `json` body from the `http.Request`.

func ParseJSONResult

func ParseJSONResult(httpResp *http.Response, dest interface{}) error

ParseJSONResult decodes `json` body from the `http.Response`.

func PostJSON

func PostJSON(url string, body interface{}, headers map[string]string) (*http.Response, error)

PostJSON sets passed `headers` and `body` and executes RequestJSON with POST method. Post issues a POST to the specified URL.

Caller should close resp.Body when done reading from it.

If the provided body is an io.Closer, it is closed after the request.

Post is a wrapper around DefaultClient.Post.

To set custom headers, use NewRequest and DefaultClient.Do.

See the Client.Do method documentation for details on how redirects are handled.

func PutJSON

func PutJSON(url string, body interface{}, headers map[string]string) (*http.Response, error)

PutJSON sets passed `headers` and `body` and executes RequestJSON with PUT method.

func RequestJSON

func RequestJSON(method string, url string, data interface{}, headers map[string]string) (*http.Response, error)

RequestJSON creates and executes new request with JSON content type.

Types

type Client

type Client interface {
	JSONClient
	CookiesSupport
	CustomHeadersSupport
	// Clone returns safe clone of Client.
	Clone() Client
	// SetHTTP - Set customized instance of http.Client
	SetHTTP(hc http.Client) Client
	// SetLogger - Set logger to enable log requests
	SetLogger(logger *logrus.Entry) Client
}

Client is an interface of extended http.Client.

func GetClient

func GetClient() Client

GetClient returns new Client.

func WithCookies

func WithCookies(cookies []*http.Cookie) Client

WithCookies returns default client with cookies.

func WithHeaders added in v1.8.0

func WithHeaders(headers Headers) Client

WithHeaders append headers to the client and return new instance.

type CookiesSupport

type CookiesSupport interface {
	// DefaultCookies returns a client's default cookies.
	DefaultCookies() []*http.Cookie
	// SetDefaultCookies sets a default cookies to a client.
	SetDefaultCookies(cookies []*http.Cookie) Client
	// RemoveDefaultCookies removes a default client's cookies.
	RemoveDefaultCookies() Client
	// WithCookies append cookies to a client and return new instance.
	WithCookies(cookies []*http.Cookie) Client
}

type CustomHeadersSupport

type CustomHeadersSupport interface {
	// DefaultHeaders returns a client's default headers.
	DefaultHeaders() Headers
	// SetDefaultHeaders sets a default headers to a client.
	SetDefaultHeaders(headers Headers) Client
	// SetHeader sets new default header to the client.
	SetHeader(key, val string) Client
	// RemoveDefaultHeaders removes a default client's headers.
	RemoveDefaultHeaders() Client
	// WithHeaders append headers to a client and return new instance.
	WithHeaders(headers Headers) Client
}

type Headers

type Headers map[string]string

Headers is a type for request headers.

type JSONClient

type JSONClient interface {
	// PostJSON sets passed `headers` and `body` and executes RequestJSON with POST method.
	PostJSON(url string, body interface{}, headers Headers) (*http.Response, error)
	// PatchJSON sets passed `headers` and `body` and executes RequestJSON with PATCH method.
	PatchJSON(url string, body interface{}, headers Headers) (*http.Response, error)
	// PutJSON sets passed `headers` and `body` and executes RequestJSON with PUT method.
	PutJSON(url string, body interface{}, headers Headers) (*http.Response, error)
	// GetJSON sets passed `headers` and executes RequestJSON with GET method.
	GetJSON(url string, headers Headers) (*http.Response, error)
	// DeleteJSON sets passed `headers` and executes RequestJSON with DELETE method.
	DeleteJSON(url string, headers Headers) (*http.Response, error)
	// RequestJSON creates and executes new request with JSON content type.
	RequestJSON(method string, url string, data interface{}, headers Headers) (*http.Response, error)
	// ParseJSONBody decodes `json` body from the `http.Request`.
	ParseJSONBody(r *http.Request, dest interface{}) error
	// ParseJSONResult decodes `json` body from the `http.Response`.
	ParseJSONResult(httpResp *http.Response, dest interface{}) error
}

type SXClient added in v1.8.0

type SXClient struct {
	XClient
	// contains filtered or unexported fields
}

SXClient implementation of the SecuredClient.

func NewSXClient added in v1.8.0

func NewSXClient() *SXClient

NewSXClient returns new SecuredClient.

func (*SXClient) Auth added in v1.8.0

func (client *SXClient) Auth() bool

Auth returns current state of authentication flag.

func (*SXClient) CloneWAuth added in v1.8.0

func (client *SXClient) CloneWAuth() SecuredClient

CloneWAuth returns a safe clone of SecuredClient.

func (*SXClient) DeleteJSON added in v1.8.0

func (client *SXClient) DeleteJSON(url string, headers Headers) (*http.Response, error)

DeleteJSON sets passed `headers` and executes RequestJSON with DELETE method.

func (*SXClient) GetJSON added in v1.8.0

func (client *SXClient) GetJSON(url string, headers Headers) (*http.Response, error)

GetJSON sets passed `headers` and executes RequestJSON with GET method.

func (*SXClient) GetSignedWithHeaders added in v1.8.0

func (client *SXClient) GetSignedWithHeaders(url string, headers map[string]string) (*http.Response, error)

GetSignedWithHeaders create new signed GET request with headers

func (*SXClient) OffAuth added in v1.8.0

func (client *SXClient) OffAuth() SecuredClient

func (*SXClient) OnAuth added in v1.8.0

func (client *SXClient) OnAuth() SecuredClient

OnAuth enables request authentication.

func (*SXClient) PatchJSON added in v1.8.0

func (client *SXClient) PatchJSON(url string, bodyStruct interface{}, headers Headers) (*http.Response, error)

PatchJSON sets passed `headers` and `body` and executes RequestJSON with PATCH method.

func (*SXClient) PostJSON added in v1.8.0

func (client *SXClient) PostJSON(url string, bodyStruct interface{}, headers Headers) (*http.Response, error)

PostJSON sets passed `headers` and `body` and executes RequestJSON with POST method.

func (*SXClient) PostSignedWithHeaders added in v1.8.0

func (client *SXClient) PostSignedWithHeaders(url string, data interface{},
	headers map[string]string) (*http.Response, error)

PostSignedWithHeaders create new POST signed request with headers

func (*SXClient) PublicKey added in v1.8.0

func (client *SXClient) PublicKey() crypto.Key

PublicKey returns client public key.

func (*SXClient) PutJSON added in v1.8.0

func (client *SXClient) PutJSON(url string, bodyStruct interface{}, headers Headers) (*http.Response, error)

PutJSON sets passed `headers` and `body` and executes RequestJSON with PUT method.

func (*SXClient) RequestJSON added in v1.8.0

func (client *SXClient) RequestJSON(method string, url string, bodyStruct interface{},
	headers Headers) (*http.Response, error)

RequestJSON creates and executes new request with JSON content type.

func (*SXClient) Service added in v1.8.0

func (client *SXClient) Service() string

Service returns auth service name.

func (*SXClient) SetAuth added in v1.8.0

func (client *SXClient) SetAuth(service string, kp crypto.KP) SecuredClient

SetAuth sets the auth credentials.

func (*SXClient) SignRequest added in v1.8.0

func (client *SXClient) SignRequest(req *http.Request, body []byte, headers map[string]string) (*http.Request, error)

SignRequest takes body hash, some headers and full URL path, sings this request details using the `client.privateKey` and adds the auth headers.

func (*SXClient) VerifyBody added in v1.8.0

func (client *SXClient) VerifyBody(r *http.Request, body []byte) (bool, error)

VerifyBody checks the request body match with it hash.

func (*SXClient) VerifyRequest added in v1.8.0

func (client *SXClient) VerifyRequest(r *http.Request, publicKey string) (bool, error)

VerifyRequest checks the request auth headers.

type SecuredClient added in v1.8.0

type SecuredClient interface {
	Client
	// CloneWAuth returns a safe clone of SecuredClient.
	CloneWAuth() SecuredClient

	// Auth returns current state of authentication flag.
	Auth() bool
	// OffAuth disables request authentication.
	OffAuth() SecuredClient
	// OnAuth enables request authentication.
	OnAuth() SecuredClient
	// PublicKey returns client public key.
	PublicKey() crypto.Key
	// Service returns auth service name.
	Service() string
	// SetAuth sets the auth credentials.
	SetAuth(service string, kp crypto.KP) SecuredClient
	// SignRequest takes body hash, some headers and full URL path,
	// sings this request details using the `client.privateKey` and adds the auth headers.
	SignRequest(req *http.Request, body []byte, headers map[string]string) (*http.Request, error)
	// VerifyBody checks the request body match with it hash.
	VerifyBody(r *http.Request, body []byte) (bool, error)
	// VerifyRequest checks the request auth headers.
	VerifyRequest(r *http.Request, publicKey string) (bool, error)
	// PostSignedWithHeaders signs request body and headers by set keys and sends it.
	PostSignedWithHeaders(url string, data interface{}, headers map[string]string) (*http.Response, error)
	// GetSignedWithHeaders signs request headers by set keys and sends it.
	GetSignedWithHeaders(url string, headers map[string]string) (*http.Response, error)
}

SecuredClient is an extension of the Client that adds possibility to sign and verify request.

func WithAuth

func WithAuth(service string, kp crypto.KP) SecuredClient

WithAuth returns default client with set auth data.

type XClient

type XClient struct {
	http.Client
	// contains filtered or unexported fields
}

XClient is an implementation of the Client.

func NewXClient

func NewXClient() *XClient

NewXClient returns new Client.

func (*XClient) Clone added in v1.8.0

func (client *XClient) Clone() Client

Clone returns safe clone of Client.

func (*XClient) DefaultCookies

func (client *XClient) DefaultCookies() []*http.Cookie

DefaultCookies returns a client's default cookies.

func (*XClient) DefaultHeaders

func (client *XClient) DefaultHeaders() Headers

DefaultHeaders returns a client's default headers.

func (*XClient) DeleteJSON

func (client *XClient) DeleteJSON(url string, headers Headers) (*http.Response, error)

DeleteJSON sets passed `headers` and executes RequestJSON with DELETE method.

func (*XClient) GetJSON

func (client *XClient) GetJSON(url string, headers Headers) (*http.Response, error)

GetJSON sets passed `headers` and executes RequestJSON with GET method.

func (*XClient) ParseJSONBody

func (client *XClient) ParseJSONBody(r *http.Request, dest interface{}) error

ParseJSONBody decodes `json` body from the `http.Request`. !> `dest` must be a pointer value.

func (*XClient) ParseJSONResult

func (client *XClient) ParseJSONResult(httpResp *http.Response, dest interface{}) error

ParseJSONResult decodes `json` body from the `http.Response` body into `dest` > `dest` must be a pointer value.

func (*XClient) PatchJSON

func (client *XClient) PatchJSON(url string, body interface{}, headers Headers) (*http.Response, error)

PatchJSON sets passed `headers` and `body` and executes RequestJSON with PATCH method.

func (*XClient) PostJSON

func (client *XClient) PostJSON(url string, body interface{}, headers Headers) (*http.Response, error)

PostJSON sets passed `headers` and `body` and executes RequestJSON with POST method.

func (*XClient) PutJSON

func (client *XClient) PutJSON(url string, body interface{}, headers Headers) (*http.Response, error)

PutJSON sets passed `headers` and `body` and executes RequestJSON with PUT method.

func (*XClient) RemoveDefaultCookies

func (client *XClient) RemoveDefaultCookies() Client

RemoveDefaultCookies removes a default client's cookies.

func (*XClient) RemoveDefaultHeaders

func (client *XClient) RemoveDefaultHeaders() Client

RemoveDefaultHeaders removes a default client's headers.

func (*XClient) RequestJSON

func (client *XClient) RequestJSON(method string, url string, body interface{},
	headers Headers) (*http.Response, error)

RequestJSON creates and executes new request with JSON content type.

func (*XClient) SetDefaultCookies

func (client *XClient) SetDefaultCookies(cookies []*http.Cookie) Client

SetDefaultCookies sets a default cookies to the client.

func (*XClient) SetDefaultHeaders

func (client *XClient) SetDefaultHeaders(headers Headers) Client

SetDefaultHeaders sets a default headers to the client.

func (*XClient) SetHTTP added in v1.8.0

func (client *XClient) SetHTTP(hc http.Client) Client

SetHTTP - Set customized instance of http.Client

func (*XClient) SetHeader

func (client *XClient) SetHeader(key, val string) Client

SetHeader sets new default header to the client.

func (*XClient) SetLogger

func (client *XClient) SetLogger(logger *logrus.Entry) Client

SetLogger - Set logger to enable log requests

func (*XClient) WithCookies

func (client *XClient) WithCookies(cookies []*http.Cookie) Client

WithCookies append cookies to the client and return new instance.

func (*XClient) WithHeaders

func (client *XClient) WithHeaders(headers Headers) Client

WithHeaders append headers to the client and return new instance.

Jump to

Keyboard shortcuts

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