-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINCLUDE_HTTP.SMD
More file actions
257 lines (221 loc) · 6.84 KB
/
INCLUDE_HTTP.SMD
File metadata and controls
257 lines (221 loc) · 6.84 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
CONSTANTS begin
end
CLASSES BEGIN
cHttpConstants is Struct begin
GET_METHOD as Char default "GET"
POST_METHOD as Char default "POST"
PUT_METHOD as Char default "PUT"
PATCH_METHOD as Char default "PATCH"
DELETE_METHOD as Char default "DELETE"
HTTP_422_UNPROCESSABLE_ENTITY as Integer default 422
HTTP_424_FAILED_DEPENDENCY as Integer default 424
HTTP_400_BAD_REQUEST as Integer default 400
HTTP_200_OK as Integer default 200
HTTP_405_METHOD_NOT_ALLOWED as Integer default 405
HTTP_404_NOT_FOUND as Integer default 404
HTTP_503_SERVICE_UNAVAILABLE as Integer default 503
HTTP_401_UNAUTHORIZED as Integer default 401
HTTP_204_NO_CONTENT as Integer default 204
HTTP_201_CREATED as Integer default 201
HTTP_403_FORBIDDEN as Integer default 403
HTTP_500_INTERNAL_SERVER_ERROR as Integer default 500
end
cParameter is Struct begin
Key as Char
Value as Char
end
cDictionary is Array [1] of cParameter
cRequest is Struct begin
Url as Char
Method as Char (10)
private Headers as cDictionary
private QueryParameters as cDictionary
Body as JSON
ResponseText as Char
Response as JSON
StatusResponse as Integer
private timeElapsed as Decimal (6,3)
end
END
objects begin
HttpTypes AS cHttpConstants
end
CODE CLASS cParameter BEGIN
//{{CODEBEGIN
//{{CODEEND
END
CODE CLASS cDictionary BEGIN
//{{CODEBEGIN
public reset
begin
self.Resize( 0 );
end
public add( pchrKey as char, pchrValue as char ) return boolean
begin
if pchrKey is null or pchrKey.Trim().Length() == 0 then return false;
self.Resize(self.Size()+1);
self[self.Size].Key = pchrKey;
self[self.Size].Value = pchrValue;
return true;
end
//{{CODEEND
END
CODE CLASS cRequest BEGIN
//{{CODEBEGIN
{
Init method
}
public reset
begin
self.Url = null;
self.Method = null;
self.ResponseText = null;
self.Response = null;
self.StatusResponse = null;
self.timeElapsed = 0;
self.Headers.reset();
self.QueryParameters.reset();
self.Body.Clear();
end
{
Getters
}
public getTimeElapsed return decimal
begin
return (self.timeElapsed)/1000;
end
{
Setters
}
public addHeader( pchrKey as char, pchrValue as char ) return boolean
begin
return self.Headers.add( pchrKey, pchrValue );
end
public addQueryParameter( pchrKey as char, pchrValue as char ) return boolean
begin
return self.QueryParameters.add( pchrKey, pchrValue );
end
{
Get the complete url after include query parameters
}
public getQueryFormed() return char
objects
begin
intI as integer default 0
chrQueryFormed as char default ''
end
begin
if self.Url is null or self.Url.Length() == 0 then return null;
chrQueryFormed = self.Url;
if self.QueryParameters.Size() > 0 then
begin
if chrQueryFormed.Locate("?") == 0 then chrQueryFormed += "?";
if chrQueryFormed.Locate("&") > 0 then chrQueryFormed += "&";
for intI = 1 to self.QueryParameters.Size() do
begin
chrQueryFormed += self.QueryParameters[intI].Key +"="+ self.QueryParameters[intI].Value +"&";
end
chrQueryFormed = chrQueryFormed[1,chrQueryFormed.Length()-1];
end
return chrQueryFormed;
end
{
Create a header with Basic Auth Authentication method with user and password parameters received
}
public addBasicAuthHeader( pchrUser as char, pchrPass as char ) return boolean
objects
begin
chrHeader as char default ''
binString as binary
end
begin
if pchrUser is null or pchrUser.Trim().Length() == 0 or pchrPass is null or pchrPass.Trim().Length() == 0 then return false;
chrHeader = pchrUser +":"+ pchrPass;
binString.LoadFromChar( chrHeader );
binString.Encode64( chrHeader );
chrHeader = "Basic "+chrHeader;
self.addHeader( "Authorization", chrHeader );
return true;
end
{
Send the request and store the result into object properties
}
public send return boolean
objects
begin
chrQuery as char default ''
intI as integer default 0
axHttp as activex
blnResult as boolean default false
decSecsInit as decimal
end
begin
axHttp.CreateActiveXFromProgID("Msxml2.ServerXMLHTTP.6.0");
if self.Url is null or self.Url.Length() == 0 then return false;
chrQuery = getQueryFormed();
axHttp.Invoke( "open", // ActiveX Method
null, // Variable to store value return. In this case null
self.Method, // Http Method
chrQuery, // Url target
false); // false = synchronous
if self.Headers.Size() > 0 then
begin
for intI = 1 to self.Headers.Size() do
begin
axHttp.Invoke("setRequestHeader",null,self.Headers[intI].Key,self.Headers[intI].Value);
end
end
decSecsInit = (GetTickCount64);
blnResult = axHttp.Invoke("send",null,self.Body.Char);
self.timeElapsed = ( (GetTickCount64) - decSecsInit );
axHttp.GetProperty("responseText",self.ResponseText);
axHttp.GetProperty("status",self.StatusResponse);
self.Response.LoadFromChar( self.ResponseText );
fixMSXMLHTTPBugs( blnResult );
return blnResult;
end
{
A extra method to nest the response with other request properties
}
public getResponseWithInfo return Json
objects
begin
jsnInfo as Json
end
begin
jsnInfo.Set( "timeElapsed", self.getTimeElapsed().Char );
jsnInfo.Set( "status", self.StatusResponse );
if self.Response is not null then jsnInfo.Set( "response", self.Response );
else jsnInfo.Set( "response", "null" );
return jsnInfo;
end
{
More info about this bug:
- https://bugs.dojotoolkit.org/ticket/2418
- https://stackoverflow.com/questions/10046972/msie-returns-status-code-of-1223-for-ajax-request
}
private fixMSXMLHTTPBugs( VAR pblnResult as boolean )
begin
if self.StatusResponse == 1223 then
begin
self.StatusResponse = 204;
pblnResult = true;
end
end
//{{CODEEND
END
CODE BEGIN
//{{CODEBEGIN
{+--------------- GNU General Public License v3.0 ------------------+
| |
| Modulo: INCLUDE_HTTP |
| Autor: David Ropero |
| Fecha: Septembre 2017 |
| |
+-----------------------------------------------------------------+}
{
Function API used to calculate elapsed time during the request.
}
Public dll "kernel32" GetTickCount64() return integer
//{{CODEEND
END