-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch.go
183 lines (158 loc) · 3.67 KB
/
fetch.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package api
import (
"encoding/xml"
"errors"
"fmt"
"net/http"
"reflect"
"strings"
"time"
"github.com/ajg/form"
json "github.com/pquerna/ffjson/ffjson"
"github.com/valyala/fasthttp"
"golang.org/x/sync/syncmap"
)
var methods syncmap.Map
func init() {
methods.Store(http.MethodGet, true)
methods.Store(http.MethodPost, true)
methods.Store(http.MethodPatch, true)
methods.Store(http.MethodPut, true)
methods.Store(http.MethodConnect, true)
methods.Store(http.MethodHead, true)
methods.Store(http.MethodTrace, true)
methods.Store(http.MethodDelete, true)
methods.Store(http.MethodOptions, true)
}
// Option :
type Option struct {
ContentType string
Method string
Headers map[string]string
Body interface{}
}
var (
defaultOption = Option{
ContentType: ContentTypeJSON,
Method: http.MethodGet,
}
)
// Client :
type Client struct {
method string
headers *fasthttp.RequestHeader
contentType string
timeTaken time.Duration
body []byte
err error
}
// Fetch :
func Fetch(url string, option ...Option) *Client {
startAt := time.Now()
c := new(Client)
c.method = http.MethodGet
c.contentType = ContentTypeJSON
c.headers = new(fasthttp.RequestHeader)
request := fasthttp.AcquireRequest()
response := fasthttp.AcquireResponse()
defer func() {
fasthttp.ReleaseRequest(request)
fasthttp.ReleaseResponse(response)
c.timeTaken = time.Since(startAt)
}()
if err := validateURL(url); err != nil {
c.err = err
return c
}
c.headers.SetRequestURI(url)
opt := defaultOption
if len(option) > 0 {
opt = option[0]
}
opt.Method = strings.TrimSpace(opt.Method)
opt.ContentType = strings.TrimSpace(opt.ContentType)
if opt.Method != "" {
c.method = strings.ToUpper(opt.Method)
}
if opt.ContentType != "" {
c.contentType = strings.ToLower(opt.ContentType)
}
if _, isOk := methods.Load(c.method); !isOk {
c.err = fmt.Errorf("api: invalid request method %q", c.method)
return c
}
c.headers.SetMethod(c.method)
c.headers.SetContentType(c.contentType)
for key, value := range opt.Headers {
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)
c.headers.Add(key, value)
}
c.headers.CopyTo(&request.Header)
if opt.Body != nil {
bb, err := c.toByte(opt.Body)
if err != nil {
c.err = err
return c
}
request.SetBody(bb)
}
if err := fasthttp.Do(request, response); err != nil {
c.err = err
return c
}
c.body = response.Body()
return c
}
func (c *Client) toByte(i interface{}) ([]byte, error) {
switch string(c.contentType) {
case ContentTypeXML:
return xml.Marshal(i)
case ContentTypeXWWWFormURLEncoded:
str, err := form.EncodeToString(i)
return []byte(str), err
default:
return json.Marshal(i)
}
}
// ToString :
func (c *Client) ToString() (string, error) {
if c.err != nil {
return "", c.err
}
return string(c.body), nil
}
// ToXML :
func (c *Client) ToXML(i interface{}) error {
if c.err != nil {
return c.err
}
var (
errUnableUnmarshalXML = errors.New("api: unable to unmarshal the xml")
errStructShouldPointer = errors.New("api: struct should be pointer")
)
if reflect.ValueOf(i).Kind() != reflect.Ptr {
return errStructShouldPointer
}
if err := xml.Unmarshal(c.body, i); err != nil {
return errUnableUnmarshalXML
}
return nil
}
// ToJSON :
func (c *Client) ToJSON(i interface{}) error {
var (
errUnableUnmarshalJSON = errors.New("api: unable to unmarshal the json")
errStructShouldPointer = errors.New("api: struct should be pointer")
)
if c.err != nil {
return c.err
}
if reflect.ValueOf(i).Kind() != reflect.Ptr {
return errStructShouldPointer
}
if err := json.Unmarshal(c.body, i); err != nil {
return errUnableUnmarshalJSON
}
return nil
}