-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpCall.go
144 lines (114 loc) · 3.21 KB
/
httpCall.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
package gocally
import (
"encoding/json"
"fmt"
"net/http"
)
type HttpCall struct {
requestItems *RequestItems
response HandlerInterface
}
func SetRequest() *HttpCall {
return &HttpCall{
requestItems: PrepareRequestItems(),
}
}
func (h *HttpCall) WithUrl(url string) *HttpCall {
h.requestItems.url.payload = url
return h
}
func (h *HttpCall) SetAuthorization(token string) *HttpCall {
if len(h.requestItems.header.payload) == 0 {
h.requestItems.header.payload = map[string]string{"Authorization": token}
} else {
h.requestItems.header.payload["Authorization"] = token
}
return h
}
func (h *HttpCall) SetHeader(key string, value string) *HttpCall {
if len(h.requestItems.header.payload) == 0 {
h.requestItems.header.payload = map[string]string{key: value}
} else {
h.requestItems.header.payload[key] = value
}
return h
}
func (h *HttpCall) SetHeaderBulk(headers map[string]string) *HttpCall {
if len(h.requestItems.header.payload) == 0 {
h.requestItems.header.payload = headers
} else {
for key, value := range headers {
h.requestItems.header.payload[key] = value
}
}
return h
}
func (h *HttpCall) DisableJsonHeaders() *HttpCall {
h.requestItems.header.jsonHeaders = true
return h
}
func (h *HttpCall) SetQueryParam(key string, value string) *HttpCall {
h.requestItems.query.payload = QueryParamJoinSign(h.requestItems.query.payload)
h.requestItems.query.payload += fmt.Sprintf("%s=%s", key, value)
return h
}
func (h *HttpCall) SetQueryParamBulk(query map[string]string) *HttpCall {
h.requestItems.query.payload = QueryParamJoinSign(h.requestItems.query.payload)
setAmpersand, queryCount, loopCount := false, len(query), 0
if queryCount > 1 {
setAmpersand = true
}
for key, value := range query {
loopCount++
h.requestItems.query.payload += fmt.Sprintf("%s=%s", key, value)
if setAmpersand && (loopCount < queryCount) {
h.requestItems.query.payload += "&"
}
}
return h
}
func (h *HttpCall) SetBody(payload any) *HttpCall {
err := json.NewEncoder(&h.requestItems.body.payload).Encode(payload)
if err != nil {
h.requestItems.body.error = err
}
return h
}
func (h *HttpCall) SetForm(key string, value string) *HttpCall {
if len(h.requestItems.form.payload) == 0 {
h.requestItems.form.payload = map[string]string{key: value}
} else {
h.requestItems.form.payload[key] = value
}
return h
}
func (h *HttpCall) SetFormBulk(payload map[string]string) *HttpCall {
if len(h.requestItems.form.payload) == 0 {
h.requestItems.form.payload = payload
} else {
for key, value := range payload {
h.requestItems.form.payload[key] = value
}
}
return h
}
func (h *HttpCall) SetRequestTimeout(timeoutInSec int) *HttpCall {
h.requestItems.timeout.payload = timeoutInSec
return h
}
func (h *HttpCall) Get() HandlerInterface {
h.requestItems.method = http.MethodGet
return h.requestItems.SendRequest()
}
func (h *HttpCall) Post() HandlerInterface {
h.requestItems.method = http.MethodPost
return h.requestItems.SendRequest()
}
func (h *HttpCall) Put() HandlerInterface {
h.requestItems.method = http.MethodPut
return h.requestItems.SendRequest()
}
func (h *HttpCall) Delete() HandlerInterface {
h.requestItems.method = http.MethodDelete
return h.requestItems.SendRequest()
}