source: trunk/UFioAPI.pas

Last change on this file was 22, checked in by chronos, 11 years ago
  • Upraveno: Příprava pro zobrazování seznamu operací.
File size: 3.0 KB
Line 
1unit UFioAPI;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, httpsend, ssl_openssl, dateutils;
9
10type
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
29const
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
35implementation
36
37{ TFioAPI }
38
39function TFioAPI.Download(URL: string; Data: TStrings): Boolean;
40var
41 HTTPSender: THTTPSend;
42begin
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;
66end;
67
68function TFioAPI.DownloadInterval(TimeFrom, TimeTo: TDateTime; Data: TStrings
69 ): Boolean;
70begin
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);
75end;
76
77function TFioAPI.DownloadMonthly(Year, Id: Integer; Data: TStrings): Boolean;
78begin
79 Result := Download('https://www.fio.cz/ib_api/rest/by-id/' + Token +
80 '/' + IntToStr(Year) + '/' + IntToStr(Id) +
81 '/transactions.' + DataFormatURL[Format], Data);
82end;
83
84function TFioAPI.DownloadLast(Data: TStrings): Boolean;
85begin
86 Result := Download('https://www.fio.cz/ib_api/rest/last/' + Token +
87 '/transactions.' + DataFormatURL[Format], Data);
88end;
89
90function TFioAPI.SetLastId(Id: Integer): Boolean;
91begin
92 Result := Download('https://www.fio.cz/ib_api/rest/set-last-id/' + Token +
93 '/' + IntToStr(Id), nil);
94end;
95
96function TFioAPI.SetLastDate(Date: TDate): Boolean;
97begin
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);
101end;
102
103end.
104
Note: See TracBrowser for help on using the repository browser.