-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ksAwsSns.pas
315 lines (275 loc) · 9.89 KB
/
ksAwsSns.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
{*******************************************************************************
* *
* ksSNS - AWS SNS Interface *
* *
* https://github.com/gmurt/ksAWS *
* *
* Copyright 2020 Graham Murt *
* *
* email: graham@kernow-software.co.uk *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
*******************************************************************************}
unit ksAwsSns;
interface
uses
Sysutils, Classes, ksAwsBase, System.Generics.Collections, Xml.XMLIntf;
type
IksAwsSnsSubscription = interface
['{8640ABC9-89F5-407A-A60C-E3E9B4EA49E0}']
function GetEndpoint: string;
function GetOwner: string;
function GetProtocol: string;
function GetSubscriptionArn: string;
function GetTopicArn: string;
procedure SetEndpoint(const Value: string);
procedure SetOwner(const Value: string);
procedure SetProtocol(const Value: string);
procedure SetSubscriptionArn(const Value: string);
procedure SetTopicArn(const Value: string);
procedure LoadFromXML(AXml: IXMLNode);
property Endpoint: string read GetEndpoint write SetEndpoint;
property Owner: string read GetOwner write SetOwner;
property Protocol: string read GetProtocol write SetProtocol;
property SubscriptionArn: string read GetSubscriptionArn write SetSubscriptionArn;
property TopicArn: string read GetTopicArn write SetTopicArn;
end;
TksAwsSnsSubscriptionList = class(TList<IksAwsSnsSubscription>)
public
function AddSubscription: IksAwsSnsSubscription;
end;
IksAwsSNS = interface
['{8D5D089F-BA87-457D-BB51-693B551920C1}']
procedure CreateTopic(ATopicName: string);
procedure DeleteTopic(ATopicArn: string);
procedure ListSubscriptions(ASubscriptions: TksAwsSnsSubscriptionList);
procedure ListSubscriptionsByTopic(ATopicArn: string; ASubscriptions: TksAwsSnsSubscriptionList);
procedure ListTopics(ATopics: TStrings; const AMaxItems: integer = 0);
end;
function CreateSNS(AAccessKey, ASecretKey: string; ARegion: TksAwsRegion): IksAwsSNS;
implementation
uses ksAwsConst, Xml.xmldom, Xml.XMLDoc, Math, ksAwsHash;
type
TksAwsSnsSubscription = class(TInterfacedObject, IksAwsSnsSubscription)
private
FEndpoint: string;
FOwner: string;
FProtocol: string;
FSubscriptionArn: string;
FTopicArn: string;
function GetEndpoint: string;
function GetOwner: string;
function GetProtocol: string;
function GetSubscriptionArn: string;
function GetTopicArn: string;
procedure SetEndpoint(const Value: string);
procedure SetOwner(const Value: string);
procedure SetProtocol(const Value: string);
procedure SetSubscriptionArn(const Value: string);
procedure SetTopicArn(const Value: string);
protected
procedure LoadFromXML(AXml: IXMLNode);
property Endpoint: string read GetEndpoint write SetEndpoint;
property Owner: string read GetOwner write SetOwner;
property Protocol: string read GetProtocol write SetProtocol;
property SubscriptionArn: string read GetSubscriptionArn write SetSubscriptionArn;
property TopicArn: string read GetTopicArn write SetTopicArn;
end;
TksAwsSNS = class(TksAwsBaseService, IksAwsSNS)
protected
function GetApiVersion: string; override;
function GetServiceName: string; override;
procedure CreateTopic(ATopicName: string);
procedure DeleteTopic(ATopicArn: string);
procedure ListSubscriptions(ASubscriptions: TksAwsSnsSubscriptionList);
procedure ListSubscriptionsByTopic(ATopicArn: string; ASubscriptions: TksAwsSnsSubscriptionList);
procedure ListTopics(ATopics: TStrings; const AMaxItems: integer = 0);
public
{ Public declarations }
end;
function CreateSNS(AAccessKey, ASecretKey: string; ARegion: TksAwsRegion): IksAwsSNS;
begin
Result := TksAwsSNS.Create(AAccessKey, ASecretKey, ARegion);
end;
{ TksAwsSNS }
procedure TksAwsSNS.CreateTopic(ATopicName: string);
var
AParams: TStrings;
begin
AParams := TStringList.Create;
try
AParams.Values['Name'] := ATopicName;
ExecuteHttp(C_POST, 'CreateTopic', Host, '', '', nil, AParams).ContentAsString;
finally
AParams.Free;
end;
end;
procedure TksAwsSNS.DeleteTopic(ATopicArn: string);
var
AParams: TStrings;
begin
AParams := TStringList.Create;
try
AParams.Values['TopicArn'] := ATopicArn;
ExecuteHttp(C_GET, 'DeleteTopic', Host, '', '', nil, AParams).ContentAsString;
finally
AParams.Free;
end;
end;
function TksAwsSNS.GetApiVersion: string;
begin
Result := C_SNS_API_VERSION;
end;
function TksAwsSNS.GetServiceName: string;
begin
Result := C_SERVICE_SNS;
end;
procedure TksAwsSNS.ListSubscriptions(ASubscriptions: TksAwsSnsSubscriptionList);
begin
ListSubscriptionsByTopic('', ASubscriptions);
end;
procedure TksAwsSNS.ListSubscriptionsByTopic(ATopicArn: string; ASubscriptions: TksAwsSnsSubscriptionList);
var
AResponse: string;
AParams: TStrings;
AXml: IXmlDocument;
ANode: IXmlNode;
ICount: integer;
ANextToken: string;
AComplete: Boolean;
AMember: IXMLNode;
begin
ASubscriptions.Clear;
ANextToken := '';
AComplete := False;
AXml := TXMLDocument.Create(nil);
while not AComplete do
begin
AParams := TStringList.Create;
try
AParams.Values['TopicArn'] := ATopicArn;
AParams.Values['NextToken'] := ANextToken;
AResponse := ExecuteHttp(C_GET, 'ListSubscriptions', Host, '', '', nil, AParams).ContentAsString;
finally
AParams.Free;
end;
AXml.LoadFromXML(AResponse);
ANode := AXml.DocumentElement.ChildNodes['ListSubscriptionsResult'];
ANextToken := UrlEncode(ANode.ChildNodes['NextToken'].Text);
ANode := ANode.ChildNodes['Subscriptions'];
for ICount := 0 to ANode.ChildNodes.Count -1 do
begin
AMember := ANode.ChildNodes[ICount];
ASubscriptions.AddSubscription.LoadFromXML(AMember);
end;
AComplete := (ANextToken = '') ;
end;
end;
procedure TksAwsSNS.ListTopics(ATopics: TStrings; const AMaxItems: integer = 0);
var
AResponse: string;
AParams: TStrings;
AXml: IXmlDocument;
ANode: IXmlNode;
ICount: integer;
ANextToken: string;
AComplete: Boolean;
ATopic: IXMLNode;
begin
ATopics.Clear;
ANextToken := '';
AComplete := False;
AXml := TXMLDocument.Create(nil);
while not AComplete do
begin
AParams := TStringList.Create;
try
if AMaxItems > 0 then
AParams.Values['MaxItems'] := IntToStr(Min(AMaxItems, 100));
AParams.Values['NextToken'] := ANextToken;
AResponse := ExecuteHttp(C_GET, 'ListTopics', Host, '', '', nil, AParams).ContentAsString;
finally
AParams.Free;
end;
AXml.LoadFromXML(AResponse);
ANode := AXml.DocumentElement.ChildNodes['ListTopicsResult'];
ANextToken := UrlEncode(ANode.ChildNodes['NextToken'].Text);
ANode := ANode.ChildNodes['Topics'];
for ICount := 0 to ANode.ChildNodes.Count -1 do
begin
ATopic := ANode.ChildNodes[ICount];
if (AMaxItems = 0) or (ATopics.Count < AMaxItems) then
begin
ATopics.Add(ATopic.ChildNodes['TopicArn'].Text);
end;
end;
AComplete := (ANextToken = '') or (ATopics.Count >= AMaxItems);
end;
end;
{ TksAwsSnsSubscription }
function TksAwsSnsSubscription.GetEndpoint: string;
begin
Result := FEndpoint;
end;
function TksAwsSnsSubscription.GetOwner: string;
begin
Result := FOwner;
end;
function TksAwsSnsSubscription.GetProtocol: string;
begin
Result := FProtocol;
end;
function TksAwsSnsSubscription.GetSubscriptionArn: string;
begin
Result := FSubscriptionArn;
end;
function TksAwsSnsSubscription.GetTopicArn: string;
begin
Result := FTopicArn;
end;
procedure TksAwsSnsSubscription.LoadFromXML(AXml: IXMLNode);
begin
FEndpoint := AXml.ChildNodes['Endpoint'].Text;
FOwner := AXml.ChildNodes['Owner'].Text;
FProtocol := AXml.ChildNodes['Protocol'].Text;
FSubscriptionArn := AXml.ChildNodes['SubscriptionArn'].Text;
FTopicArn := AXml.ChildNodes['TopicArn'].Text;
end;
procedure TksAwsSnsSubscription.SetEndpoint(const Value: string);
begin
FEndpoint := Value;
end;
procedure TksAwsSnsSubscription.SetOwner(const Value: string);
begin
FOwner := Value;
end;
procedure TksAwsSnsSubscription.SetProtocol(const Value: string);
begin
FProtocol := Value;
end;
procedure TksAwsSnsSubscription.SetSubscriptionArn(const Value: string);
begin
FSubscriptionArn := Value;
end;
procedure TksAwsSnsSubscription.SetTopicArn(const Value: string);
begin
FTopicArn := Value;
end;
{ TksAwsSnsSubscriptionList }
function TksAwsSnsSubscriptionList.AddSubscription: IksAwsSnsSubscription;
begin
Result := TksAwsSnsSubscription.Create;
Add(Result);
end;
end.