-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathceosclient.pas
159 lines (119 loc) · 3.79 KB
/
ceosclient.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
{*****************************************************************}
{ ceosclient is part of Ceos middleware/n-tier JSONRPC components }
{ }
{ Beta version }
{ }
{ This library is distributed in the hope that it will be useful, }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. }
{ }
{ by Jose Benedito - [email protected] }
{ www.jbsolucoes.net }
{*****************************************************************}
unit ceosclient;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics,
fpjson, jsonparser, fphttpclient, ceostypes;
type
{ TCeosClient }
TCeosClient = class(TComponent)
private
FHost: string;
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function Call(AMethod: string; Args: array of variant; AID: integer): string; overload;
function Call(ARequest: TCeosRequestContent): string; overload;
function JSONCall(AMethod: string; Args: array of variant; AID: integer): TJSONObject;
function JSONCall(ARequest: TCeosRequestContent): TJSONObject; overload;
published
property Host: string read FHost write FHost;
{ Published declarations }
end;
procedure Register;
implementation
uses ceosconsts;
procedure Register;
begin
{$I ceosclient_icon.lrs}
RegisterComponents('Ceos',[TCeosClient]);
end;
{ TCeosClient }
constructor TCeosClient.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
end;
destructor TCeosClient.Destroy;
begin
inherited Destroy;
end;
function TCeosClient.Call(AMethod: string; Args: array of variant; AID: integer): string;
var
response: TMemoryStream;
joContent: TJSONObject;
joArgs: TJSONArray;
slRes: TStringList;
httpcli: TFPHTTPClient;
begin
try
httpcli := TFPHTTPClient.Create(self);
slRes := TStringList.Create;
response := TMemoryStream.Create;
joContent := TJSONObject.Create;
joContent.Add('jsonrpc', JSONRPC_VERSION);
joContent.Add('method', AMethod);
joArgs := GetJSONArray(Args);
joContent.Add('params', joArgs);
joContent.Add('id',AID);
httpcli.FormPost(Host,joContent.AsJSON, response);
if (response <> nil) then
begin
response.Seek(0, soFromBeginning);
slRes.LoadFromStream(response);
Result := slRes.text;
end;
finally
joContent.free;
freeandnil(response);
freeandnil(slRes);
freeandnil(httpcli);
end;
end;
function TCeosClient.Call(ARequest: TCeosRequestContent): string;
var
response: TMemoryStream;
slRes: TStringList;
httpcli: TFPHTTPClient;
begin
try
httpcli := TFPHTTPClient.Create(self);
slRes := TStringList.Create;
response := TMemoryStream.Create;
httpcli.FormPost(Host,ARequest.AsJSON, response);
if (response <> nil) then
begin
response.Seek(0, soFromBeginning);
slRes.LoadFromStream(response);
Result := slRes.text;
end;
finally
freeandnil(response);
freeandnil(slRes);
freeandnil(httpcli);
end;
end;
function TCeosClient.JSONCall(AMethod: string; Args: array of variant; AID: integer): TJSONObject;
begin
Result := (TJSONParser.Create(Call(AMethod,Args,AID)).Parse as TJSONObject);
end;
function TCeosClient.JSONCall(ARequest: TCeosRequestContent): TJSONObject;
begin
Result := (TJSONParser.Create(Call(ARequest)).Parse as TJSONObject);
end;
end.