httpx

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2019 License: Apache-2.0 Imports: 13 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"
	HeaderPassHeaders = "X-Custom-Headers"
)

Variables

View Source
var DefaultXClient = NewXClient()

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 {
	CookiesSupport
	CustomHeadersSupport
	JSONClient

	// Auth returns current state of authentication flag.
	Auth() bool
	// OnAuth disables request authentication.
	OffAuth() Client
	// OnAuth enables request authentication.
	OnAuth() Client
	// 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) Client
	// 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)
}

Client is a interface of extended http.Client which support signed request.

func GetClient

func GetClient() Client

func WithAuth

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

WithAuth returns default client with set auth data.

func WithCookies

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

WithCookies returns default client with cookies.

type CookiesSupport

type CookiesSupport interface {
	// DefaultCookies returns a client's default cookies.
	DefaultCookies() []*http.Cookie
	// SetCookies 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 XClient

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

func NewXClient

func NewXClient() *XClient

func SetTimeout

func SetTimeout(duration time.Duration) *XClient

SetTimeout updated `DefaultXClient` default timeout (15s).

func (*XClient) Auth

func (client *XClient) Auth() bool

Auth returns current state of authentication flag.

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) GetSignedWithHeaders

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

PostSignedWithHeaders create new signed GET request with headers

func (*XClient) OffAuth

func (client *XClient) OffAuth() Client

func (*XClient) OnAuth

func (client *XClient) OnAuth() Client

OnAuth enables request authentication.

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, bodyStruct 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, bodyStruct interface{}, headers Headers) (*http.Response, error)

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

func (*XClient) PostSignedWithHeaders

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

PostSignedWithHeaders create new POST signed request with headers

func (*XClient) PublicKey

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

PublicKey returns client public key.

func (*XClient) PutJSON

func (client *XClient) PutJSON(url string, bodyStruct 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, bodyStruct interface{}, headers Headers) (*http.Response, error)

RequestJSON creates and executes new request with JSON content type.

func (*XClient) Service

func (client *XClient) Service() string

Service returns auth service name.

func (*XClient) SetAuth

func (client *XClient) SetAuth(service string, kp crypto.KP) Client

SetAuth sets the auth credentials.

func (*XClient) SetDefaultCookies

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

SetCookies 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) SetHeader

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

SetHeader sets new default header to the client.

func (*XClient) SetLogger

func (client *XClient) SetLogger(l log.Entry)

SetLogger - Set logger to enable log requests

func (*XClient) SignRequest

func (client *XClient) 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 (*XClient) VerifyBody

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

VerifyBody checks the request body match with it hash.

func (*XClient) VerifyRequest

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

VerifyRequest checks the request auth headers.

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