OpenAPI Delphi Parser

I am happy to announce the sgcOpenAPI Pascal Parser, a brand new product allowing import an OpenAPI Specification and generate a Pascal Interface. The sgcOpenAPI Parser supports the following specifications:

  • OpenAPI 3.*
  • Swagger 2.* (automatically converted from 2.0 to 3.0)
  • Swagger 1.* (automatically converted from 1.0 to 3.0)

Supports the following Authentications:

  • Basic
  • Bearer Token
  • OAuth2
  • JWT

OpenAPI Parser

An OpenAPI Parser is a tool used for parsing and validating OpenAPI specifications. OpenAPI is a specification for building APIs that defines a standard language-agnostic interface for RESTful web services.

The OpenAPI Parser reads OpenAPI documents written in either JSON or YAML format, and ensures that they adhere to the specification's rules and guidelines. This includes checking that the document contains all the required fields, has valid data types, and conforms to the expected structure.


In addition to validating the syntax of the OpenAPI document, the parser can also be used to generate code from the specification, making it easier to implement the API in different programming languages.

Overall, the OpenAPI Parser helps ensure that APIs are built according to the OpenAPI specification, which can improve interoperability between different APIs and make it easier for developers to consume and integrate them. 

Now you can use the sgcOpenAPI Parser to import any OpenAPI Specification and generate the required Delphi / Pascal interface methods to interactuate with the API.

OpenAPI Specification

Find below an example of OpenAPI Specification used to get information given an IP Address 

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
{
  "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"
X

The sgcOpenAPI Parser reads the specification and creates automatically all required classes, methods... to interactuate with the class. Find below the interface created given the prior specification. 

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
{ ***************************************************************************
  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}
X
Once you have the Pascal interface, it's very easy to start interacting with the API, see below for a simple example written in Delphi.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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; 
X

Find below a link to the sgcOpenAPI Parser Trial

sgcOpenAPI Parser Trial

×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

sgcWebSockets 2023.2
Delphi Server Sent Events (SSE) Client