Documentation
¶
Index ¶
- Variables
- func GetBody(body interface{}) (buf io.ReadWriter, err error)
- type Request
- func (r *Request) Execute(ctx context.Context, method string, url string, body io.Reader, ...) error
- func (r *Request) Get(result interface{}, path string, query url.Values) error
- func (r *Request) GetBase(path string) string
- func (r *Request) GetWithCache(result interface{}, path string, query url.Values, cache time.Duration) error
- func (r *Request) GetWithContext(ctx context.Context, result interface{}, path string, query url.Values) error
- func (r *Request) Post(result interface{}, path string, body interface{}) error
- func (r *Request) PostWithCache(result interface{}, path string, body interface{}, cache time.Duration) error
- func (r *Request) PostWithContext(ctx context.Context, result interface{}, path string, body interface{}) error
- func (r *Request) SetTimeout(seconds time.Duration)
Constants ¶
This section is empty.
Variables ¶
var DefaultClient = &http.Client{ Timeout: time.Second * 15, }
DefaultClient represents a generic and default http client.
DefaultErrorHandler represents a generic and default error handler.
Functions ¶
func GetBody ¶
func GetBody(body interface{}) (buf io.ReadWriter, err error)
GetBody cast custom object body to io.ReadWriter buffer It returns an error if occurs.
Types ¶
type Request ¶
type Request struct { BaseURL string Headers map[string]string HTTPClient *http.Client ErrorHandler func(res *http.Response, uri string) error }
Request object
func InitJSONClient ¶
InitJSONClient initialize the client for application/json requests.
func (*Request) Execute ¶
func (r *Request) Execute(ctx context.Context, method string, url string, body io.Reader, result interface{}) error
Execute sends any HTTP request and returns an HTTP response, following policy (such as redirects, cookies, auth) as configured on the client. The response is unmarshal and stored inside the result parameter.
An error is returned if caused by client policy (such as CheckRedirect), or failure to speak HTTP (such as a network connectivity problem). A non-2xx status code doesn't cause an error.
eg.:
var block Block err := r.Execute("POST", uri, buf, result)
func (*Request) Get ¶
Get sends an HTTP GET request and returns an HTTP response in background context, following policy (such as redirects, cookies, auth) as configured on the client.
The response is unmarshal and stored inside the result parameter.
An error is returned if caused by client policy (such as CheckRedirect), or failure to speak HTTP (such as a network connectivity problem). A non-2xx status code doesn't cause an error.
eg.:
var block Block err := c.Get(&block, "blocks/latest", url.Values{"page": {"1"}})
func (*Request) GetWithCache ¶
func (r *Request) GetWithCache(result interface{}, path string, query url.Values, cache time.Duration) error
GetWithCache sends an HTTP GET request with cache duration. After the duration the cache is expires and the request are made again. It returns an error if occurs.
eg.:
var block Block err := c.Get(&block, "blocks/latest", url.Values{"page": {"1"}}, time.Minute*20)
func (*Request) GetWithContext ¶
func (r *Request) GetWithContext(ctx context.Context, result interface{}, path string, query url.Values) error
GetWithContext sends an HTTP GET request and returns an HTTP response in the passed context, following policy (such as redirects, cookies, auth) as configured on the client.
The response is unmarshal and stored inside the result parameter.
An error is returned if caused by client policy (such as CheckRedirect), or failure to speak HTTP (such as a network connectivity problem). A non-2xx status code doesn't cause an error.
eg.:
var block Block err := c.GetWithContext(&block, "blocks/latest", url.Values{"page": {"1"}}, context.Background())
func (*Request) Post ¶
Post sends an HTTP POST request and returns an HTTP response in background context, following policy (such as redirects, cookies, auth) as configured on the client.
The response is unmarshal and stored inside the result parameter.
An error is returned if caused by client policy (such as CheckRedirect), or failure to speak HTTP (such as a network connectivity problem). A non-2xx status code doesn't cause an error.
eg.:
var block Block err := c.Post(&block, "blocks/latest", CustomObject{Id: 3, Name: "request"})
func (*Request) PostWithCache ¶
func (r *Request) PostWithCache(result interface{}, path string, body interface{}, cache time.Duration) error
PostWithCache sends an HTTP POST request with cache duration. After the duration the cache is expires and the request are made again. It returns an error if occurs.
eg.:
var block Block err := c.PostWithCache(&block, "blocks/latest", CustomObject{Id: 3, Name: "request"}, time.Minute*20)
func (*Request) PostWithContext ¶
func (r *Request) PostWithContext(ctx context.Context, result interface{}, path string, body interface{}) error
PostWithContext sends an HTTP POST request and returns an HTTP response in the passed context, following policy (such as redirects, cookies, auth) as configured on the client.
The response is unmarshal and stored inside the result parameter.
An error is returned if caused by client policy (such as CheckRedirect), or failure to speak HTTP (such as a network connectivity problem). A non-2xx status code doesn't cause an error.
eg.:
var block Block err := c.PostWithContext(&block, "blocks/latest", CustomObject{Id: 3, Name: "request"}, context.Background())
func (*Request) SetTimeout ¶
SetTimeout set the timeout request.