-
Notifications
You must be signed in to change notification settings - Fork 0
/
TelegaPi.Core.Methods.pas
149 lines (127 loc) · 4.04 KB
/
TelegaPi.Core.Methods.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
unit TelegaPi.Core.Methods;
interface
uses
TelegaPi.Methods,
Citrus.Mandarin,
System.SysUtils,
TelegaPi.Types;
type
TtgMethod<T> = class(TInterfacedObject, ITgMethod<T>)
private const
BOT_URL = '{server}/bot{token}/{METHOD_NAME}'; // do not localize
private
FClient: TMandarinClientJson;
FMandarin: IMandarin;
protected
function BuildMandarin(const AMethod: string): IMandarinExt;
procedure InternExec<TR>(AResponse: TProc<TR, IHttpResponse>);
public
constructor Create(AMandarin: TMandarinClientJson); virtual;
function GetMandarin: IMandarin;
end;
TtgGetMeMethod = class(TtgMethod<ItgUser>, ITgGetMeMethod)
public
constructor Create(AMandarin: TMandarinClientJson); override;
procedure Excecute(AResponse: TProc<ItgUser, IHttpResponse>);
end;
TtgGetUpdatesMethod = class(TtgMethod<ITgGetUpdatesMethod>, ITgGetUpdatesMethod)
public
function SetOffset(const AOffset: Integer): ITgGetUpdatesMethod;
function SetLimit(const ALimit: Integer): ITgGetUpdatesMethod;
function SetTimeout(const ATimeout: Integer): ITgGetUpdatesMethod;
constructor Create(AMandarin: TMandarinClientJson); override;
procedure Excecute(AResponse: TProc<TArray<ItgUpdate>, IHttpResponse>);
end;
implementation
uses
System.JSON, Citrus.JObject, TelegaPi.Core.Types,
System.Generics.Collections;
{ TtgMethod<T> }
constructor TtgMethod<T>.Create(AMandarin: TMandarinClientJson);
begin
inherited Create;
FClient := AMandarin;
end;
function TtgMethod<T>.GetMandarin: IMandarin;
begin
Result := FMandarin;
end;
procedure TtgMethod<T>.InternExec<TR>(AResponse: TProc<TR, IHttpResponse>);
begin
FClient.Execute(GetMandarin,
procedure(AHttp: IHttpResponse)
var
LRawData: string;
LResponse: TJSONObject;
begin
LRawData := AHttp.ContentAsString();
LResponse := TJSONObject.ParseJSONValue(LRawData) as TJSONObject;
try
if LResponse.GetValue('ok').Value = 'true' then
AResponse(TJObjectConfig.Current.Serializer.Deserialize<TR>(LResponse.GetValue('result').ToJSON), AHttp)
else
raise Exception.CreateFmt('%s: %d = %s', [FMandarin.GetUrlSegment('METHOD_NAME'),
LResponse.GetValue('error_code').Value, LResponse.GetValue('description').Value]);
finally
LResponse.Free;
end;
end, True);
end;
function TtgMethod<T>.BuildMandarin(const AMethod: string): IMandarinExt;
begin
Result := FClient.NewMandarin(BOT_URL)//
.AddUrlSegment('METHOD_NAME', AMethod);
end;
constructor TtgGetMeMethod.Create(AMandarin: TMandarinClientJson);
begin
inherited Create(AMandarin);
FMandarin := BuildMandarin('getMe');
end;
{ TtgGetMeMethod }
procedure TtgGetMeMethod.Excecute(AResponse: TProc<ItgUser, IHttpResponse>);
begin
InternExec<TTgUser>(
procedure(AData: TTgUser; AHttp: IHttpResponse)
begin
AResponse(AData, AHttp);
end);
end;
{ TtgGetUpdatesMethod }
constructor TtgGetUpdatesMethod.Create(AMandarin: TMandarinClientJson);
begin
inherited Create(AMandarin);
FMandarin := BuildMandarin('getUpdates');
end;
procedure TtgGetUpdatesMethod.Excecute(AResponse: TProc<TArray<ItgUpdate>, IHttpResponse>);
begin
InternExec < TArray < TtgUpdate >> (
procedure(AData: TArray<TtgUpdate>; AHttp: IHttpResponse)
var
LUpdates: TList<ItgUpdate>;
begin
LUpdates := TList<ItgUpdate>.Create;
try
for var LUpdate in AData do
LUpdates.Add(LUpdate);
AResponse(LUpdates.ToArray, AHttp);
finally
LUpdates.Free;
end;
end);
end;
function TtgGetUpdatesMethod.SetLimit(const ALimit: Integer): ITgGetUpdatesMethod;
begin
GetMandarin.AddQueryParameter('limit', ALimit.ToString);
Result := Self;
end;
function TtgGetUpdatesMethod.SetOffset(const AOffset: Integer): ITgGetUpdatesMethod;
begin
GetMandarin.AddQueryParameter('offset', AOffset.ToString);
Result := Self;
end;
function TtgGetUpdatesMethod.SetTimeout(const ATimeout: Integer): ITgGetUpdatesMethod;
begin
GetMandarin.AddQueryParameter('timeout', ATimeout.ToString);
Result := Self;
end;
end.