-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp_request.lasso
204 lines (173 loc) · 6.32 KB
/
http_request.lasso
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
define http_request => type {
data
public curl,
public urlProtocol ::string = '',
public urlHostname ::string = '',
public urlPath ::string = '',
public username ::string = '',
public password ::string = '',
public basicAuthOnly ::boolean = false,
public queryParams ::array = array,
public postParams,
public headers ::array = array,
public sslNoVerify ::boolean = false,
public sslCert,
public sslCertType,
public sslKey,
public sslKeyType,
public sslKeyPasswd,
public timeout,
public connectTimeout,
public method,
public options
public onCreate() => { .onCreate('') }
public onCreate(
url::string,
-username::string='',
-password::string='',
-basicAuthOnly::boolean=false,
-queryParams::trait_forEach=array,
-postParams=void,
-headers::trait_forEach=array,
-sslNoVerify::boolean=false,
-sslCert=void,
-sslCertType=void,
-sslKey=void,
-sslKeyType=void,
-sslKeyPasswd=void,
-timeout=void,
-connectTimeout=void,
-method::string='',
-options::trait_forEach=(:)
) => {
#queryParams->isNotA(::array)
? #queryParams->hasMethod(::asArray)
? #queryParams = #queryParams->asArray
| #queryParams = (with elm in #queryParams select #elm)->asStaticArray->asArray
#headers->isNotA(::array)
? #headers->hasMethod(::asArray)
? #headers = #headers->asArray
| #headers = (with elm in #headers select #elm)->asStaticArray->asArray
.queryParams = #queryParams
.postParams = #postParams
.username = #username
.password = #password
.basicAuthOnly = #basicAuthOnly
.headers = #headers
.sslNoVerify = #sslNoVerify
.sslCert = #sslCert
.sslCertType = #sslCertType
.sslKey = #sslKey
.sslKeyType = #sslKeyType
.sslKeyPasswd = #sslKeyPasswd
.timeout = #timeout
.connectTimeout = #connectTimeout
.method = #method
.options = #options->asStaticArray
// URL must go last (or at least after queryParams)
.url = #url
}
public url => .urlProtocol + .urlHostname + .urlPath + .queryParamsString
public url=(value::string) => {
// split up the different parts
local(copy) = #value->asCopy
if(#copy->beginsWith(`https://`)) => {
.urlProtocol = `https://`
#copy->remove(1,8)
else
.urlProtocol = `http://`
#copy->beginsWith(`http://`)
? #copy->remove(1,7)
}
local(path_start) = #copy->find(`/`)
local(param_start) = #copy->find(`?`)
if(#path_start == 0) => {
.urlHostname = #copy
.urlPath = ``
return #value
else
.urlHostname = #copy->sub(1, #path_start-1)
}
if(#param_start == 0) => {
.urlPath = #copy->sub(#path_start)
return #value
else
.urlPath = #copy->sub(#path_start, #param_start-#path_start)
}
// Prepend any GET parameters
local(params) = array
with item in #copy->sub(#param_start+1)->split(`&`)
let param = #item->split(`=`)
do #params->insert(#param->first=#param->second)
.queryParams = #params + .queryParams
return #value
}
public queryParamsString::string => {
.queryParams->size == 0
? return ``
return '?' + (
with elm in .queryParams
select #elm->first->asString->asBytes->encodeUrl + '=' + #elm->second->asString->asBytes->encodeUrl
)->join(`&`)
}
public response => {
.curl->isNotA(::curl)? .makeRequest
return http_response(.curl->raw)
}
// Code adapted from include_url
public makeRequest => {
fail_if(.urlHostname == '', `No URL specified`)
local(curl) = curl(.url)
// Set cURL authentication options
if(.username != '') => {
#curl->set(CURLOPT_USERPWD, .username + ':' + .password)
.basicAuthOnly
? #curl->set(CURLOPT_HTTPAUTH, CURLAUTH_BASIC)
| #curl->set(CURLOPT_HTTPAUTH, CURLAUTH_ANY)
}
// Set cURL postParams
if(.postParams->isA(::trait_forEach)) => {
#curl->set(CURLOPT_POSTFIELDS,
(
with param in .postParams
select #param->first->asString->asBytes->encodeUrl + '=' + #param->second->asString->asBytes->encodeUrl
)->join('&')
)
else(.postParams->isA(::string) or .postParams->isA(::bytes))
#curl->set(CURLOPT_POSTFIELDS, .postParams)
}
// Prepare headers
#curl->set(CURLOPT_HTTPHEADER,
(
with item in .headers
let header = (#item->isA(::pair) ? #item->first + `: ` + #item->second | #item->asString)
select #header
)->asStaticArray
)
// SSL Options
#curl->set(CURLOPT_SSL_VERIFYPEER, not .sslNoVerify)
.sslCert?
#curl->set(CURLOPT_SSLCERT, string(.sslCert))
.sslCertType?
#curl->set(CURLOPT_SSLCERTTYPE, string(.sslCertType))
.sslKey?
#curl->set(CURLOPT_SSLKEY, string(.sslKey))
.sslKeyType?
#curl->set(CURLOPT_SSLKEYTYPE, string(.sslKeyType))
.sslKeyPasswd?
#curl->set(CURLOPT_SSLKEYPASSWD, string(.sslKeyPasswd))
// Timeout Options
.timeout?
#curl->set(CURLOPT_TIMEOUT, integer(.timeout))
.connectTimeout?
#curl->set(CURLOPT_CONNECTTIMEOUT, integer(.connectTimeout))
// HTTP Request Method
.method->size > 0
? #curl->set(CURLOPT_CUSTOMREQUEST, .method)
// These options will override anything already set
with option in .options
where #option->isA(::pair)
do #curl->set(#option->first, #option->second)
.`curl` = #curl
}
}