hexahttp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2022 License: MIT Imports: 14 Imported by: 4

Documentation

Index

Constants

View Source
const (
	LogModeNone uint = 0

	LogModeRequestHeaders uint = 1 << iota
	LogModeRequestBody
	LogModeResponseHeaders
	LogModeResponseBody

	LogModeRequest  = LogModeRequestHeaders | LogModeRequestBody
	LogModeResponse = LogModeResponseHeaders | LogModeResponseBody
	LogModeAll      = LogModeRequest | LogModeResponse
)

Variables

This section is empty.

Functions

func Bytes

func Bytes(r *http.Response) ([]byte, error)

Bytes returns response's body bytes and close the response's body.

func Drain

func Drain(r *http.Response) error

Drain drains the response body and closes its connection.

func ResponseErr

func ResponseErr(r *http.Response) error

ResponseErr returns http error if the response is not successful. it drains the response.

func ResponseErrAndBytes

func ResponseErrAndBytes(r *http.Response) ([]byte, error)

ResponseErrAndBytes returns the response body byes and a http error if the response is not successful.

func ResponseErrBytes

func ResponseErrBytes(r *http.Response) ([]byte, error)

ResponseErrBytes returns the response body and its http error if the response isn't successful. It closes the response's body.

func ResponseErrOrBytes

func ResponseErrOrBytes(r *http.Response) ([]byte, error)

ResponseErrOrBytes checks if the response is successful, it returns the body bytes, otherwise the http error.

Types

type Client

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

Client is improved version of the net/http client

func NewClient

func NewClient(base string, logMode uint) (*Client, error)

func NewClientWithOptions

func NewClientWithOptions(cli *http.Client, base *URL, logMode uint) *Client

NewClientWithOptions creates a new HTTP client. base url could be nil. in that case all request urls must be absolute.

func (*Client) Do

func (c *Client) Do(req *http.Request, options ...RequestOption) (*http.Response, error)

func (*Client) Get

func (c *Client) Get(url string, options ...RequestOption) (*http.Response, error)

func (*Client) Head

func (c *Client) Head(url string, options ...RequestOption) (resp *http.Response, err error)

func (*Client) Post

func (c *Client) Post(url string, contentType string, body io.Reader, options ...RequestOption) (*http.Response, error)

func (*Client) PostForm

func (c *Client) PostForm(url string, data urlpkg.Values, options ...RequestOption) (*http.Response, error)

func (*Client) PostJsonForm

func (c *Client) PostJsonForm(urlPath string, data hexa.Map, options ...RequestOption) (*http.Response, error)

type HttpErr

type HttpErr struct {
	Code   int
	Status string
}

HttpErr represents a Http response error.

func (HttpErr) Error

func (err HttpErr) Error() string

type RequestOption

type RequestOption func(req *http.Request) error

RequestOption is like middleware which can change request before the client send it.

func AuthenticateHeader

func AuthenticateHeader(header string, tokenType string, token string) RequestOption

func AuthorizationToken

func AuthorizationToken(tokenType string, token string) RequestOption

func BasicAuth

func BasicAuth(username string, password string) RequestOption

func BearerToken

func BearerToken(token string) RequestOption

func QueryParams

func QueryParams(params hexa.Map) RequestOption

type URL

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

func NewURL

func NewURL(base string) (*URL, error)

NewURL creates a new url with the base url value. we can use it to convert relative url paths to absolute path.

func (*URL) URL

func (u *URL) URL(url string, options ...URLOption) (resultUrl *urlpkg.URL, err error)

type URLOption

type URLOption func(url *urlpkg.URL) error

func UrlQueryParams

func UrlQueryParams(params hexa.Map) URLOption

Jump to

Keyboard shortcuts

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