J'ai le plaisir d'annoncer le sgcOpenAPI Pascal Parser, un tout nouveau produit qui permet d'importer une spécification OpenAPI et de générer une interface Pascal. Le parser sgcOpenAPI prend en charge les spécifications suivantes :
- OpenAPI 3.*
- Swagger 2.* (converti automatiquement de 2.0 vers 3.0)
- Swagger 1.* (converti automatiquement de 1.0 vers 3.0)
Il prend en charge les authentifications suivantes :
- Basic
- Bearer Token
- OAuth2
- JWT
Parser OpenAPI
Un parser OpenAPI est un outil utilisé pour analyser et valider les spécifications OpenAPI. OpenAPI est une spécification pour la construction d'API qui définit une interface standard indépendante du langage pour les services web RESTful.
Le parser OpenAPI lit des documents OpenAPI écrits au format JSON ou YAML et vérifie qu'ils respectent les règles et directives de la spécification. Cela comprend la vérification que le document contient tous les champs requis, dispose de types de données valides et est conforme à la structure attendue.
En plus de valider la syntaxe du document OpenAPI, le parser peut aussi être utilisé pour générer du code à partir de la spécification, ce qui facilite l'implémentation de l'API dans différents langages de programmation.
Globalement, le parser OpenAPI aide à s'assurer que les API sont construites conformément à la spécification OpenAPI, ce qui peut améliorer l'interopérabilité entre différentes API et faciliter leur consommation et leur intégration par les développeurs.
Tu peux désormais utiliser le parser sgcOpenAPI pour importer n'importe quelle spécification OpenAPI et générer les méthodes d'interface Delphi / Pascal nécessaires pour interagir avec l'API.
Spécification OpenAPI
Tu trouveras ci-dessous un exemple de spécification OpenAPI utilisée pour obtenir des informations à partir d'une adresse IP.
{
"openapi": "3.0.1",
"servers": [
{
"url": "https://ipgeolocation.abstractapi.com"
}
],
"info": {
"description": "Abstract IP geolocation API allows developers to retrieve the region, country and city behind any IP worldwide. The API covers the geolocation of IPv4 and IPv6 addresses in 180+ countries worldwide. Extra information can be retrieved like the currency, flag or language associated to an IP.",
"title": "IP geolocation API",
"version": "1.0.0",
"x-apisguru-categories": [
"location"
],
"x-logo": {
"url": "https://api.apis.guru/v2/cache/logo/https_global-uploads.webflow.com_5ebbd0a566a3996636e55959_5ec2ba29feeeb05d69160e7b_webclip.png"
},
"x-origin": [
{
"format": "openapi",
"url": "https://documentation.abstractapi.com/ip-geolocation-openapi.json",
"version": "3.0"
}
],
"x-providerName": "abstractapi.com",
"x-serviceName": "geolocation"
},
"externalDocs": {
"description": "API Documentation",
"url": "https://www.abstractapi.com/ip-geolocation-api#docs"
},
"paths": {
"/v1/": {
"get": {
"description": "Retrieve the location of an IP address",
"parameters": [
{
"explode": true,
"in": "query",
"name": "api_key",
"required": true,
"schema": {
"type": "string"
},
"style": "form"
},
{
"explode": true,
"in": "query",
"name": "ip_address",
"required": false,
"schema": {
"example": "195.154.25.40",
"type": "string"
},
"style": "form"
},
{
"explode": true,
"in": "query",
"name": "fields",
"required": false,
"schema": {
"example": "country,city,timezone",
"type": "string"
},
"style": "form"
}
],
"responses": {
"200": {
"content": {
"application/json": {
"examples": {
"0": {
"value": "{\"ip_address\":\"195.154.25.40\",\"city\":\"Paris\",\"city_geoname_id\":2988507,\"region\":\"Île-de-France\",\"region_iso_code\":\"IDF\",\"region_geoname_id\":3012874,\"postal_code\":\"75008\",\"country\":\"France\",\"country_code\":\"FR\",\"country_geoname_id\":3017382,\"country_is_eu\":true,\"continent\":\"Europe\",\"continent_code\":\"EU\",\"continent_geoname_id\":6255148,\"longitude\":2.4075,\"latitude\":48.8323,\"security\":{\"is_vpn\":false},\"timezone\":{\"name\":\"Europe/Paris\",\"abbreviation\":\"CEST\",\"gmt_offset\":2,\"current_time\":\"15:42:18\",\"is_dst\":true},\"flag\":{\"emoji\":\"<ë<÷\",\"unicode\":\"U+1F1EB U+1F1F7\",\"png\":\"https://static.abstractapi.com/country-flags/FR_flag.png\",\"svg\":\"https://static.abstractapi.com/country-flags/FR_flag.svg\"},\"currency\":{\"currency_name\":\"Euros\",\"currency_code\":\"EUR\"},\"connection\":{\"autonomous_system_number\":12876,\"autonomous_system_organization\":\"Online S.a.s.\",\"connection_type\":\"Corporate\",\"isp_name\":\"Online S.A.S.\",\"organization_name\":\"ONLINE\"}}"
}
},
"schema": {
"$ref": "#/components/schemas/inline_response_200"
}
}
},
"description": "Location of geolocated IP"
}
},
"servers": [
{
"url": "https://ipgeolocation.abstractapi.com"
}
]
},
"servers": [
{
"url": "https://ipgeolocation.abstractapi.com"
}
]
}
},
"components": {
"schemas": {
"inline_response_200": {
"properties": {
"city": {
"type": "string"
},
"city_geoname_id": {
"type": "integer"
},
"connection": {
"properties": {
"autonomous_system_number": {
"type": "integer"
},
"autonomous_system_organization": {
"type": "string"
},
"connection_type": {
"type": "string"
},
"isp_name": {
"type": "string"
},
"organization_name": {
"type": "string"
}
},
"type": "object"
},
"continent": {
"type": "string"
},
"continent_code": {
"type": "string"
},
"continent_geoname_id": {
"type": "integer"
},
"country": {
"type": "string"
},
"country_code": {
"type": "string"
},
"country_geoname_id": {
"type": "integer"
},
"country_is_eu": {
"type": "boolean"
},
"currency": {
"properties": {
"currency_code": {
"type": "string"
},
"currency_name": {
"type": "string"
}
},
"type": "object"
},
"flag": {
"properties": {
"emoji": {
"type": "string"
},
"png": {
"type": "string"
},
"svg": {
"type": "string"
},
"unicode": {
"type": "string"
}
},
"type": "object"
},
"ip_address": {
"type": "string"
},
"latitude": {
"type": "number"
},
"longitude": {
"type": "number"
},
"postal_code": {
"type": "string"
},
"region": {
"type": "string"
},
"region_geoname_id": {
"type": "integer"
},
"region_iso_code": {
"type": "string"
},
"security": {
"properties": {
"is_vpn": {
"type": "boolean"
}
},
"type": "object"
},
"timezone": {
"properties": {
"abbreviation": {
"type": "string"
},
"current_time": {
"type": "string"
},
"gmt_offset": {
"type": "integer"
},
"is_dst": {
"type": "boolean"
},
"name": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
}
}
}
}
Le parser sgcOpenAPI lit la spécification et crée automatiquement toutes les classes, méthodes... requises pour interagir avec la classe. Tu trouveras ci-dessous l'interface créée à partir de la spécification précédente.
{ ***************************************************************************
sgcOpenAPI component
written by eSeGeCe
copyright © 2023
Web : http://www.esegece.com
*************************************************************************** }
unit geolocation;
interface
{$IF DEFINED(VER280) OR DEFINED(VER290) OR DEFINED(VER300) OR DEFINED(VER310) OR DEFINED(VER320) OR DEFINED(VER330) OR DEFINED(VER340) OR DEFINED(VER350)}
{$DEFINE SGC_OPENAPI_JSON}
{$IFEND}
uses
Classes, SysUtils,
sgcHTTP_OpenAPI_Client;
Type
{$IFDEF SGC_OPENAPI_JSON}
TsgcOpenAPI_inline_response_200_Class = class;
TsgcOpenAPI_Retrieve_the_location_of_an_IP_address_Response = class;
{$ELSE}
TsgcOpenAPI_inline_response_200_Class = string;
TsgcOpenAPI_Retrieve_the_location_of_an_IP_address_Response = string;
{$ENDIF}
sgcOpenAPI_geolocation = class(TsgcOpenAPI_Client)
protected
function _Retrieve_the_location_of_an_IP_address(aApi_key: string;
aIp_address: string = ''; aFields: string = '';
const aResponse: TsgcOpenAPIResponse = nil): string; virtual;
public
constructor Create(aOwner: TComponent); override;
public
procedure SetServer0();
public
function Retrieve_the_location_of_an_IP_address(aApi_key: string;
aIp_address: string = ''; aFields: string = '')
: TsgcOpenAPI_Retrieve_the_location_of_an_IP_address_Response;
end;
{$IFDEF SGC_OPENAPI_JSON}
TsgcOpenAPI_inline_response_200_Class = class(TsgcOpenAPIClass)
private
Fcity: String;
Fcity_geoname_id: Int64;
Fconnection: String;
Fcontinent: String;
Fcontinent_code: String;
Fcontinent_geoname_id: Int64;
Fcountry: String;
Fcountry_code: String;
Fcountry_geoname_id: Int64;
Fcountry_is_eu: Boolean;
Fcurrency: String;
Fflag: String;
Fip_address: String;
Flatitude: Extended;
Flongitude: Extended;
Fpostal_code: String;
Fregion: String;
Fregion_geoname_id: Int64;
Fregion_iso_code: String;
Fsecurity: String;
Ftimezone: String;
public
class function Read(const aValue: string)
: TsgcOpenAPI_inline_response_200_Class;
public
property City: String read Fcity write Fcity;
property City_geoname_id: Int64 read Fcity_geoname_id
write Fcity_geoname_id;
property Connection: String read Fconnection write Fconnection;
property Continent: String read Fcontinent write Fcontinent;
property Continent_code: String read Fcontinent_code write Fcontinent_code;
property Continent_geoname_id: Int64 read Fcontinent_geoname_id
write Fcontinent_geoname_id;
property Country: String read Fcountry write Fcountry;
property Country_code: String read Fcountry_code write Fcountry_code;
property Country_geoname_id: Int64 read Fcountry_geoname_id
write Fcountry_geoname_id;
property Country_is_eu: Boolean read Fcountry_is_eu write Fcountry_is_eu;
property Currency: String read Fcurrency write Fcurrency;
property Flag: String read Fflag write Fflag;
property Ip_address: String read Fip_address write Fip_address;
property Latitude: Extended read Flatitude write Flatitude;
property Longitude: Extended read Flongitude write Flongitude;
property Postal_code: String read Fpostal_code write Fpostal_code;
property Region: String read Fregion write Fregion;
property Region_geoname_id: Int64 read Fregion_geoname_id
write Fregion_geoname_id;
property Region_iso_code: String read Fregion_iso_code
write Fregion_iso_code;
property Security: String read Fsecurity write Fsecurity;
property Timezone: String read Ftimezone write Ftimezone;
end;
TsgcOpenAPI_Retrieve_the_location_of_an_IP_address_Response = class
(TsgcOpenAPIResponse)
private
FSuccessful: TsgcOpenAPI_inline_response_200_Class;
public
destructor Destroy; override;
public
property Successful: TsgcOpenAPI_inline_response_200_Class read FSuccessful
write FSuccessful;
end;
{$ENDIF}
function GetOpenAPIClient: sgcOpenAPI_geolocation;
procedure FreeOpenAPIClient;
implementation
Une fois que tu as l'interface Pascal, il est très facile de commencer à interagir avec l'API ; vois ci-dessous un exemple simple écrit en Delphi.
function GetCountry(const aApiKey, aIpAddress: string): string;
var
oResponse: TsgcOpenAPI_Retrieve_the_location_of_an_IP_address_Response;
begin
oResponse := GetOpenAPIClient.Retrieve_the_location_of_an_IP_address(aApiKey, aIpAddress);
Try
if oResponse.IsSuccessful then
result := oResponse.Successful.Country
else
raise Exception.Create(oResponse.ResponseError);
Finally
oResponse.Free;
End;
end;
Tu trouveras ci-dessous un lien vers la version d'essai du parser sgcOpenAPI.
