Documentation
¶
Overview ¶
Package gwc is simple HTTP client based on premise to use client side middlewares. It is based on cliware library that defines basic type for client side middlewares and series of middlewares implemented in cliware-middlewares package.
Basic idea behind this client is to create simple, yet easily composable HTTP client. Client does provide simple Request and Response types, but they are only thin wrappers around same type in http package used for middleware composition.
Example ¶
package main
import (
"fmt"
"net/http"
"github.com/delicb/cliware-middlewares/errors"
"github.com/delicb/cliware-middlewares/headers"
)
type HTTPBinResponse struct {
Headers struct {
UserAgent string `json:"User-Agent"`
} `json:"headers"`
}
func main() {
respBody := new(HTTPBinResponse)
client := New(
http.DefaultClient,
// Add user agent header, it will be applied to all requests
headers.Add("User-Agent", "example-client"),
errors.Errors(),
)
// send request
resp, err := client.Get().URL("https://httpbin.org/get").Send()
// check errors
// because of errors middleware included in client, ever status codes
// 400+ will be turned into errors.
if err != nil {
panic(err)
}
resp.JSON(respBody)
fmt.Println(respBody.Headers.UserAgent)
}
Output: example-client
Index ¶
- func ClientFromContext(ctx context.Context) *http.Client
- type Client
- func (c *Client) Delete() *Request
- func (c *Client) Do(middlewares ...cliware.Middleware) (*Response, error)
- func (c *Client) DoCtx(ctx context.Context, middlewares ...cliware.Middleware) (*Response, error)
- func (c *Client) Get() *Request
- func (c *Client) Head() *Request
- func (c *Client) Options() *Request
- func (c *Client) Patch() *Request
- func (c *Client) Post() *Request
- func (c *Client) Put() *Request
- func (c *Client) Request() *Request
- func (c *Client) Use(m cliware.Middleware) *Client
- func (c *Client) UseFunc(m func(cliware.Handler) cliware.Handler) *Client
- func (c *Client) UsePost(m cliware.Middleware) *Client
- func (c *Client) UsePostFunc(m func(cliware.Handler) cliware.Handler) *Client
- type Doer
- type Group
- type Request
- func (r *Request) AddCookie(cookie *http.Cookie) *Request
- func (r *Request) AddHeader(key, value string) *Request
- func (r *Request) AddPath(path string) *Request
- func (r *Request) AddQuery(key, value string) *Request
- func (r *Request) BaseURL(rawURL string) *Request
- func (r *Request) BodyJSON(data interface{}) *Request
- func (r *Request) Context() context.Context
- func (r *Request) Method(method string) *Request
- func (r *Request) Param(key, value string) *Request
- func (r *Request) Params(values map[string]string) *Request
- func (r *Request) Path(path string) *Request
- func (r *Request) Send() (*Response, error)
- func (r *Request) SetContext(ctx context.Context) *Request
- func (r *Request) SetCookie(key, value string) *Request
- func (r *Request) SetHeader(key, value string) *Request
- func (r *Request) SetHeaders(headerMap map[string]string) *Request
- func (r *Request) SetQuery(key, value string) *Request
- func (r *Request) SetQueryParams(values map[string]string) *Request
- func (r *Request) URL(rawURL string) *Request
- func (r *Request) Use(m ...cliware.Middleware) *Request
- func (r *Request) UseFunc(m func(cliware.Handler) cliware.Handler) *Request
- type Response
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
Before *cliware.Chain
After *cliware.Chain
// contains filtered or unexported fields
}
Client is main point of contact with this this library. It is used to set up basic configuration that will be used for all requests (except if it is overridden on per-request basis). For better performance (reuse of connections) only one instance of client should be created.
func New ¶
func New(client *http.Client, middlewares ...cliware.Middleware) *Client
New creates and returns instance of a client. First parameter is http.Client instance that will be used to send actual requests. This can be used to modify default behavior (like TLS or timeouts). If nil is provided, &http.Client{} will be used.
Also, any number of middlewares can be provided. These will be used for each request. Additional middlewares can be added by using "Use*" methods on a client.
func (*Client) Do ¶
func (c *Client) Do(middlewares ...cliware.Middleware) (*Response, error)
Do creates new request, applies all provided middlewares to it and sends request. Context that is used is context.Background()
func (*Client) DoCtx ¶
DoCtx creates new request, applies all provided middlewares to it and sends request with provided context.
func (*Client) Request ¶
Request creates and returns new request that uses this client to perform HTTP request and uses its defined middlewares.
func (*Client) Use ¶
func (c *Client) Use(m cliware.Middleware) *Client
Use adds provided middleware to this clients middleware chain.
type Doer ¶
type Doer interface {
Do(middlewares ...cliware.Middleware) (*Response, error)
DoCtx(ctx context.Context, middlewares ...cliware.Middleware) (*Response, error)
}
Doer defines that object is capable of executing HTTP request with provided middlewares.
type Group ¶
Group is container of any additional middlewares that group of endpoints use.
func NewGroup ¶
func NewGroup(next Doer, middlewares ...cliware.Middleware) *Group
NewGroup creates and returns new instance of Group that will apply all provided middlewares and use next Doer to do actual work.
func (*Group) Do ¶
func (s *Group) Do(middlewares ...cliware.Middleware) (*Response, error)
Do applies all middlewares from this layer and provided middlewares and calls next Doer to do actual work.
func (*Group) DoCtx ¶
DoCtx applies all middlewares from this layer and provided middlewares and calls next Doer to do actual work.
type Request ¶
type Request struct {
Client *Client
// contains filtered or unexported fields
}
Request is struct used to hold information (mostly middlewares) used to construct HTTP request.
func NewRequest ¶
NewRequest creates new instance of request for provided client and with initial middleware chain.
func (*Request) AddHeader ¶
AddHeader adds header with provided name and value to current request. It does not override existing headers.
func (*Request) AddQuery ¶
AddQuery adds query parameter to URL for this request. If parameter with same name already exist, new one will be appended. To replace it, use SetQuery.
func (*Request) Params ¶
Params replaces all keys in URL with key-value pairs provided in map for this request.
func (*Request) Send ¶
Send constructs and sends HTTP request. This method uses all defined middlewares and client defined in requests to construct HTTP request.
func (*Request) SetContext ¶
SetContext sets instance of context.Context used for this request.
func (*Request) SetHeader ¶
SetHeader sets provided header value to current request. If header with same name already exists, it will be overridden. To add same header again, use AddHeader.
func (*Request) SetHeaders ¶
SetHeaders sets headers for this request provided in map.
func (*Request) SetQuery ¶
SetQuery sets query parameter to URL for this request. If parameter already exists it will be replaced.
func (*Request) SetQueryParams ¶
SetQueryParams sets query parameters for this request provided in the map.
type Response ¶
Response is thin wrapper around http.Response that provides some convenient behavior.
func BuildResponse ¶
BuildResponse creates new instance of response based on provided raw HTTP response.
func (*Response) SaveToFile ¶
SaveToFile writes response content to file with provided path.