diff --git a/client.go b/client.go index 2de29aa..b2e3bbe 100644 --- a/client.go +++ b/client.go @@ -20,46 +20,49 @@ func NewClient() *Client { }} } -func (this *Client) getTransport() *http.Transport { - return this.cli.Transport.(*http.Transport) +func (c *Client) getTransport() *http.Transport { + return c.cli.Transport.(*http.Transport) } -func (this *Client) SetTimeOut(d time.Duration) *Client { - this.cli.Timeout = d - return this +func (c *Client) SetTimeOut(d time.Duration) *Client { + c.cli.Timeout = d + return c } -func (this *Client) SetTransport(transport *http.Transport) *Client { - this.cli.Transport = transport - return this +// SetTransport +// if you used SetTransport after SetTimeOut or SetProxyURL, +// SetTimeOut/SetProxyURL will be invalid +func (c *Client) SetTransport(transport *http.Transport) *Client { + c.cli.Transport = transport + return c } -func (this *Client) SetProxyURL(url string) *Client { +func (c *Client) SetProxyURL(url string) *Client { urlProxy, err := neturl.Parse(url) if err != nil { panic(err) } - this.getTransport().Proxy = http.ProxyURL(urlProxy) - return this + c.getTransport().Proxy = http.ProxyURL(urlProxy) + return c } -func (this *Client) SetInsecureSkipVerify(skip bool) *Client { - this.getTransport().TLSClientConfig = &tls.Config{InsecureSkipVerify: skip} - return this +func (c *Client) SetInsecureSkipVerify(skip bool) *Client { + c.getTransport().TLSClientConfig = &tls.Config{InsecureSkipVerify: skip} + return c } -func (this *Client) Get(url string) *Request { - return NewRequest(Method_GET, url).SetClient(this.cli) +func (c *Client) Get(url string) *Request { + return NewRequest(Method_GET, url).SetClient(c.cli) } -func (this *Client) Post(url string) *Request { - return NewRequest(Method_POST, url).SetClient(this.cli) +func (c *Client) Post(url string) *Request { + return NewRequest(Method_POST, url).SetClient(c.cli) } -func (this *Client) Put(url string) *Request { - return NewRequest(Method_PUT, url).SetClient(this.cli) +func (c *Client) Put(url string) *Request { + return NewRequest(Method_PUT, url).SetClient(c.cli) } -func (this *Client) Delete(url string) *Request { - return NewRequest(Method_DELETE, url).SetClient(this.cli) +func (c *Client) Delete(url string) *Request { + return NewRequest(Method_DELETE, url).SetClient(c.cli) } diff --git a/response.go b/response.go index caf0fff..c38bab1 100644 --- a/response.go +++ b/response.go @@ -1,6 +1,7 @@ package hasaki import ( + "bytes" "io/ioutil" "net/http" @@ -8,6 +9,7 @@ import ( "github.com/pkg/errors" ) +// ErrorChecker if you need to decode the body, please use CloserWrapper to save the result type ErrorChecker func(resp *http.Response) error var DefaultErrorChecker ErrorChecker = func(resp *http.Response) error { @@ -47,3 +49,13 @@ func (c *Response) BindJSON(v interface{}) error { defer c.Body.Close() return errors.WithStack(jsoniter.NewDecoder(c.Body).Decode(v)) } + +type CloserWrapper []byte + +func (c *CloserWrapper) Read(p []byte) (n int, err error) { + return bytes.NewBuffer(*c).Read(p) +} + +func (c *CloserWrapper) Close() error { + return nil +}