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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
unit UnitJoursFeries;
interface
uses
Winapi.Windows, Winapi.Messages, Wininet, System.Math,
System.SysUtils, System.Variants, System.Classes, System.JSON, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, IdIOHandler, IdURI,
IdIOHandlerStream, IPPeerClient, Data.Bind.Components, Data.Bind.ObjectScope,
REST.Client, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL,
Vcl.Grids, Vcl.ValEdit, System.DateUtils;
type
TOnGetJourFerie = reference to procedure(Nom: string; Jour: TDate);
procedure GetJourFeries(OnGetJourFerie: TOnGetJourFerie; AnneeDebut, AnneeFin: Integer);
procedure GetJourFeriesAvecSSL(OnGetJourFerie: TOnGetJourFerie);
implementation
const
API = 'https://calendrier.api.gouv.fr/jours-feries/%s.json';
Zones: array [0 .. 12] of string = (
'alsace-moselle', 'guadeloupe', 'guyane', 'la-reunion', 'martinique',
'mayotte', 'metropole', 'nouvelle-caledonie', 'polynesie-francaise',
'saint-barthelemy', 'saint-martin', 'saint-pierre-et-miquelon', 'wallis-et-futuna');
{$REGION 'AVEC dépendance à OpenSSL'}
/// <summary>
/// Nécessite libeay32.dll et ssleay32.dll version 1.0.2.11 ou plus
/// </summary>
procedure DownloadHTTP(const AUrl: string; out DestStream: TStream);
function IsHTTPS(const AUrl: string): Boolean;
begin
with TIdURI.Create(AUrl) do
try
Result := SameText(Protocol, 'HTTPS');
finally
Free;
end;
end;
begin
with TIdHTTP.Create(Application) do
try
if IsHTTPS(AUrl) then
begin
IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
TIdSSLIOHandlerSocketOpenSSL(IOHandler).SSLOptions.Method := sslvSSLv23;
// la valeur sslvSSLv23 laisse au server le choix du protocole, on ne s'en occupe pas
end;
try
Get(AUrl, DestStream);
except
on e: Exception do
MessageDlg(Format('Erreur : %s', [e.Message]), mtInformation, [mbOK], 0);
end;
finally
IOHandler.Free;
Free;
end;
end;
procedure GetJourFeriesAvecSSL(OnGetJourFerie: TOnGetJourFerie);
begin
end;
{$ENDREGION 'AVEC dépendance à OpenSSL'}
{$REGION 'SANS dépendance à OpenSSL'}
procedure GetInternetFile(const AServerName: string; const AUrl: string; AStream: TStream;
const ExpectedContentType: string = '');
type
TAcceptTypes = packed array [0 .. 1] of LPWSTR;
const
BufferSize = 1024;
ACCEPT_TYPES: TAcceptTypes = (PChar('text/*'), nil); // PCTSTR rgpszAcceptTypes[] = {_T(?text/*?), NULL};
var
hSession, hHTTP, hReq: HINTERNET;
Accept : TAcceptTypes;
Buffer : array [1 .. BufferSize] of Byte;
BufferLen : DWORD;
sAppName : string;
begin
AStream.Size := 0;
sAppName := ExtractFileName(Application.ExeName);
Accept := ACCEPT_TYPES;
if ExpectedContentType <> '' then
Accept[0] := PChar(ExpectedContentType);
// Winapi.WinInet !!!
hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
hHTTP := InternetConnect(hSession, PChar(AServerName), INTERNET_DEFAULT_HTTP_PORT, nil, nil,
INTERNET_SERVICE_HTTP, 0, 1);
try
hReq := HttpOpenRequest(hHTTP, PChar('GET'), PChar(AUrl), nil, nil, @Accept, 0, 1);
try
if HttpSendRequest(hReq, nil, 0, nil, 0) then
begin
BufferLen := 0;
repeat
if InternetReadFile(hReq, @Buffer, BufferSize, BufferLen) then
AStream.WriteBuffer(Buffer, BufferLen);
until BufferLen = 0;
end
else
raise Exception.Create('HttpOpenRequest failed. ' + SysErrorMessage(GetLastError));
finally
InternetCloseHandle(hReq);
end;
finally
InternetCloseHandle(hHTTP);
end;
finally
InternetCloseHandle(hSession);
end;
end;
procedure GetJourFeries(OnGetJourFerie: TOnGetJourFerie; AnneeDebut, AnneeFin: Integer);
var
JSONStream : TStringStream;
ResponseJSON: TJSONObject;
Pair : TJSONPair;
DateStr : string;
Dts : TArray<string>;
DateDte : TDate;
Annee : Word;
begin
JSONStream := TStringStream.Create('', TEncoding.UTF8);
try
GetInternetFile('calendrier.api.gouv.fr', '/jours-feries/metropole.json', JSONStream, 'application/json');
ResponseJSON := TJSONObject.ParseJSONValue(JSONStream.DataString) as TJSONObject; // UTF8 !
try
for Pair in ResponseJSON do
begin
DateStr := Pair.JsonString.Value;
Dts := DateStr.Split(['-']);
Annee := Dts[0].ToInteger;
if InRange(Annee, AnneeDebut, AnneeFin) then
begin
DateDte := System.DateUtils.EncodeDateTime(Annee, Dts[1].ToInteger, Dts[2].ToInteger, 0, 0, 0, 0);
OnGetJourFerie(Pair.JsonValue.Value, DateDte);
end;
end;
finally
ResponseJSON.Free();
end;
finally
JSONStream.Free();
end;
end;
{$ENDREGION 'SANS dépendance à OpenSSL'}
end. |
Partager