commit 84fe20aea43bf0f2b66e5892fbd195f1c581f1a4 Author: Paulo Simão Date: Sat Oct 9 10:34:41 2021 -0300 1st ver diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d1775f0 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module go.digitalcircle.com.br/open/httpcli + +go 1.17 diff --git a/lib.go b/lib.go new file mode 100644 index 0000000..1218817 --- /dev/null +++ b/lib.go @@ -0,0 +1,138 @@ +package httpcli + +import ( + "bytes" + "encoding/json" + "io" + "net/http" + "strings" +) + +type Cli struct { + cli *http.Client + headers map[string]string + basePath string +} + +func (c *Cli) BasePath() string { + return c.basePath +} + +func (c *Cli) SetBasePath(basePath string) { + c.basePath = basePath +} + +func (c *Cli) AddHeader(k string, v string) { + c.headers[k] = v +} +func (c *Cli) DelHeader(k string) { + delete(c.headers, k) +} +func (c *Cli) Do(method string, strurl string, body []byte) (*http.Response, error) { + if !strings.HasPrefix(strurl, "http") { + addr := c.BasePath() + if strings.HasSuffix(addr, "/") { + addr = addr[:len(addr)-1] + } + if !strings.HasPrefix(strurl, "") { + strurl = "/" + strurl + } + strurl = addr + strurl + } + req, err := http.NewRequest(method, strurl, bytes.NewReader(body)) + if err != nil { + return nil, err + } + if c.headers != nil { + for k, v := range c.headers { + req.Header.Set(k, v) + } + } + if body != nil && len(body) > 0 { + req, err = http.NewRequest(method, strurl, io.NopCloser(bytes.NewReader(body))) + + } else { + req, err = http.NewRequest(method, strurl, nil) + } + if err != nil { + return nil, err + } + return c.cli.Do(req) +} +func (c *Cli) DoJson(method string, strurl string, i interface{}, o interface{}) (err error) { + bs, err := json.Marshal(i) + if err != nil { + return err + } + res, err := c.Do(method, strurl, bs) + if err != nil { + return + } + defer res.Body.Close() + if o != nil { + err = json.NewDecoder(res.Body).Decode(o) + } + return +} +func (c *Cli) JsonGet(strurl string, o interface{}) error { + return c.DoJson(http.MethodGet, strurl, nil, o) +} +func (c *Cli) JsonDelete(strurl string, o interface{}) error { + return c.DoJson(http.MethodDelete, strurl, nil, o) +} +func (c *Cli) JsonHead(strurl string, o interface{}) error { + return c.DoJson(http.MethodHead, strurl, nil, o) +} +func (c *Cli) JsonPost(strurl string, i interface{}, o interface{}) error { + return c.DoJson(http.MethodPost, strurl, i, o) +} +func (c *Cli) JsonPut(strurl string, i interface{}, o interface{}) error { + return c.DoJson(http.MethodPut, strurl, i, o) +} +func (c *Cli) JsonPatch(strurl string, i interface{}, o interface{}) error { + return c.DoJson(http.MethodPatch, strurl, i, o) +} +func (c *Cli) RawGet(strurl string) ([]byte, error) { + res, err := c.Do(http.MethodGet, strurl, nil) + if err != nil { + return nil, err + } + defer res.Body.Close() + bs, _ := io.ReadAll(res.Body) + return bs, nil +} +func (c *Cli) RawDelete(strurl string) ([]byte, error) { + res, err := c.Do(http.MethodDelete, strurl, nil) + if err != nil { + return nil, err + } + defer res.Body.Close() + bs, _ := io.ReadAll(res.Body) + return bs, nil +} +func (c *Cli) RawPost(strurl string, i []byte) ([]byte, error) { + res, err := c.Do(http.MethodPost, strurl, i) + if err != nil { + return nil, err + } + defer res.Body.Close() + bs, _ := io.ReadAll(res.Body) + return bs, nil +} +func (c *Cli) RawPut(strurl string, i []byte) ([]byte, error) { + res, err := c.Do(http.MethodPut, strurl, i) + if err != nil { + return nil, err + } + defer res.Body.Close() + bs, _ := io.ReadAll(res.Body) + return bs, nil +} + +func NewCli() *Cli { + ret := &Cli{ + cli: &http.Client{}, + headers: make(map[string]string), + } + return ret +}