-
Notifications
You must be signed in to change notification settings - Fork 8
/
simpleclient.go
131 lines (118 loc) · 3.2 KB
/
simpleclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package httpsimple
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/grokify/mogo/io/ioutil"
"github.com/grokify/mogo/net/urlutil"
)
var rxHTTPURL = regexp.MustCompile(`^(?i)https?://`)
// Client provides a simple interface to making HTTP requests using `net/http`.
type Client struct {
BaseURL string
Query url.Values // Add If Not Exists
HTTPClient *http.Client
}
func NewClient(httpClient *http.Client, baseURL string) Client {
return Client{HTTPClient: httpClient, BaseURL: baseURL}
}
func (sc *Client) Get(reqURL string) (*http.Response, error) {
return sc.Do(Request{Method: http.MethodGet, URL: reqURL})
}
func (sc *Client) Do(req Request) (*http.Response, error) {
req.Inflate()
var bodyReader io.Reader
if req.BodyType == BodyTypeFile && ioutil.IsReader(req.Body) {
if reqBodyReader, ok := req.Body.(io.Reader); ok {
bodyReader = reqBodyReader
} else {
panic("cannot cast `io.Reader` as `io.Reader`")
}
} else {
if bodyBytes, err := req.BodyBytes(); err != nil {
return nil, err
} else {
bodyReader = bytes.NewReader(bodyBytes)
}
}
reqURL := strings.TrimSpace(req.URL)
if len(sc.BaseURL) > 0 {
if len(reqURL) == 0 {
reqURL = sc.BaseURL
} else if !rxHTTPURL.MatchString(reqURL) {
reqURL = urlutil.JoinAbsolute(sc.BaseURL, reqURL)
}
}
qry := urlutil.AppendValues(req.Query, sc.Query, false)
if len(qry) > 0 {
goURL, err := urlutil.URLStringAddQuery(reqURL, qry, false)
if err != nil {
return nil, err
}
reqURL = goURL.String()
}
if sc.HTTPClient == nil {
sc.HTTPClient = &http.Client{}
}
return doSimple(sc.HTTPClient, req.Method, reqURL, req.Headers, bodyReader)
}
func (sc *Client) DoUnmarshalJSON(req Request, resBody any) ([]byte, *http.Response, error) {
resp, err := sc.Do(req)
if err != nil {
return []byte{}, nil, err
}
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return bytes, resp, err
}
err = json.Unmarshal(bytes, resBody)
return bytes, resp, err
}
func doSimple(client *http.Client, httpMethod, reqURL string, headers map[string][]string, body io.Reader) (*http.Response, error) {
// func doSimple(client *http.Client, httpMethod, reqURL string, headers map[string][]string, body []byte) (*http.Response, error) {
reqURL = strings.TrimSpace(reqURL)
if len(reqURL) == 0 {
return nil, errors.New("requrl is required but not present")
}
if client == nil {
client = &http.Client{}
}
httpMethod = strings.TrimSpace(httpMethod)
if httpMethod == "" {
return nil, errors.New("httpMethod is required but not present")
}
//var req *http.Request
//var err error
//if len(body) == 0 {
req, err := http.NewRequest(httpMethod, reqURL, body)
//} else {
// req, err = http.NewRequest(httpMethod, reqURL, bytes.NewBuffer(body))
//}
if err != nil {
return nil, err
}
for k, vals := range headers {
for _, v := range vals {
req.Header.Set(k, v)
}
}
return client.Do(req)
}
func Do(req Request) (*http.Response, error) {
sc := Client{}
return sc.Do(req)
}
func DoMore(req Request) ([]byte, *http.Response, error) {
sc := Client{}
resp, err := sc.Do(req)
if err != nil {
return []byte{}, resp, err
}
body, err := io.ReadAll(resp.Body)
return body, resp, err
}