forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb3.json.rpc.https.pas
More file actions
140 lines (124 loc) · 3.5 KB
/
web3.json.rpc.https.pas
File metadata and controls
140 lines (124 loc) · 3.5 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
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2018 Stefan van As <svanas@runbox.com> }
{ 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, IJsonRpc)
strict private
FThrottler: IThrottler;
public
function Call(
const URL : string;
const method: string;
args : array of const): TJsonObject; overload;
procedure Call(
const URL : string;
const method: string;
args : array of const;
callback : TAsyncJsonObject); overload;
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.Call(
const URL : string;
const method: string;
args : array of const): TJsonObject;
var
resp : TJsonValue;
error : TJsonObject;
begin
Result := nil;
web3.http.post(
URL,
CreatePayload(method, args),
[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;
end;
procedure TJsonRpcHttps.Call(
const URL : string;
const method: string;
args : array of const;
callback : TAsyncJsonObject);
var
handler: TAsyncJsonObject;
payload: string;
headers: TNetHeaders;
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 := CreatePayload(method, args);
headers := [TNetHeader.Create('Content-Type', 'application/json')];
if Assigned(FThrottler) then
begin
FThrottler.Post(TPost.Create(URL, payload, headers, handler));
EXIT;
end;
web3.http.post(URL, payload, headers, handler);
end;
end.