-
Notifications
You must be signed in to change notification settings - Fork 6
/
RestClient_u.pas
171 lines (144 loc) · 3.52 KB
/
RestClient_u.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
unit RestClient_u;
interface
uses
DBXJSON, JSON, System.Classes, System.SysUtils,
FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
System.Net.URLClient, System.Net.HttpClient,
System.Net.HttpClientComponent;
type
TRequest = class
uri : String;
vToken : String;
body : TStringList;
headers : TStringList;
params : TStringList;
public
procedure Clear ;
constructor Create ;
destructor Destroy ;
end;
TRestClient = Class ( TComponent )
private
NetHTTP : TNetHTTPClient;
function armarParametros( sl : TStringList ) : String ;
protected
constructor Create ; virtual ;
destructor Destroy ;
public
sURLDominio : String ;
class function Get( req : TRequest ) : String ;
class function Post( req : TRequest ) : String ;
class function Put( req : TRequest ) : String ;
End;
implementation
uses StrUtils ;
{ TRestClient }
function TRestClient.armarParametros(sl: TStringList): String;
var i : integer ;
begin
result := '';
for I := 0 to sl.Count - 1 do begin
result := result+sl[i];
if i<sl.Count-1 then
result := result + '&';
end;
end;
constructor TRestClient.Create;
var
req : TRequest;
begin
NetHTTP := TNetHTTPClient.Create(nil);
try
with NetHTTP do begin
Accept := 'application/json';
ContentType := 'application/json';
Asynchronous := False;
ConnectionTimeout := 60000;
ResponseTimeout := 60000;
HandleRedirects := True;
AllowCookies := False;
UserAgent := 'MercadoPago PHP SDK v0.5.2';
end;
except
on E: Exception do
Raise Exception.Create( 'Error: '+E.ClassName + ': ' +E.Message );
end;
end;
destructor TRestClient.Destroy;
begin
NetHTTP.Free ;
end;
class function TRestClient.Get(req: TRequest ): String;
var s : String ;
begin
With Self.Create do
try
try
s := sURLDominio + req.uri + armarParametros( req.params ) ;
result := NetHTTP.Get(s).ContentAsString(tencoding.UTF8);
except
on E: Exception do
Raise Exception.Create( 'Error: '+E.ClassName + ': ' +E.Message );
end;
finally
Free;
end;
end;
class function TRestClient.Post(req: TRequest): String;
var
sUrl : String ;
JsonToSend: TStringStream;
begin
With Self.Create do
try
try
sUrl := sURLDominio + req.uri + armarParametros( req.params ) ;
JsonToSend := TStringStream.Create(Utf8Encode(req.body.Text));
result := NetHTTP.Post(sUrl, JsonToSend ).ContentAsString(tencoding.UTF8);
except
on E: Exception do
Raise Exception.Create( 'Error: '+E.ClassName + ': ' +E.Message );
end;
finally
Free;
end;
end;
class function TRestClient.Put(req: TRequest): String;
var
sUrl : String ;
JsonToSend: TStringStream;
begin
With Self.Create do
try
try
sUrl := sURLDominio + req.uri + armarParametros( req.params ) ;
JsonToSend := TStringStream.Create(Utf8Encode(req.body.Text));
result := NetHTTP.Put(sUrl, JsonToSend ).ContentAsString(tencoding.UTF8);
except
on E: Exception do
Raise Exception.Create( 'Error: '+E.ClassName + ': ' +E.Message );
end;
finally
Free;
end;
end;
{ TRequest }
procedure TRequest.Clear;
begin
body.Clear ;
headers.Clear ;
params.Clear ;
end;
constructor TRequest.Create;
begin
body := TStringList.Create ;
headers := TStringList.Create ;
params := TStringList.Create ;
end;
destructor TRequest.Destroy;
begin
body.Free ;
headers.Free ;
params.Free ;
end;
end.