master
Paulo Simão 2021-10-09 10:34:41 -03:00
commit 84fe20aea4
3 changed files with 142 additions and 0 deletions

1
.gitignore vendored 100644
View File

@ -0,0 +1 @@
.idea

3
go.mod 100644
View File

@ -0,0 +1,3 @@
module go.digitalcircle.com.br/open/httpcli
go 1.17

138
lib.go 100644
View File

@ -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
}