| 1 | unit UFioAPI;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, httpsend, ssl_openssl, dateutils;
|
|---|
| 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 | function SetLastId(Id: Integer): Boolean;
|
|---|
| 26 | function SetLastDate(Date: TDate): Boolean;
|
|---|
| 27 | end;
|
|---|
| 28 |
|
|---|
| 29 | const
|
|---|
| 30 | DataFormatText: array[TFioDataFormat] of string = ('XML', 'CSV', 'GPC', 'OFX',
|
|---|
| 31 | 'JSON', 'HTML', 'STA');
|
|---|
| 32 | DataFormatURL: array[TFioDataFormat] of string = ('xml', 'csv', 'gpc', 'ofx',
|
|---|
| 33 | 'json', 'html', 'sta');
|
|---|
| 34 |
|
|---|
| 35 | implementation
|
|---|
| 36 |
|
|---|
| 37 | { TFioAPI }
|
|---|
| 38 |
|
|---|
| 39 | function TFioAPI.Download(URL: string; Data: TStrings): Boolean;
|
|---|
| 40 | var
|
|---|
| 41 | HTTPSender: THTTPSend;
|
|---|
| 42 | begin
|
|---|
| 43 | HTTPSender := THTTPSend.Create;
|
|---|
| 44 | try
|
|---|
| 45 | HTTPSender.HTTPMethod('GET', URL);
|
|---|
| 46 | try
|
|---|
| 47 | case HTTPSender.Resultcode of
|
|---|
| 48 | 100..299:
|
|---|
| 49 | begin
|
|---|
| 50 | if Assigned(Data) then
|
|---|
| 51 | Data.LoadFromStream(HTTPSender.Document);
|
|---|
| 52 | Result := True;
|
|---|
| 53 | end; //informational, success
|
|---|
| 54 | 300..399: Result := False; //redirection. Not implemented, but could be.
|
|---|
| 55 | 400..499: Result := False; //client error; 404 not found etc
|
|---|
| 56 | 500..599: Result := False; //internal server error
|
|---|
| 57 | else Result := False; //unknown code
|
|---|
| 58 | end;
|
|---|
| 59 | except
|
|---|
| 60 | // We don't care for the reason for this error; the download failed.
|
|---|
| 61 | Result := False;
|
|---|
| 62 | end;
|
|---|
| 63 | finally
|
|---|
| 64 | HTTPSender.Free;
|
|---|
| 65 | end;
|
|---|
| 66 | end;
|
|---|
| 67 |
|
|---|
| 68 | function TFioAPI.DownloadInterval(TimeFrom, TimeTo: TDateTime; Data: TStrings
|
|---|
| 69 | ): Boolean;
|
|---|
| 70 | begin
|
|---|
| 71 | Result := Download('https://www.fio.cz/ib_api/rest/periods/' + Token +
|
|---|
| 72 | '/' + FormatDateTime('yyyy-mm-dd', TimeFrom) +
|
|---|
| 73 | '/' + FormatDateTime('yyyy-mm-dd', TimeTo) +
|
|---|
| 74 | '/transactions.' + DataFormatURL[Format], Data);
|
|---|
| 75 | end;
|
|---|
| 76 |
|
|---|
| 77 | function TFioAPI.DownloadMonthly(Year, Id: Integer; Data: TStrings): Boolean;
|
|---|
| 78 | begin
|
|---|
| 79 | Result := Download('https://www.fio.cz/ib_api/rest/by-id/' + Token +
|
|---|
| 80 | '/' + IntToStr(Year) + '/' + IntToStr(Id) +
|
|---|
| 81 | '/transactions.' + DataFormatURL[Format], Data);
|
|---|
| 82 | end;
|
|---|
| 83 |
|
|---|
| 84 | function TFioAPI.DownloadLast(Data: TStrings): Boolean;
|
|---|
| 85 | begin
|
|---|
| 86 | Result := Download('https://www.fio.cz/ib_api/rest/last/' + Token +
|
|---|
| 87 | '/transactions.' + DataFormatURL[Format], Data);
|
|---|
| 88 | end;
|
|---|
| 89 |
|
|---|
| 90 | function TFioAPI.SetLastId(Id: Integer): Boolean;
|
|---|
| 91 | begin
|
|---|
| 92 | Result := Download('https://www.fio.cz/ib_api/rest/set-last-id/' + Token +
|
|---|
| 93 | '/' + IntToStr(Id), nil);
|
|---|
| 94 | end;
|
|---|
| 95 |
|
|---|
| 96 | function TFioAPI.SetLastDate(Date: TDate): Boolean;
|
|---|
| 97 | begin
|
|---|
| 98 | Result := Download('https://www.fio.cz/ib_api/rest/set-last-date/' + Token +
|
|---|
| 99 | '/' + IntToStr(YearOf(Date)) + '/' + IntToStr(MonthOf(Date)) + '/' +
|
|---|
| 100 | IntToStr(DayOf(Date)), nil);
|
|---|
| 101 | end;
|
|---|
| 102 |
|
|---|
| 103 | end.
|
|---|
| 104 |
|
|---|