1 | unit UFioAPI;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, httpsend, ssl_openssl;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | TFioDataFormat = (dfXML, dfCVS, dfGPC, dfOFX, dfJSON, dfHTML, dfSTA);
|
---|
13 |
|
---|
14 | { TFioAPI }
|
---|
15 |
|
---|
16 | TFioAPI = class
|
---|
17 | private
|
---|
18 | function Download(URL: string; Data: TStrings): Boolean;
|
---|
19 | public
|
---|
20 | Token: string;
|
---|
21 | Format: TFioDataFormat;
|
---|
22 | function DownloadInterval(TimeFrom, TimeTo: TDateTime; Data: TStrings): Boolean;
|
---|
23 | function DownloadMonthly(Year, Id: Integer; Data: TStrings): Boolean;
|
---|
24 | function DownloadLast(Data: TStrings): Boolean;
|
---|
25 | end;
|
---|
26 |
|
---|
27 | const
|
---|
28 | DataFormatText: array[TFioDataFormat] of string = ('XML', 'CSV', 'GPC', 'OFX',
|
---|
29 | 'JSON', 'HTML', 'STA');
|
---|
30 | DataFormatURL: array[TFioDataFormat] of string = ('xml', 'csv', 'gpc', 'ofx',
|
---|
31 | 'json', 'html', 'sta');
|
---|
32 |
|
---|
33 | implementation
|
---|
34 |
|
---|
35 | { TFioAPI }
|
---|
36 |
|
---|
37 | function TFioAPI.Download(URL: string; Data: TStrings): Boolean;
|
---|
38 | var
|
---|
39 | HTTPSender: THTTPSend;
|
---|
40 | begin
|
---|
41 | HTTPSender := THTTPSend.Create;
|
---|
42 | try
|
---|
43 | HTTPSender.HTTPMethod('GET', URL);
|
---|
44 | try
|
---|
45 | case HTTPSender.Resultcode of
|
---|
46 | 100..299:
|
---|
47 | begin
|
---|
48 | Data.LoadFromStream(HTTPSender.Document);
|
---|
49 | Result := True;
|
---|
50 | end; //informational, success
|
---|
51 | 300..399: Result := False; //redirection. Not implemented, but could be.
|
---|
52 | 400..499: Result := False; //client error; 404 not found etc
|
---|
53 | 500..599: Result := False; //internal server error
|
---|
54 | else Result := False; //unknown code
|
---|
55 | end;
|
---|
56 | except
|
---|
57 | // We don't care for the reason for this error; the download failed.
|
---|
58 | Result := False;
|
---|
59 | end;
|
---|
60 | finally
|
---|
61 | HTTPSender.Free;
|
---|
62 | end;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | function TFioAPI.DownloadInterval(TimeFrom, TimeTo: TDateTime; Data: TStrings
|
---|
66 | ): Boolean;
|
---|
67 | begin
|
---|
68 | Result := Download('https://www.fio.cz/ib_api/rest/periods/' + Token +
|
---|
69 | '/' + FormatDateTime('yyyy-mm-dd', TimeFrom) +
|
---|
70 | '/' + FormatDateTime('yyyy-mm-dd', TimeTo) +
|
---|
71 | '/transactions.' + DataFormatURL[Format], Data);
|
---|
72 | end;
|
---|
73 |
|
---|
74 | function TFioAPI.DownloadMonthly(Year, Id: Integer; Data: TStrings): Boolean;
|
---|
75 | begin
|
---|
76 | Result := Download('https://www.fio.cz/ib_api/rest/by-id/' + Token +
|
---|
77 | '/' + IntToStr(Year) + '/' + IntToStr(Id) +
|
---|
78 | '/transactions.' + DataFormatURL[Format], Data);
|
---|
79 | end;
|
---|
80 |
|
---|
81 | function TFioAPI.DownloadLast(Data: TStrings): Boolean;
|
---|
82 | begin
|
---|
83 | Result := Download('https://www.fio.cz/ib_api/rest/last/' + Token +
|
---|
84 | '/transactions.' + DataFormatURL[Format], Data);
|
---|
85 | end;
|
---|
86 |
|
---|
87 | end.
|
---|
88 |
|
---|