1 | unit UOdorikApi;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | System.SysUtils, System.Types, System.Classes, Generics.Collections, IdHTTP
|
---|
7 | {$IFDEF ANDROID},IdSSLOpenSSLHeaders{$endif}, IdIOHandler, IdIOHandlerSocket,
|
---|
8 | IdIOHandlerStack, IdSSL, IdSSLOpenSSL, System.DateUtils, System.TimeSpan,
|
---|
9 | System.JSON;
|
---|
10 |
|
---|
11 | type
|
---|
12 | TCall = class
|
---|
13 | Oid: Integer;
|
---|
14 | RedirectionParentId: Integer;
|
---|
15 | Date: TDateTime;
|
---|
16 | Direction: string;
|
---|
17 | SourceNumber: string;
|
---|
18 | DestinatioNumber: string;
|
---|
19 | DestinationName: string;
|
---|
20 | Length: Integer;
|
---|
21 | RingingLength: Integer;
|
---|
22 | Status: string;
|
---|
23 | Price: Currency;
|
---|
24 | PricePerMinute: Currency;
|
---|
25 | BalanceAfter: Currency;
|
---|
26 | Line: Integer;
|
---|
27 | Recording: string;
|
---|
28 | end;
|
---|
29 |
|
---|
30 | TOdorikApi = class(TComponent)
|
---|
31 | private
|
---|
32 | IdHTTP1: TIdHTTP;
|
---|
33 | IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
|
---|
34 | public
|
---|
35 | UserName: string;
|
---|
36 | Password: string;
|
---|
37 | ServerUrl: string;
|
---|
38 | function GetCredit: Currency;
|
---|
39 | function SendGet(Path: string; Parameters: string = ''): string;
|
---|
40 | function GetCallHistory(TimeFrom, TimeTo: TDateTime): TList<TCall>;
|
---|
41 | constructor Create(AOwner: TComponent); override;
|
---|
42 | destructor Destroy; override;
|
---|
43 | end;
|
---|
44 |
|
---|
45 | implementation
|
---|
46 |
|
---|
47 | function TOdorikApi.SendGet(Path: string; Parameters: string = ''): string;
|
---|
48 | var
|
---|
49 | Url: string;
|
---|
50 | begin
|
---|
51 | if (ServerUrl <> '') and (UserName <> '') and (Password <> '') then begin
|
---|
52 | Url := ServerUrl + Path + '?user=' + UserName + '&password=' + Password;
|
---|
53 | if Parameters <> '' then Url := Url + '&' + Parameters;
|
---|
54 | Result := IdHTTP1.Get(Url);
|
---|
55 | end else Result := '';
|
---|
56 | end;
|
---|
57 |
|
---|
58 | constructor TOdorikApi.Create(AOwner: TComponent);
|
---|
59 | begin
|
---|
60 | inherited;
|
---|
61 | {$IFDEF ANDROID}
|
---|
62 | IdOpenSSLSetLibPath(System.IOUtils.TPath.GetDocumentsPath);
|
---|
63 | {$ENDIF}
|
---|
64 | IdHTTP1 := TIdHTTP.Create(Self);
|
---|
65 | IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create(Self);
|
---|
66 | IdHTTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
|
---|
67 | end;
|
---|
68 |
|
---|
69 | destructor TOdorikApi.Destroy;
|
---|
70 | begin
|
---|
71 |
|
---|
72 | inherited;
|
---|
73 | end;
|
---|
74 |
|
---|
75 | function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): string;
|
---|
76 | const
|
---|
77 | Neg: array[Boolean] of string = ('+', '-');
|
---|
78 | var
|
---|
79 | Bias: Integer;
|
---|
80 | begin
|
---|
81 | Result := FormatDateTime('yyyy''-''mm''-''dd''T''hh'':''nn'':''ss''.''zzz', Value); { Do not localize }
|
---|
82 | Bias := Trunc(TTimeZone.Local.GetUtcOffset(Value).TotalMinutes);
|
---|
83 | if (Bias <> 0) and ApplyLocalBias then
|
---|
84 | begin
|
---|
85 | Result := Format('%s%s%.2d:%.2d', [Result, Neg[Bias > 0], { Do not localize }
|
---|
86 | Abs(Bias) div MinsPerHour,
|
---|
87 | Abs(Bias) mod MinsPerHour]);
|
---|
88 | end else
|
---|
89 | Result := Result + 'Z'; { Do not localize }
|
---|
90 | end;
|
---|
91 |
|
---|
92 | function TOdorikApi.GetCallHistory(TimeFrom, TimeTo: TDateTime): TList<TCall>;
|
---|
93 | var
|
---|
94 | Response: string;
|
---|
95 | LJsonResponse: TJSONValue;
|
---|
96 | jv: TJSONValue;
|
---|
97 | joName: TJSONObject;
|
---|
98 | Call: TCall;
|
---|
99 | begin
|
---|
100 | Result := TList<TCall>.Create;
|
---|
101 | Response := SendGet('/calls.json', 'from=' + DateTimeToXMLTime(TimeFrom) +
|
---|
102 | '&to=' + DateTimeToXMLTime(TimeTo));
|
---|
103 | LJsonResponse := TJSONObject.ParseJSONValue(Response);
|
---|
104 | for jv in LJsonResponse as TJSONArray do begin
|
---|
105 | joName := jv as TJSONObject;
|
---|
106 | Call := TCall.Create;
|
---|
107 | Call.SourceNumber := joName.Get('source_number').JSONValue.Value;
|
---|
108 | Call.DestinatioNumber := joName.Get('destination_number').JSONValue.Value;
|
---|
109 | Result.Add(Call);
|
---|
110 | end;
|
---|
111 | end;
|
---|
112 |
|
---|
113 | function TOdorikApi.GetCredit: Currency;
|
---|
114 | var
|
---|
115 | Response: string;
|
---|
116 | Value: Extended;
|
---|
117 | begin
|
---|
118 | Response := SendGet('/balance');
|
---|
119 | if Response <> '' then begin
|
---|
120 | Response := StringReplace(Response, '.', FormatSettings.DecimalSeparator, [rfReplaceAll]);
|
---|
121 | if TryStrToFloat(Response, Value) then
|
---|
122 | Result := Value
|
---|
123 | else Result := 0;
|
---|
124 | end else Result := 0;
|
---|
125 | end;
|
---|
126 |
|
---|
127 |
|
---|
128 |
|
---|
129 | end.
|
---|