Documentation
¶
Overview ¶
Package httpclient provides simple httpclient for any REST request in XML or JSON payload
Example (Delete_WithDecorators) ¶
package main import ( "context" "fmt" "net/http" "github.com/kepinsu/httpclient" ) func main() { // Random Decorator like Logging var logger httpclient.Decorator = func(d httpclient.Doer) httpclient.Doer { return httpclient.DoerFunc(func(r *http.Request) (*http.Response, error) { return d.Do(r) }) } // Create a http client client, err := httpclient.NewClient("http://example.com", httpclient.WithDecorator(logger)) if err != nil { fmt.Println("fail to setup the client", err) return } // Want the response in JSON decode client.Delete(context.Background(), "", nil, nil, nil, httpclient.WithIsJson()) }
Output:
Example (Get) ¶
package main import ( "context" "fmt" "github.com/kepinsu/httpclient" ) func main() { // Create a http client client, err := httpclient.NewClient("http://example.com") if err != nil { fmt.Println("fail to setup the client", err) return } client.Get(context.Background(), "", nil, nil) }
Output:
Example (Post_Multipart) ¶
package main import ( "context" "fmt" "strings" "github.com/kepinsu/httpclient" ) func main() { // Create a http client client, err := httpclient.NewClient("http://example.com") if err != nil { fmt.Println("fail to setup the client", err) return } // prepare the multipart body m := httpclient.NewMultipartBody() m.SetMultipartFields( httpclient.MultipartField{ Param: "uploadManifest1", FileName: "upload-file-1.json", ContentType: "application/json", Reader: strings.NewReader(`{"input": {"name": "Uploaded document 1", "_filename" : ["file1.txt"]}}`), }, httpclient.MultipartField{ Param: "uploadManifest2", ContentType: "application/json", ContentID: "up", Reader: strings.NewReader(`{"input": {"name": "random file"}}`), }) // Want the response in JSON decode client.Post(context.Background(), "", m, nil, nil, httpclient.WithIsJson()) }
Output:
Index ¶
- Variables
- type Client
- func (c *Client) Delete(ctx context.Context, path string, body any, result any, resultError any, ...) (Response, error)
- func (c *Client) Do(r *http.Request) (*http.Response, error)
- func (c *Client) Get(ctx context.Context, path string, result any, resultError any, ...) (Response, error)
- func (c *Client) Head(ctx context.Context, path string, resultError any, opts ...RequestOption) (Response, error)
- func (c *Client) NewRequest(path string, method string, body any, opts ...RequestOption) (*http.Request, error)
- func (c *Client) NewRequestWithContext(ctx context.Context, path string, method string, body any, ...) (*http.Request, error)
- func (c *Client) Post(ctx context.Context, path string, body any, result any, resultError any, ...) (Response, error)
- func (c *Client) PostForm(ctx context.Context, path string, data url.Values, result any, resultError any, ...) (Response, error)
- func (c *Client) Put(ctx context.Context, path string, body any, result any, resultError any, ...) (Response, error)
- func (c *Client) SetBaseURL(url string) *Client
- type ClientsOption
- type Decorator
- type Doer
- type DoerFunc
- type MultipartBody
- type MultipartField
- type RequestOption
- type Response
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrResponseBodyTooLarge = errors.New("httpclient: response body too large")
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client struct is used to create a httpclient with client-level settings, these settings apply to all the requests raised from the client.
func (*Client) Delete ¶
func (c *Client) Delete( ctx context.Context, path string, body any, result any, resultError any, opts ...RequestOption, ) (Response, error)
Delete method does DELETE HTTP request. It's defined in section 4.3.5 of RFC7231.
func (*Client) Get ¶
func (c *Client) Get( ctx context.Context, path string, result any, resultError any, opts ...RequestOption, ) (Response, error)
Get method does GET HTTP request. It's defined in section 4.3.1 of RFC7231.
func (*Client) NewRequest ¶
func (c *Client) NewRequest( path string, method string, body any, opts ...RequestOption) (*http.Request, error)
NewRequest method returns the http.Request if your need to create your own request.
func (*Client) NewRequestWithContext ¶
func (c *Client) NewRequestWithContext(ctx context.Context, path string, method string, body any, opts ...RequestOption) (*http.Request, error)
NewRequestWithContext method returns the http.Request if your need to create your own request.
func (*Client) Post ¶
func (c *Client) Post( ctx context.Context, path string, body any, result any, resultError any, opts ...RequestOption, ) (Response, error)
Post method does POST HTTP request. It's defined in section 4.3.3 of RFC7231.
func (*Client) PostForm ¶
func (c *Client) PostForm( ctx context.Context, path string, data url.Values, result any, resultError any, opts ...RequestOption) (Response, error)
Post method does POST HTTP request. It's defined in section 4.3.3 of RFC7231.
func (*Client) Put ¶
func (c *Client) Put( ctx context.Context, path string, body any, result any, resultError any, opts ...RequestOption, ) (Response, error)
Put method does PUT HTTP request. It's defined in section 4.3.4 of RFC7231.
func (*Client) SetBaseURL ¶
SetBaseURL method sets the Base URL in the client instance. It will be used with a request raised from this client with a relative URL
// Setting HTTP address client.SetBaseURL("http://myjeeva.com") // Setting HTTPS address client.SetBaseURL("https://myjeeva.com")
type ClientsOption ¶
type ClientsOption func(*clientConfig)
ClientsOption is to create convenient client options like wait custom RoundTripper, custom http.client
func WithDecorator ¶
func WithDecorator(decorators ...Decorator) ClientsOption
WithDecorators is to add custom Decorators for http call Is more like http middlewares
func WithSizeLimit ¶
func WithSizeLimit(limitSize int) ClientsOption
WithDecorators is to add custom Decorators for http call Is more like http middlewares
func WithTransport ¶
func WithTransport(t http.RoundTripper) ClientsOption
WithTransport is to add custom Transport for http call
func WithUserAgent ¶
func WithUserAgent(u string) ClientsOption
WithUserAgent is to add user agent for any http request
type DoerFunc ¶
DoerFunc is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a function with the appropriate signture
type MultipartBody ¶ added in v0.1.0
type MultipartBody struct { // The Header of the Boundary like multipart/form-data Boundary string List []MultipartField }
MultipartBody struct represents the multipart body in HTTP request
func NewMultipartBody ¶ added in v0.1.0
func NewMultipartBody() *MultipartBody
func (*MultipartBody) SetMultipartFields ¶ added in v0.1.0
func (m *MultipartBody) SetMultipartFields(fields ...MultipartField)
SetMultipartFields method sets multiple data fields using io.Reader for multipart upload.
For Example:
m := NewMultipartBody() m.SetMultipartFields( httpclient.MultipartField{ Param: "uploadManifest1", FileName: "upload-file-1.json", ContentType: "application/json", Reader: strings.NewReader(`{"input": {"name": "Uploaded document 1", "_filename" : ["file1.txt"]}}`), }, httpclient.MultipartField{ Param: "uploadManifest2", ContentID: "2", ContentType: "application/json", Reader: strings.NewReader(`{"input": {"name": "random file"}}`), })
If you have a `slice` of fields already, then call-
m.SetMultipartFields.SetMultipartFields(fields...)
type MultipartField ¶ added in v0.1.0
type MultipartField struct { Param string FileName string ContentType string ContentID string io.Reader }
MultipartField struct represents the custom data part for a multipart request
type RequestOption ¶
type RequestOption func(*requestConfig)
RequestOption is to create convenient request options like wait custom fields for http.request
func WithHeaders ¶
func WithHeaders(headers http.Header) RequestOption
WithHeaders is when you need to add specific headers in your request
func WithIsJson ¶
func WithIsJson() RequestOption
WithIsJson is to indicate this request/response is in json payload
func WithIsXml ¶
func WithIsXml() RequestOption
WithIsXml is to indicate this request/response is in xml payload
func WithQueries ¶
func WithQueries( queries map[string]string) RequestOption
WithQueries is when you need to add specific query in your request