-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathudmWordnikAPI.pas
130 lines (113 loc) · 4.07 KB
/
udmWordnikAPI.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
unit udmWordnikAPI;
interface
uses
System.SysUtils, System.Classes, REST.Types, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param,
FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, Data.DB, FireDAC.Comp.DataSet,
FireDAC.Comp.Client, REST.Response.Adapter, REST.Client, Data.Bind.Components, Data.Bind.ObjectScope,
FireDAC.Stan.StorageBin;
type
TdmWordnikAPI = class(TDataModule)
RESTClientWordnik: TRESTClient;
RESTReqDefinitions: TRESTRequest;
RESTRespDefinitions: TRESTResponse;
RESTRespDSADefinitions: TRESTResponseDataSetAdapter;
tblDefinitions: TFDMemTable;
tblDefinitionsid: TWideStringField;
tblDefinitionspartOfSpeech: TWideStringField;
tblDefinitionsattributionText: TWideStringField;
tblDefinitionssourceDictionary: TWideStringField;
tblDefinitionstext: TWideStringField;
tblDefinitionssequence: TWideStringField;
tblDefinitionsscore: TWideStringField;
tblDefinitionslabels: TWideStringField;
tblDefinitionscitations: TWideStringField;
tblDefinitionsword: TWideStringField;
tblDefinitionsrelatedWords: TWideStringField;
tblDefinitionsexampleUses: TWideStringField;
tblDefinitionstextProns: TWideStringField;
tblDefinitionsnotes: TWideStringField;
tblDefinitionsattributionUrl: TWideStringField;
tblDefinitionswordnikUrl: TWideStringField;
RESTReqExamples: TRESTRequest;
RESTRespExamples: TRESTResponse;
RESTRespDSAExamples: TRESTResponseDataSetAdapter;
tblExamples: TFDMemTable;
tblExamplesprovider: TWideStringField;
tblExamplesyear: TFloatField;
tblExamplesrating: TFloatField;
tblExamplesurl: TWideStringField;
tblExamplesword: TWideStringField;
tblExamplestext: TWideStringField;
tblExamplesdocumentId: TFloatField;
tblExamplesexampleId: TFloatField;
tblExamplestitle: TWideStringField;
RESTReqWOTD: TRESTRequest;
RESTRespWOTD: TRESTResponse;
RESTRespDSAWordOofTheDay: TRESTResponseDataSetAdapter;
tblWOTD: TFDMemTable;
tblWOTD_id: TWideStringField;
tblWOTDword: TWideStringField;
tblWOTDcontentProvider: TWideStringField;
tblWOTDdefinitions: TWideStringField;
tblWOTDpublishDate: TDateTimeField;
tblWOTDexamples: TWideStringField;
tblWOTDpdd: TDateTimeField;
tblWOTDnote: TWideStringField;
tblWOTDhtmlExtra: TWideStringField;
RESTReqRandom: TRESTRequest;
RESTRespRandom: TRESTResponse;
RESTRespDSARandom: TRESTResponseDataSetAdapter;
tblRandom: TFDMemTable;
tblRandomid: TFloatField;
tblRandomword: TWideStringField;
procedure DataModuleCreate(Sender: TObject);
private
function GetAPIKey: string;
procedure SetRequestAPIKey(ARESTRequest: TRESTRequest; const APIKey: string);
public
procedure GetDefsAndExamples(const WordLookup: string);
function GetWordOfTheDay: string;
function GetRandomWord: string;
end;
var
dmWordnikAPI: TdmWordnikAPI;
implementation
{%CLASSGROUP 'FMX.Controls.TControl'}
{$R *.dfm}
{ TdmWordnikAPI }
procedure TdmWordnikAPI.SetRequestAPIKey(ARESTRequest: TRESTRequest; const APIKey: string);
begin
for var i := 0 to ARESTRequest.Params.Count - 1 do
if SameText(ARESTRequest.Params[i].Name, 'api_key') then begin
ARESTRequest.Params[i].Value := APIKey;
Break;
end;
end;
procedure TdmWordnikAPI.DataModuleCreate(Sender: TObject);
begin
for var i := 0 to ComponentCount - 1 do
if Components[i] is TRESTRequest then
SetRequestAPIKey(Components[i] as TRESTRequest, GetAPIKey);
end;
function TdmWordnikAPI.GetAPIKey: string;
begin
Result := 'arqntn49hevnmr68rm20ohqyrjzkh001i3l5k847jq1w0bkz1';
end;
procedure TdmWordnikAPI.GetDefsAndExamples(const WordLookup: string);
begin
RESTReqDefinitions.Resource := 'word.json/' + WordLookup;
RESTReqDefinitions.Execute;
RESTReqExamples.Resource := RESTReqDefinitions.Resource;
RESTReqExamples.Execute;
end;
function TdmWordnikAPI.GetRandomWord: string;
begin
RESTReqRandom.Execute;
Result := tblRandomword.AsString;
end;
function TdmWordnikAPI.GetWordOfTheDay: string;
begin
RESTReqWOTD.Execute;
Result := tblWOTDword.AsString;
end;
end.