httpcli/lib.go

150 lines
3.3 KiB
Go
Raw Normal View History

2021-10-09 13:34:41 +00:00
package httpcli
import (
"bytes"
"encoding/json"
2021-10-26 17:29:50 +00:00
"errors"
2021-10-09 13:34:41 +00:00
"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]
}
2021-10-09 13:38:14 +00:00
if !strings.HasPrefix(strurl, "/") {
2021-10-09 13:34:41 +00:00
strurl = "/" + strurl
}
strurl = addr + strurl
}
2021-10-09 13:43:06 +00:00
var req *http.Request
var err error
2021-10-09 13:34:41 +00:00
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
}
2021-10-09 13:43:06 +00:00
if c.headers != nil {
for k, v := range c.headers {
req.Header.Set(k, v)
}
}
2021-10-26 16:45:45 +00:00
res,err:= c.cli.Do(req)
if err==nil && res.StatusCode >=400{
return nil, errors.New("Http return code - %d: %s",res.StatusCode,res.Status)
}
return res,err
2021-10-09 13:34:41 +00:00
}
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
}
2021-10-10 23:54:58 +00:00
var cli *Cli
func C() *Cli {
if cli == nil {
cli = NewCli()
}
return cli
}