forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.json.rpc.https.pas
159 lines (143 loc) · 3.93 KB
/
web3.json.rpc.https.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
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2018 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under Creative Commons NonCommercial (aka CC BY-NC) license. }
{ }
{******************************************************************************}
unit web3.json.rpc.https;
{$I web3.inc}
interface
uses
// Delphi
System.JSON,
// web3
web3,
web3.http.throttler,
web3.json.rpc;
type
TJsonRpcHttps = class(TCustomJsonRpc)
strict private
FThrottler: IThrottler;
public
function Send(
const URL : string;
security : TSecurity;
const method: string;
args : array of const): TJsonObject; overload; override;
procedure Send(
const URL : string;
security : TSecurity;
const method: string;
args : array of const;
callback : TAsyncJsonObject); overload; override;
constructor Create; overload;
constructor Create(const throttler: IThrottler); overload;
end;
implementation
uses
// Delphi
System.Classes,
System.Net.URLClient,
// web3
web3.http,
web3.json;
{ TJsonRpcHttps }
constructor TJsonRpcHttps.Create;
begin
inherited Create;
end;
constructor TJsonRpcHttps.Create(const throttler: IThrottler);
begin
inherited Create;
FThrottler := throttler;
end;
function TJsonRpcHttps.Send(
const URL : string;
security : TSecurity;
const method: string;
args : array of const): TJsonObject;
var
source: TStream;
resp : TJsonValue;
error : TJsonObject;
begin
Result := nil;
source := TStringStream.Create(GetPayload(method, args));
try
web3.http.post(
URL,
source,
[TNetHeader.Create('Content-Type', 'application/json')],
resp
);
if Assigned(resp) then
try
// did we receive an error? then translate that into an exception
error := web3.json.getPropAsObj(resp, 'error');
if Assigned(error) then
raise EJsonRpc.Create(
web3.json.getPropAsInt(error, 'code'),
web3.json.getPropAsStr(error, 'message')
);
Result := resp.Clone as TJsonObject;
finally
resp.Free;
end;
finally
source.Free;
end;
end;
procedure TJsonRpcHttps.Send(
const URL : string;
security : TSecurity;
const method: string;
args : array of const;
callback : TAsyncJsonObject);
var
handler: TAsyncJsonObject;
payload: string;
headers: TNetHeaders;
source : TStream;
begin
handler := procedure(resp: TJsonObject; err: IError)
var
error: TJsonObject;
begin
if Assigned(err) then
begin
callback(nil, err);
EXIT;
end;
// did we receive an error?
error := web3.json.getPropAsObj(resp, 'error');
if Assigned(error) then
callback(resp, TJsonRpcError.Create(
web3.json.getPropAsInt(error, 'code'),
web3.json.getPropAsStr(error, 'message')
))
else
// if we reached this far, then we have a valid response object
callback(resp, nil);
end;
payload := GetPayload(method, args);
headers := [TNetHeader.Create('Content-Type', 'application/json')];
if Assigned(FThrottler) then
begin
FThrottler.Post(TPost.Create(URL, payload, headers, handler));
EXIT;
end;
source := TStringStream.Create(payload);
web3.http.post(URL, source, headers, procedure(resp: TJsonObject; err: IError)
begin
try
handler(resp, err);
finally
source.Free;
end;
end);
end;
end.