-
Notifications
You must be signed in to change notification settings - Fork 3
/
RestUtils.pas
472 lines (369 loc) · 11.1 KB
/
RestUtils.pas
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//https://github.com/fabriciocolombo/delphi-rest-client-api
unit RestUtils;
interface
uses
{$IFDEF HAS_UNIT_NETENCODING}
System.NetEncoding, //allows inlining of EncodeString, DecodeString
{$ENDIF}
EncdDecd, Data.DBXPlatform, FMX.Graphics, System.Classes, System.SysUtils;
const
MediaType_Json = 'application/json';
MediaType_Xml = 'text/xml';
LOCALE_PORTUGUESE_BRAZILIAN = 'pt-BR';
LOCALE_US = 'en-US';
type
TReponseCode = record
StatusCode: Integer;
Reason: string;
end;
(* See Status Code Definitions at http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html *)
TStatusCode = class
public
(* 100 CONTINUE *)
class function CONTINUE: TReponseCode;
(* 101 Switching Protocols *)
class function SWITCHING_PROTOCOLS: TReponseCode;
(* 200 OK *)
class function OK: TReponseCode;
(* 201 Created *)
class function CREATED: TReponseCode;
(* 202 Accepted *)
class function ACCEPTED: TReponseCode;
(* 203 Non-Authoritative Information *)
class function NON_AUTHORITATIVE_INFORMATION: TReponseCode;
(* 204 No Content *)
class function NO_CONTENT: TReponseCode;
(* 205 Reset Content *)
class function RESET_CONTENT: TReponseCode;
(* 206 Partial Content *)
class function PARTIAL_CONTENT: TReponseCode;
(* 208 Reserved Content *)
class function RESERVED_CONTENT: TReponseCode;
(* 300 Multiple Choices *)
class function MULTIPLE_CHOICES: TReponseCode;
(* 301 Moved Permanently *)
class function MOVED_PERMANENTLY: TReponseCode;
(* 302 Found *)
class function FOUND: TReponseCode;
(* 303 See Other *)
class function SEE_OTHER: TReponseCode;
(* 304 Not Modified *)
class function NOT_MODIFIED: TReponseCode;
(* 305 Use Proxy *)
class function USE_PROXY: TReponseCode;
(* 307 Temporary Redirect *)
class function TEMPORARY_REDIRECT: TReponseCode;
(* 400 Bad Request *)
class function BAD_REQUEST: TReponseCode;
(* 401 Unauthorized *)
class function UNAUTHORIZED: TReponseCode;
(* 402 Payment Required *)
class function PAYMENT_REQUIRED: TReponseCode;
(* 403 Forbidden *)
class function FORBIDDEN: TReponseCode;
(* 404 Not Found *)
class function NOT_FOUND: TReponseCode;
(* 405 Method Not Allowed *)
class function METHOD_NOT_ALLOWED: TReponseCode;
(* 406 Not Acceptable *)
class function NOT_ACCEPTABLE: TReponseCode;
(* 407 Proxy Authentication Required *)
class function PROXY_AUTHENTICATION_REQUIRED: TReponseCode;
(* 408 Request Timeout *)
class function REQUEST_TIMEOUT: TReponseCode;
(* 409 OK *)
class function CONFLICT: TReponseCode;
(* 410 Gone *)
class function GONE: TReponseCode;
(* 411 Length Required *)
class function LENGTH_REQUIRED: TReponseCode;
(* 412 Precondition Failed *)
class function PRECONDITION_FAILED: TReponseCode;
(* 413 Request Entity Too Large *)
class function REQUEST_ENTITY_TOO_LARGE: TReponseCode;
(* 414 Request-URI Too Long *)
class function REQUEST_URI_TOO_LONG: TReponseCode;
(* 415 Unsupported Media Type *)
class function UNSUPPORTED_MEDIA_TYPE: TReponseCode;
(* 416 Requested Range Not Satisfiable *)
class function REQUESTED_RANGE_NOT_SATISFIABLE: TReponseCode;
(* 417 Expectation Failed *)
class function EXPECTATION_FAILED: TReponseCode;
(* 422 Unprocessable Entity *)
class function UNPROCESSABLE_ENTITY: TReponseCode;
(* 500 Internal Server Error *)
class function INTERNAL_SERVER_ERROR: TReponseCode;
(* 501 Not Implemented *)
class function NOT_IMPLEMENTED: TReponseCode;
(* 502 Bad Gateway *)
class function BAD_GATEWAY: TReponseCode;
(* 503 Service Unavailable *)
class function SERVICE_UNAVAILABLE: TReponseCode;
(* 504 Gateway Timeout *)
class function GATEWAY_TIMEOUT: TReponseCode;
(* 505 HTTP Version Not Supported *)
class function HTTP_VERSION_NOT_SUPPORTED: TReponseCode;
end;
TRestUtils = class
public
class function Base64Encode(const AValue: String): String;
class function Base64Decode(const AValue: String): String;
class function BitmapFromBase64(const base64: string): TBitmap;
class procedure responseServer(const Response: TReponseCode; Content : string = '');
end;
implementation
{ TStatusCode }
class function TStatusCode.CONTINUE: TReponseCode;
begin
Result.StatusCode := 100;
Result.Reason := 'Continue';
end;
class function TStatusCode.SWITCHING_PROTOCOLS: TReponseCode;
begin
Result.StatusCode := 101;
Result.Reason := 'Switching Protocols';
end;
class function TStatusCode.OK: TReponseCode;
begin
Result.StatusCode := 200;
Result.Reason := 'OK';
end;
class function TStatusCode.Created: TReponseCode;
begin
Result.StatusCode := 201;
Result.Reason := 'Created';
end;
class function TStatusCode.ACCEPTED: TReponseCode;
begin
Result.StatusCode := 202;
Result.Reason := 'Accepted';
end;
class function TStatusCode.NON_AUTHORITATIVE_INFORMATION: TReponseCode;
begin
Result.StatusCode := 203;
Result.Reason := 'Non-Authoritative Information';
end;
class function TStatusCode.NO_CONTENT: TReponseCode;
begin
Result.StatusCode := 204;
Result.Reason := 'No Content';
end;
class function TStatusCode.RESET_CONTENT: TReponseCode;
begin
Result.StatusCode := 205;
Result.Reason := 'Reset Content';
end;
class function TStatusCode.PARTIAL_CONTENT: TReponseCode;
begin
Result.StatusCode := 206;
Result.Reason := 'Partial Content';
end;
class function TStatusCode.RESERVED_CONTENT: TReponseCode;
begin
Result.StatusCode := 208;
Result.Reason := 'reserved Content';
end;
class function TStatusCode.MULTIPLE_CHOICES: TReponseCode;
begin
Result.StatusCode := 300;
Result.Reason := 'Multiple Choices';
end;
class function TStatusCode.MOVED_PERMANENTLY: TReponseCode;
begin
Result.StatusCode := 301;
Result.Reason := 'Moved Permanently';
end;
class function TStatusCode.FOUND: TReponseCode;
begin
Result.StatusCode := 302;
Result.Reason := 'Found';
end;
class function TStatusCode.SEE_OTHER: TReponseCode;
begin
Result.StatusCode := 303;
Result.Reason := 'See Other';
end;
class function TStatusCode.NOT_MODIFIED: TReponseCode;
begin
Result.StatusCode := 304;
Result.Reason := 'Not Modified';
end;
class function TStatusCode.USE_PROXY: TReponseCode;
begin
Result.StatusCode := 305;
Result.Reason := 'Use Proxy';
end;
class function TStatusCode.TEMPORARY_REDIRECT: TReponseCode;
begin
Result.StatusCode := 307;
Result.Reason := 'Temporary Redirect';
end;
class function TStatusCode.BAD_REQUEST: TReponseCode;
begin
Result.StatusCode := 400;
Result.Reason := 'Bad Request';
end;
class function TStatusCode.UNAUTHORIZED: TReponseCode;
begin
Result.StatusCode := 401;
Result.Reason := 'Unauthorized';
end;
class function TStatusCode.PAYMENT_REQUIRED: TReponseCode;
begin
Result.StatusCode := 402;
Result.Reason := 'Payment Required';
end;
class function TStatusCode.FORBIDDEN: TReponseCode;
begin
Result.StatusCode := 403;
Result.Reason := 'Forbidden';
end;
class function TStatusCode.NOT_FOUND: TReponseCode;
begin
Result.StatusCode := 404;
Result.Reason := 'Not Found';
end;
class function TStatusCode.METHOD_NOT_ALLOWED: TReponseCode;
begin
Result.StatusCode := 405;
Result.Reason := 'Method Not Allowed';
end;
class function TStatusCode.NOT_ACCEPTABLE: TReponseCode;
begin
Result.StatusCode := 406;
Result.Reason := 'Not Acceptable';
end;
class function TStatusCode.PROXY_AUTHENTICATION_REQUIRED: TReponseCode;
begin
Result.StatusCode := 407;
Result.Reason := 'Proxy Authentication Required';
end;
class function TStatusCode.REQUEST_TIMEOUT: TReponseCode;
begin
Result.StatusCode := 408;
Result.Reason := 'Request Timeout';
end;
class function TStatusCode.CONFLICT: TReponseCode;
begin
Result.StatusCode := 409;
Result.Reason := 'Conflict';
end;
class function TStatusCode.GONE: TReponseCode;
begin
Result.StatusCode := 410;
Result.Reason := 'Gone';
end;
class function TStatusCode.LENGTH_REQUIRED: TReponseCode;
begin
Result.StatusCode := 411;
Result.Reason := 'Length Required';
end;
class function TStatusCode.PRECONDITION_FAILED: TReponseCode;
begin
Result.StatusCode := 412;
Result.Reason := 'Precondition Failed';
end;
class function TStatusCode.REQUEST_ENTITY_TOO_LARGE: TReponseCode;
begin
Result.StatusCode := 413;
Result.Reason := 'Request Entity Too Large';
end;
class function TStatusCode.REQUEST_URI_TOO_LONG: TReponseCode;
begin
Result.StatusCode := 414;
Result.Reason := 'Request-URI Too Long';
end;
class function TStatusCode.UNSUPPORTED_MEDIA_TYPE: TReponseCode;
begin
Result.StatusCode := 415;
Result.Reason := 'Unsupported Media Type';
end;
class function TStatusCode.REQUESTED_RANGE_NOT_SATISFIABLE: TReponseCode;
begin
Result.StatusCode := 416;
Result.Reason := 'Requested Range Not Satisfiable';
end;
class function TStatusCode.EXPECTATION_FAILED: TReponseCode;
begin
Result.StatusCode := 417;
Result.Reason := 'Expectation Failed';
end;
class function TStatusCode.UNPROCESSABLE_ENTITY: TReponseCode;
begin
Result.StatusCode := 422;
Result.Reason := 'Unprocessable Entity';
end;
class function TStatusCode.INTERNAL_SERVER_ERROR: TReponseCode;
begin
Result.StatusCode := 500;
Result.Reason := 'Internal Server Error';
end;
class function TStatusCode.NOT_IMPLEMENTED: TReponseCode;
begin
Result.StatusCode := 501;
Result.Reason := 'Not Implemented';
end;
class function TStatusCode.BAD_GATEWAY: TReponseCode;
begin
Result.StatusCode := 502;
Result.Reason := 'Bad Gateway';
end;
class function TStatusCode.SERVICE_UNAVAILABLE: TReponseCode;
begin
Result.StatusCode := 503;
Result.Reason := 'Service Unavailable';
end;
class function TStatusCode.GATEWAY_TIMEOUT: TReponseCode;
begin
Result.StatusCode := 504;
Result.Reason := 'Gateway Timeout';
end;
class function TStatusCode.HTTP_VERSION_NOT_SUPPORTED: TReponseCode;
begin
Result.StatusCode := 505;
Result.Reason := 'HTTP Version Not Supported';
end;
{ TRestUtils }
class function TRestUtils.Base64Decode(const AValue: String): String;
begin
Result := EncdDecd.DecodeString(AValue);
end;
class function TRestUtils.Base64Encode(const AValue: String): String;
begin
Result := EncdDecd.EncodeString(AValue);
end;
class function TRestUtils.BitmapFromBase64(const base64: string): TBitmap;
var
Input: TStringStream;
Output: TBytesStream;
begin
Input := TStringStream.Create(base64, TEncoding.ASCII);
try
Output := TBytesStream.Create;
try
EncdDecd.DecodeStream(Input, Output);
Output.Position := 0;
Result := TBitmap.Create;
try
Result.LoadFromStream(Output);
except
Result.Free;
raise;
end;
finally
Output.Free;
end;
finally
Input.Free;
end;
end;
class procedure TRestUtils.responseServer(const Response: TReponseCode; Content : string = '');
begin
GetInvocationMetadata.ResponseCode := Response.StatusCode;
GetInvocationMetadata.ResponseMessage := Response.Reason;
if Content = '' then
GetInvocationMetadata.ResponseContent := Response.Reason
else
GetInvocationMetadata.ResponseContent := '{"MENSAGEM":"ERRO CRÍTICO."}';
end;
end.