Recent

Author Topic: openAPI client  (Read 954 times)

rnervi

  • New Member
  • *
  • Posts: 14
openAPI client
« on: July 02, 2024, 01:57:32 pm »
some1 can help with a simple, clear and easy snippet of code to deal with info supplied by  openAPI.com services ?
ty

Thaddy

  • Hero Member
  • *****
  • Posts: 15641
  • Censorship about opinions does not belong here.
Re: openAPI client
« Reply #1 on: July 02, 2024, 02:33:45 pm »
Do you mean OpenAPI.it or OpenAI.com? A bit confusing. And then there is OpenApis.org...
« Last Edit: July 02, 2024, 02:37:42 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

rnervi

  • New Member
  • *
  • Posts: 14
Re: openAPI client
« Reply #2 on: July 02, 2024, 02:43:28 pm »
.com (and for me it opens the .it - same site with nationally interesting API)
it supplies some API services
and despite the .it or .com the interfacing way should be the same

cdbc

  • Hero Member
  • *****
  • Posts: 1513
    • http://www.cdbc.dk
Re: openAPI client
« Reply #3 on: July 02, 2024, 03:11:29 pm »
Hi
Cannot understand a word of it, api is in Italian?!?
Sorry, a waste of my time!
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Zvoni

  • Hero Member
  • *****
  • Posts: 2690
Re: openAPI client
« Reply #4 on: July 02, 2024, 03:13:49 pm »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

rnervi

  • New Member
  • *
  • Posts: 14
Re: openAPI client
« Reply #5 on: July 02, 2024, 03:59:35 pm »
sure, it's json but didnt understood how to write dowb simple code to call and use answers. They supply some code snippet for many langs BUT object pascal.
meanwhile I asked them if any tutorial or code snippet natively.
then I change - simpyfing  - question:
is there a basic delphi code to call an api service and get answer (to be post processed) ?

paweld

  • Hero Member
  • *****
  • Posts: 1213
