Menu

[r3148]: / trunk / Src / Web.USWAGRESTMgr.pas  Maximize  Restore  History

Download this file

415 lines (367 with data), 12.0 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a class that interfaces with the DelphiDabbler SWAG web service.
* For details of the web service see http://swag.delphidabbler.com/docs/api/
}
unit Web.USWAGRESTMgr;
interface
uses
// Delphi
XMLIntf,
Generics.Collections,
// Project
Web.UBaseWebService,
UXMLDocumentEx,
UURIParams,
SWAG.UCommon;
type
TSWAGRESTRange = record
Start: Cardinal;
Length: Cardinal;
constructor Create(AStart: Cardinal; ALength: Cardinal);
class function CreateNull: TSWAGRESTRange; static;
function IsNull: Boolean;
function ToString: string;
end;
type
TSWAGRESTMgr = class sealed(TBaseWebService)
strict private
const
/// <summary>Web service's base URL template.</summary>
ScriptURLTplt = 'http://swag.%s/api/v1';
/// <summary>User agent sent to web service.</summary>
UserAgent = 'CodeSnip';
procedure AddLimitParam(Params: TURIParams; const Range: TSWAGRESTRange);
procedure AddFieldsParam(Params: TURIParams; const Fields: array of string);
function CreateParams: TURIParams;
procedure ParseSnippetNode(XMLDoc: IXMLDocumentEx; Node: IXMLNode;
out Snippet: TSWAGSNippet);
procedure ValidateResponse(XMLDoc: IXMLDocumentEx);
function CreateValidXMLDoc(const XML: string): IXMLDocumentEx;
procedure ParseCategoriesResponse(const XML: string;
CatList: TList<TSWAGCategory>);
procedure ParseSnippetsResponse(const XML: string;
SnippetList: TList<TSWAGSnippet>);
procedure ParseSnippetResponse(const XML: string;
out Snippet: TSWAGSnippet);
function ParseSnippetCount(const XML: string): Cardinal;
public
constructor Create;
procedure GetCategories(CatList: TList<TSWAGCategory>);
procedure GetSnippets(const CatID: string; const Fields: array of string;
const Limit: TSWAGRESTRange; SnippetList: TList<TSWAGSnippet>);
overload;
procedure GetSnippets(const CatID: string; const Fields: array of string;
SnippetList: TList<TSWAGSnippet>); overload;
procedure GetSnippets(const Fields: array of string;
const Limit: TSWAGRESTRange; SnippetList: TList<TSWAGSnippet>); overload;
procedure GetSnippets(const Fields: array of string;
SnippetList: TList<TSWAGSnippet>); overload;
procedure GetSnippet(const SnippetID: Cardinal;
const Fields: array of string; out Snippet: TSWAGSnippet);
function GetSnippetCount(const CatID: string = '*'): Cardinal;
end;
implementation
uses
// Delphi
SysUtils,
// Project
UIStringList,
UStrUtils,
UURIEncode,
UUtils,
UXMLDocHelper,
Web.UInfo,
Web.UExceptions;
{ TSWAGRESTMgr }
procedure TSWAGRESTMgr.AddFieldsParam(Params: TURIParams;
const Fields: array of string);
var
FieldList: IStringList;
begin
FieldList := TIStringList.Create(Fields);
Params.Add('fields', FieldList.GetText(',', False));
end;
procedure TSWAGRESTMgr.AddLimitParam(Params: TURIParams;
const Range: TSWAGRESTRange);
begin
Params.Add('limit', Range.ToString);
end;
constructor TSWAGRESTMgr.Create;
begin
inherited Create(TWebServiceInfo.Create(ScriptURLTplt, UserAgent));
end;
function TSWAGRESTMgr.CreateParams: TURIParams;
begin
Result := TURIParams.Create;
Result.Add('format', 'xml');
Result.Add('suppressResponseCodes', 'true');
end;
function TSWAGRESTMgr.CreateValidXMLDoc(const XML: string): IXMLDocumentEx;
begin
Result := TXMLDocumentEx.Create(nil);
Result.LoadFromXML(XML);
ValidateResponse(Result);
end;
procedure TSWAGRESTMgr.GetCategories(CatList: TList<TSWAGCategory>);
const
ResourcePath = 'categories';
var
Params: TURIParams;
Response: string;
begin
Params := CreateParams;
try
Response := GetText(ResourcePath, Params);
ParseCategoriesResponse(Response, CatList);
finally
Params.Free;
end;
end;
procedure TSWAGRESTMgr.GetSnippet(const SnippetID: Cardinal;
const Fields: array of string; out Snippet: TSWAGSnippet);
const
ResourcePathTplt = 'snippet/%d';
var
Params: TURIParams;
Response: string;
begin
Params := CreateParams;
try
AddFieldsParam(Params, Fields);
Response := GetText(Format(ResourcePathTplt, [SnippetID]), Params);
ParseSnippetResponse(Response, Snippet);
finally
Params.Free;
end;
end;
function TSWAGRESTMgr.GetSnippetCount(const CatID: string): Cardinal;
const
ResourcePathTplt = 'snippet-count/%s';
var
Params: TURIParams;
Response: string;
begin
Params := CreateParams;
try
Response := GetText(Format(ResourcePathTplt, [URIEncode(CatID)]), Params);
Result := ParseSnippetCount(Response);
finally
Params.Free;
end;
end;
procedure TSWAGRESTMgr.GetSnippets(const CatID: string;
const Fields: array of string; const Limit: TSWAGRESTRange;
SnippetList: TList<TSWAGSnippet>);
const
ResourcePathTplt = 'snippets/%s';
var
Params: TURIParams;
Response: string;
begin
Params := CreateParams;
try
AddFieldsParam(Params, Fields);
if not Limit.IsNull then
AddLimitParam(Params, Limit);
Response := GetText(Format(ResourcePathTplt, [URIEncode(CatID)]), Params);
ParseSnippetsResponse(Response, SnippetList);
finally
Params.Free;
end;
end;
procedure TSWAGRESTMgr.GetSnippets(const CatID: string;
const Fields: array of string; SnippetList: TList<TSWAGSnippet>);
begin
GetSnippets(CatID, Fields, TSWAGRESTRange.CreateNull, SnippetList);
end;
procedure TSWAGRESTMgr.GetSnippets(const Fields: array of string;
SnippetList: TList<TSWAGSnippet>);
begin
GetSnippets('*', Fields, SnippetList);
end;
procedure TSWAGRESTMgr.GetSnippets(const Fields: array of string;
const Limit: TSWAGRESTRange; SnippetList: TList<TSWAGSnippet>);
begin
GetSnippets('*', Fields, Limit, SnippetList);
end;
procedure TSWAGRESTMgr.ParseCategoriesResponse(const XML: string;
CatList: TList<TSWAGCategory>);
var
XMLDOc: IXMLDocumentEx;
CategoriesNode: IXMLNode;
CategoryNodes: IXMLSimpleNodeList;
CategoryNode: IXMLNode;
Category: TSWAGCategory;
resourcestring
sCategoriesNodeMissing = 'Response has no category information';
begin
XMLDoc := CreateValidXMLDoc(XML);
CategoriesNode := XMLDoc.FindNode('response\categories');
if not Assigned(CategoriesNode) then
raise EWebServiceFailure.Create(sCategoriesNodeMissing);
CategoryNodes := XMLDoc.FindChildNodes(CategoriesNode, 'category');
for CategoryNode in CategoryNodes do
begin
Category.ID := TXMLDocHelper.GetSubTagText(XMLDoc, CategoryNode, 'id');
Category.Title := TXMLDocHelper.GetSubTagText(
XMLDoc, CategoryNode, 'title'
);
CatList.Add(Category);
end;
end;
function TSWAGRESTMgr.ParseSnippetCount(const XML: string): Cardinal;
var
XMLDOc: IXMLDocumentEx;
CountNode: IXMLNode;
resourcestring
sBadCountNode = 'Response has missing or invalid count information';
begin
XMLDoc := CreateValidXMLDoc(XML);
CountNode := XMLDoc.FindNode('response\snippetCount');
if not Assigned(CountNode)
or not CountNode.IsTextElement
or not TryStrToCardinal(CountNode.Text, Result) then
raise EWebServiceFailure.Create(sBadCountNode);
end;
procedure TSWAGRESTMgr.ParseSnippetNode(XMLDoc: IXMLDocumentEx; Node: IXMLNode;
out Snippet: TSWAGSNippet);
var
NumStr: string;
DateStr: string;
resourcestring
sBadID = 'Invalid value for snippet id';
sBadFileNumber = 'Invalid value for snippet file number';
begin
NumStr := TXMLDocHelper.GetSubTagText(XMLDoc, Node, 'id');
if NumStr <> '' then
begin
if not TryStrToCardinal(NumStr, Snippet.ID) then
raise EWebServiceFailure.Create(sBadID);
end
else
Snippet.ID := 0;
Snippet.Category := TXMLDocHelper.GetSubTagText(XMLDoc, Node, 'category');
Snippet.FileName := TXMLDocHelper.GetSubTagText(XMLDoc, Node, 'file_name');
DateStr := TXMLDocHelper.GetSubTagText(XMLDoc, Node, 'datestamp');
if DateStr <> '' then
Snippet.DateStamp := ParseSQLDateTime(DateStr)
else
Snippet.DateStamp := 0.0;
Snippet.Title := TXMLDocHelper.GetSubTagText(XMLDoc, Node, 'title');
Snippet.Author := TXMLDocHelper.GetSubTagText(XMLDoc, Node, 'author');
Snippet.SourceCode := TXMLDocHelper.GetSubTagText(
XMLDoc, Node, 'source_code'
);
Snippet.IsDocument := StrMatchText(
TXMLDocHelper.GetSubTagText(XMLDoc, Node, 'is_document'),
['1', 'yes', 'y', 'true']
);
end;
procedure TSWAGRESTMgr.ParseSnippetResponse(const XML: string;
out Snippet: TSWAGSnippet);
var
XMLDoc: IXMLDocumentEx;
SnippetNode: IXMLNode;
resourcestring
sSnippetNodeMissing = 'Response has no snippet information';
begin
XMLDoc := CreateValidXMLDoc(XML);
SnippetNode := XMLDoc.FindNode('response\snippet');
if not Assigned(SnippetNode) then
raise EWebServiceFailure.Create(sSnippetNodeMissing);
ParseSnippetNode(XMLDoc, SnippetNode, Snippet);
end;
procedure TSWAGRESTMgr.ParseSnippetsResponse(const XML: string;
SnippetList: TList<TSWAGSnippet>);
var
XMLDOc: IXMLDocumentEx;
SnippetsNode: IXMLNode;
SnippetNodes: IXMLSimpleNodeList;
SnippetNode: IXMLNode;
Snippet: TSWAGSnippet;
resourcestring
sSnippetsNodeMissing = 'Response has no snippets information';
begin
XMLDoc := CreateValidXMLDoc(XML);
SnippetsNode := XMLDoc.FindNode('response\snippets');
if not Assigned(SnippetsNode) then
raise EWebServiceFailure.Create(sSnippetsNodeMissing);
SnippetNodes := XMLDoc.FindChildNodes(SnippetsNode, 'snippet');
for SnippetNode in SnippetNodes do
begin
ParseSnippetNode(XMLDoc, SnippetNode, Snippet);
SnippetList.Add(Snippet);
end;
end;
procedure TSWAGRESTMgr.ValidateResponse(XMLDoc: IXMLDocumentEx);
resourcestring
sMissingRootNode = 'Response root node not found';
sBadStatus = 'Response status missing or invalid';
sMissingErrInfo = 'Error information missing';
sBadErrStatus = 'Error status code missing or mal-formed';
sBadErrCode = 'Error code missing or mal-formed';
sMissingErrMessage = 'Error message missing';
var
RootNode: IXMLNode;
Status: string;
ErrNode: IXMLNode;
ErrStatus: Integer;
ErrCode: Integer;
ErrMessage: string;
begin
TXMLDocHelper.ValidateProcessingInstr(XMLDoc);
RootNode := XMLDoc.FindNode('response');
if not Assigned(RootNode) then
raise EWebServiceFailure.Create(sMissingRootNode);
Status := TXMLDocHelper.GetSubTagText(XMLDoc, RootNode, 'status');
if not StrSameText(Status, 'ok') and not StrSameText(Status, 'error') then
raise EWebServiceFailure.Create(sBadStatus);
if StrSameText(Status, 'ok') then
Exit;
// we have error response - validate response and get details (don't validate
// 'response\error\status' node since we don't make use of it).
ErrNode := XMLDoc.FindNode('response\error');
if not Assigned(ErrNode) then
raise EWebServiceFailure(sMissingErrInfo);
if not TryStrToInt(
TXMLDocHelper.GetSubTagText(XMLDOc, ErrNode, 'status'), ErrStatus
) then
raise EWebServiceFailure.Create(sBadErrStatus);
if not TryStrToInt(
TXMLDocHelper.GetSubTagText(XMLDoc, ErrNode, 'code'), ErrCode
) then
raise EWebServiceFailure.Create(sBadErrCode);
ErrMessage := TXMLDocHelper.GetSubTagText(XMLDoc, ErrNode, 'message');
if ErrMessage = '' then
raise EWebServiceFailure.Create(sMissingErrMessage);
// error response is valid: raise exception for message and code
raise EWebServiceError.Create(ErrMessage, ErrCode);
end;
{ TSWAGRESTRange }
constructor TSWAGRESTRange.Create(AStart, ALength: Cardinal);
begin
Start := AStart;
Length := ALength;
end;
class function TSWAGRESTRange.CreateNull: TSWAGRESTRange;
begin
Result := TSWAGRESTRange.Create(0, 0);
end;
function TSWAGRESTRange.IsNull: Boolean;
begin
Result := Length = 0;
end;
function TSWAGRESTRange.ToString: string;
begin
Result := Format('%d,%d', [Start, Length]);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.