Skip to content

Commit

Permalink
impl CloserWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed Oct 10, 2022
1 parent d7bb2fb commit 764f8d9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
47 changes: 25 additions & 22 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 12 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package hasaki

import (
"bytes"
"io/ioutil"
"net/http"

jsoniter "github.com/json-iterator/go"
"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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 764f8d9

Please sign in to comment.