Re: openAPI client
« Reply #6 on: July 02, 2024, 04:10:55 pm »
Example of get data from endpoint IT-legalforms of company module. You need to generate a token in their panels or using the API.
Code: Pascal  [Select][+][-]
  1. uses  fphttpclient, opensslsockets, fpjson;
  2.  
  3. const
  4.   api_url = 'https://test.company.openapi.com/'; // for COMPANY module: <-- sandbox | production --> 'https://company.openapi.com/'
  5.   YOUR_OpenAPI_Token = 'sample-OpenAPI-fake-token';  //paste your token
  6.  
  7. procedure TForm1.Button1Click(Sender: TObject);
  8. var
  9.   hc: TFPHttpClient;
  10.   errmsg: String;
  11.   jsonstream: TStringStream;
  12.   jo: TJsonObject;
  13. begin
  14.   errmsg := '';
  15.   hc := TFPHttpClient.Create(nil);
  16.   hc.AllowRedirect := True;
  17.   hc.RequestHeaders.Insert(0, 'Authorization: Bearer ' + YOUR_OpenAPI_Token);
  18.   hc.RequestHeaders.Add('Content-Type: application/json');
  19.   jsonstream := TStringStream.Create;
  20.   hc.HTTPMethod('GET', api_url + 'IT-legalforms', jsonstream, [200, 202, 400, 401, 402, 403]); //sample endpoint
  21.   if hc.ResponseStatusCode div 100 = 2 then //if response OK
  22.     Memo1.Lines.Add(jsonstream.DataString)  //here I add response to TMemo, but you can process and get the information you need, just as I did below
  23.   else
  24.   begin
  25.     if jsonstream.DataString <> '' then
  26.     begin
  27.       jo := TJsonObject(GetJson(jsonstream.DataString));
  28.       if jo.Find('message') <> nil then
  29.         errmsg := jo.Find('message').AsString;
  30.       jo.Free;
  31.     end;
  32.     ShowMessage(Format('Error: [%d] %s' + #13#10 + 'OpenAPI Error Message: %s', [hc.ResponseStatusCode, hc.ResponseStatusText, errmsg]));
  33.   end;
  34.   jsonstream.Free;
  35.   hc.Free;
  36. end;                      
« Last Edit: July 02, 2024, 04:23:05 pm by paweld »
Best regards / Pozdrawiam
paweld

rnervi

  • New Member
  • *
  • Posts: 14
Re: openAPI client
« Reply #7 on: July 02, 2024, 04:17:36 pm »
oh ! many thanks
will analyze and test it
yes I know for the token from cpanel
ty

rnervi

  • New Member
  • *
  • Posts: 14
Re: openAPI client
« Reply #8 on: July 02, 2024, 04:23:51 pm »
for the correctness
must copy libeay32.dll and ssleay32.dll inside the dir for exe file, to avoid "cannot initialize ssl library"
and .. it works !
ty a lot (spent last 2 days in figure howto!)

gidesa

  • Full Member
  • ***
  • Posts: 114
Re: openAPI client
« Reply #9 on: July 02, 2024, 04:55:30 pm »
There is this library, for FPC and Delphi:

https://github.com/landgraf-dev/openai-delphi

rnervi

  • New Member
  • *
  • Posts: 14
Re: openAPI client
« Reply #10 on: July 02, 2024, 05:10:22 pm »
@paweld: worked like a charm ! now it's clear. ty again !

Thaddy

  • Hero Member
  • *****
  • Posts: 15641
  • Censorship about opinions does not belong here.
Re: openAPI client
« Reply #11 on: July 03, 2024, 10:51:21 am »
There is this library, for FPC and Delphi:

https://github.com/landgraf-dev/openai-delphi
That is quite, he uses my code I published one day after the API was officially released as beta. Christmas 2022.. Anyway, that is free to use without restrictions.
It originates from FreePascal for that reason.....(proof is on this forum)
There is a nice little anecdote: you need a developer key to use GPT and mine expired a couple of months later. Nice thing is I explained, showed what I did, proved my age (pensionado), and... got a new key... which does not seem to expire!. Thank you, OpenAI and Microsoft!!
« Last Edit: July 03, 2024, 10:59:24 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

abouchez

  • Full Member
  • ***
  • Posts: 118
    • Synopse
Re: openAPI client
« Reply #12 on: September 06, 2024, 06:16:36 pm »
I was not please with the Wagner code, clearly not really FPC friendly, and was not able to find Thaddy's original code.

So I made my own OpenAPI client generator:
https://blog.synopse.info/?post/2024/09/06/Swagger/OpenAPI-Client-Generator-for-Delphi-and-FPC
 ;D

Here are some features of our OpenAPI code generator for Delphi and FPC:
- Use high-level pascal records and dynamic arrays for "object" DTOs
- Use high-level pascal enumerations and sets for "enum" values
- Translate HTTP status error codes into high-level pascal Exceptions
- Recognize similar "properties" or "enum" to reuse the same pascal type
- Support of nested "$ref" for objects, parameters or types
- Support "allOf" attribute, with proper properties inheritance/overloading
- Support "oneOf" attribute, for strings or alternate record types
- Support of "in":"header" and "in":"cookie" parameter attributes
- Fallback to variant pascal type for "oneOf" or "anyOf" JSON values
- Generated source code units are very small and easy to use, read and debug
- Can generate very detailed comment documentation in the unit source code
- Tunable engine, with plenty of generation options (e.g. about verbosity)
- Leverage the mORMot RTTI and JSON kernel for its internal plumbing
- Compatible with FPC and oldest Delphi (7-2009)
- Tested with several Swagger 2 and OpenAPI 3 reference content
But still not fully compliant to all existing files: feedback is welcome!

The source code of the generator is a single unit:
https://github.com/synopse/mORMot2/blob/master/src/net/mormot.net.openapi.pas

And you will find several generated sample code units in the blog article, to make yourself your idea about the level of this little unit.

Feedback is welcome!

 

TinyPortal © 2005-2018