Documentation
¶
Index ¶
- Constants
- Variables
- func NewMultipartFormFile(name string, filePath string) multipartFormFile
- func NewMultipartFormFileReader(name, fileName string, r io.Reader) multipartFormFile
- type AsyncResponse
- type Client
- func (c *Client) AddHeader(key, value string) *Client
- func (c *Client) AddHeaders(headers http.Header) *Client
- func (c *Client) AddQueryParam(key, value string) *Client
- func (c *Client) AddQueryParams(queryParams url.Values) *Client
- func (c *Client) NewRequest() *Request
- func (c *Client) SetBaseUrl(baseUrl string) *Client
- func (c *Client) SetClient(client *http.Client) *Client
- func (c *Client) SetDebug(debug, includeBody bool) *Client
- func (c *Client) SetHeader(key, value string) *Client
- func (c *Client) SetHeaders(headers http.Header) *Client
- func (c *Client) SetLogEnabled(enable bool) *Client
- func (c *Client) SetLogFlags(flag int) *Client
- func (c *Client) SetLogOutput(w io.Writer) *Client
- func (c *Client) SetLogTimeFormat(layout string) *Client
- func (c *Client) SetQueryParam(key, value string) *Client
- func (c *Client) SetQueryParams(queryParams url.Values) *Client
- func (c *Client) SetTimeout(timeout time.Duration) *Client
- type Request
- func (r *Request) AddHeader(key, value string) *Request
- func (r *Request) AddHeaders(headers http.Header) *Request
- func (r *Request) AddQueryParam(key, value string) *Request
- func (r *Request) AddQueryParams(queryParams url.Values) *Request
- func (r *Request) BodyCustom(f func() (*bytes.Buffer, error)) *Request
- func (r *Request) BodyFormUrlEncoded(data url.Values) *Request
- func (r *Request) BodyJson(data any) *Request
- func (r *Request) BodyMultipartForm(data map[string]any, files ...multipartFormFile) *Request
- func (r *Request) BodyRaw(data []byte) *Request
- func (r *Request) BodyXml(data any) *Request
- func (r *Request) Do() (*Response, error)
- func (r *Request) DoAsync() <-chan AsyncResponse
- func (r *Request) DoAsyncCtx(ctx context.Context) <-chan AsyncResponse
- func (r *Request) DoCtx(ctx context.Context) (*Response, error)
- func (r *Request) DoStream(ctx context.Context) (*ResponseStream, error)
- func (r *Request) SetBaseUrl(baseUrl string) *Request
- func (r *Request) SetDebug(debug, includeBody bool) *Request
- func (r *Request) SetHeader(key, value string) *Request
- func (r *Request) SetHeaders(headers http.Header) *Request
- func (r *Request) SetLogEnabled(enabled bool) *Request
- func (r *Request) SetMethod(method string) *Request
- func (r *Request) SetPath(path string) *Request
- func (r *Request) SetQueryParam(key, value string) *Request
- func (r *Request) SetQueryParams(queryParams url.Values) *Request
- func (r *Request) SetTimeout(timeout time.Duration) *Request
- type Response
- func (r *Response) BodyRaw() []byte
- func (r *Response) BodyString() string
- func (r *Response) GetHeader(key string) string
- func (r *Response) Headers() http.Header
- func (r *Response) IsError() error
- func (r *Response) Status() string
- func (r *Response) StatusCode() int
- func (r *Response) Unmarshal(u ResponseUnmarshaler) error
- type ResponseError
- func (r *ResponseError) BodyRaw() []byte
- func (r *ResponseError) BodyString() string
- func (r ResponseError) Error() string
- func (r *ResponseError) GetHeader(key string) string
- func (r *ResponseError) Headers() http.Header
- func (r *ResponseError) Status() string
- func (r *ResponseError) StatusCode() int
- type ResponseStream
- func (r *ResponseStream) Close()
- func (r *ResponseStream) GetHeader(key string) string
- func (r *ResponseStream) Headers() http.Header
- func (r *ResponseStream) Recv(n uint) ([]byte, error)
- func (r *ResponseStream) RecvFunc(sr StreamReceiver) error
- func (r *ResponseStream) Status() string
- func (r *ResponseStream) StatusCode() int
- type ResponseUnmarshaler
- type StreamReceiver
Constants ¶
const ( Fshortfile = 1 << iota // short file name and line number: file.go:123 Flongfile // full file name and line number: a/b/c/file.go:123 Ftime // whether to include date-time in the log message FtimeUTC // if [Ftime] is set then use UTC ContentTypeJson = "application/json" ContentTypeXml = "application/xml" ContentTypeFormUrlEncoded = "application/x-www-form-urlencoded" ContentTypeTextEventStream = "text/event-stream" )
Variables ¶
var (
ErrRequestTimedOut = errors.New("request timed out")
)
Functions ¶
func NewMultipartFormFile ¶
NewMultipartFormFile creates a new multipartform file by reading the file from the given filepath
Types ¶
type AsyncResponse ¶ added in v2.2.0
AsyncResponse is a structure holding response data for async request
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the client used by the package
func (*Client) AddHeaders ¶
AddHeaders adds the header values
func (*Client) AddQueryParam ¶
AddQueryParam adds a single query parameter
func (*Client) AddQueryParams ¶
AddQueryParams adds the query parameters
func (*Client) SetBaseUrl ¶
SetBaseUrl sets the base URL
func (*Client) SetClient ¶
SetClient sets the underlying net/http.Client
func (*Client) SetHeaders ¶
SetHeaders sets the header values
func (*Client) SetLogEnabled ¶
SetLogEnabled sets the log mode
func (*Client) SetLogFlags ¶
SetLogFlags sets the log flags
func (*Client) SetLogOutput ¶
SetLogOutput sets the log output to the given io.Writer
func (*Client) SetLogTimeFormat ¶
SetLogTimeFormat sets the log time format if Ftime flag is given
func (*Client) SetQueryParam ¶
SetQueryParam sets a single query parameter
func (*Client) SetQueryParams ¶
SetQueryParams sets the query parameters
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request is the request created by calling NewRequest
func (*Request) AddHeaders ¶
AddHeaders adds the header values
func (*Request) AddQueryParam ¶
AddQueryParam adds a single query parameter
func (*Request) AddQueryParams ¶
AddQueryParams adds the query parameters
func (*Request) BodyCustom ¶
BodyCustom prepares the body with the given callback function
func (*Request) BodyFormUrlEncoded ¶
BodyFormUrlEncoded prepares the body as a form URL encoded request with the given data. Content-Type header is automatically set to "application/x-www-form-urlencoded"
func (*Request) BodyJson ¶
BodyJson prepares the body as a JSON request with the given data. Content-Type header is automatically set to "application/json"
func (*Request) BodyMultipartForm ¶
BodyMultipartForm prepares the body as a multipartform request with the given data and files. Content-Type header is automatically set to "multipart/form-data" with the proper boundary. Use NewMultipartFormFile or NewMultipartFormFileReader to pass files for file upload
func (*Request) BodyXml ¶
BodyXml prepares the body as an XML request with the given data. Content-Type header is automatically set to "application/xml"
func (*Request) Do ¶
Do performs the request using context.Background
func (*Request) DoAsync ¶ added in v2.2.0
func (r *Request) DoAsync() <-chan AsyncResponse
DoAsync performs an async request using context.Background. It returns an AsyncResponse channel which will receive the response when the request completes
func (*Request) DoAsyncCtx ¶ added in v2.2.0
func (r *Request) DoAsyncCtx(ctx context.Context) <-chan AsyncResponse
DoAsyncCtx performs an async request with the given context.Context. It returns an AsyncResponse channel which will receive the response when the request completes
func (*Request) DoCtx ¶
DoCtx performs the request with the given context.Context and returns a response
func (*Request) DoStream ¶
func (r *Request) DoStream(ctx context.Context) (*ResponseStream, error)
DoStream performs a request using the given context.Context and returns a streaming response
func (*Request) SetBaseUrl ¶
SetBaseUrl sets the base URL
func (*Request) SetHeaders ¶
SetHeaders sets the header values
func (*Request) SetLogEnabled ¶
SetLogEnabled sets the log mode
func (*Request) SetQueryParam ¶
SetQueryParam sets a single query parameter
func (*Request) SetQueryParams ¶
SetQueryParams sets the query parameters
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response holds the response data
func (*Response) BodyString ¶
BodyString returns the response body as string
func (*Response) GetHeader ¶
GetHeader is a convenience method to retrieve a single response header value
func (*Response) IsError ¶
IsError returns a non nil error if the response is considered as an error based on the status code. The error's type will be *ResponseError
func (*Response) Status ¶
func (r *Response) Status() string
Status returns the status of a response
func (*Response) StatusCode ¶
func (r *Response) StatusCode() int
StatusCode returns the status code of a response
func (*Response) Unmarshal ¶
func (r *Response) Unmarshal(u ResponseUnmarshaler) error
Unmarshal is a convenience method that can receive a ResponseUnmarshaler callback function that performs the unmarshalling of the response body
type ResponseError ¶ added in v2.1.0
type ResponseError struct {
// contains filtered or unexported fields
}
ResponseError holds data of response that is considered to be an error
func (*ResponseError) BodyRaw ¶ added in v2.1.0
func (r *ResponseError) BodyRaw() []byte
BodyRaw returns the response body as a byte slice
func (*ResponseError) BodyString ¶ added in v2.1.0
func (r *ResponseError) BodyString() string
BodyString returns the response body as string
func (ResponseError) Error ¶ added in v2.1.0
func (r ResponseError) Error() string
Error implements the error interface
func (*ResponseError) GetHeader ¶ added in v2.1.0
GetHeader is a convenience method to retrieve a single response header value
func (*ResponseError) Status ¶ added in v2.1.0
func (r *ResponseError) Status() string
Status returns the status of a response
func (*ResponseError) StatusCode ¶ added in v2.1.0
func (r *ResponseError) StatusCode() int
StatusCode returns the status code of a response
type ResponseStream ¶
type ResponseStream struct {
// contains filtered or unexported fields
}
ResponseStream is a streamed response
func (*ResponseStream) Close ¶
func (r *ResponseStream) Close()
Close closes the streamed response body and additionally frees up any resources associated with the context.Context used to perform the streamed request
func (*ResponseStream) GetHeader ¶
GetHeader is a convenience method to retrieve a single response header value
func (*ResponseStream) Recv ¶
func (r *ResponseStream) Recv(n uint) ([]byte, error)
Recv reads up to n bytes from a streamed response body
func (*ResponseStream) RecvFunc ¶
func (r *ResponseStream) RecvFunc(sr StreamReceiver) error
RecvFunc can receive a StreamReceiver callback function that performs the stream reading of the streamed response body
func (*ResponseStream) Status ¶
func (r *ResponseStream) Status() string
Status returns the status of a response
func (*ResponseStream) StatusCode ¶
func (r *ResponseStream) StatusCode() int
StatusCode returns the status code of a response
type ResponseUnmarshaler ¶
ResponseUnmarshaler is a function that can be used to unmarshal a response
type StreamReceiver ¶
StreamReceiver is a function that can be used to read from a streamed response