#1 2025-04-17 09:52:19

jiwi
Member
Registered: 2025-04-17
Posts: 4

OpenAPI client working example? Also with OAuth2?

Hello,

I've found the mORMot 2 OpenAPI client generator (which is by the way, awesome!) and managed to generate the client/dto units, but I'm stuck trying create an actual working client.

Does anybody have an example, maybe for the PetStore swagger demo?

Also, for the API that I'll be using I'll need to implement authorization with OAuth2 client_credentials flow (https://auth0.com/docs/get-started/auth … tials-flow). Are there any working examples for that maybe, or can someone point me in the right direction?

Thanks a lot!

Offline

#2 2025-04-17 12:17:44

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,005
Website

Re: OpenAPI client working example? Also with OAuth2?

It depends on how OAUth2 is actually implemented.

But in practice, you just add the JWT to the HTTP auth bearer of the TJsonClient, once you received it.

Offline

#3 2025-04-22 10:23:11

jiwi
Member
Registered: 2025-04-17
Posts: 4

Re: OpenAPI client working example? Also with OAuth2?

Thank you ab! Could you please elaborate on how do I add the bearer token to TJsonClient? I have found there is a TJsonClient.HttpOptions.AuthorizeBearer() call, is that it?
Also, if I want to send requests to the server directly, should I use TJsonClient.Http.Request?
How can I send application credentials like client secret/private key/etc.? Is there an option for that among TJsonClient parameters?

I have some example code, I tried to convert the PetStore swagger example, does this look okay?

const
  PetID = 61341;
  CategoryID = 61341;
  TagID = 61341;
var
  JsonClient: TJsonClient;
  Client: TPetStoreClient;
  Pet: TPet;
  Tag: TTag;
begin
  JsonClient := TJsonClient.Create('https://petstore.swagger.io/v2', '', 0);
  Client := TPetStoreClient.Create(JsonClient);

  Pet.Id := PetID;
  Pet.Name := 'Josephine';
  Pet.Status := epAvailable;
  Pet.Category.Id := CategoryID;
  Pet.Category.Name := 'Terrier Dogs';

  Tag.Id := TagID;
  Tag.Name := 'Terrier';
  SetLength(Pet.Tags, 1);
  Pet.Tags[0] := Tag;

  SetLength(Pet.PhotoUrls, 1);
  Pet.PhotoUrls[0] := 'http://dummy.com/dog.png';

  //Add new pet
  Client.AddPet(Pet);

  //Get pet info
  Pet := Client.GetPetById(PetID);

  //Delete the newly created pet
  Client.DeletePet(PetID, 'special-key');

Offline

#4 2025-04-26 09:33:06

flydev
Member
From: France
Registered: 2020-11-27
Posts: 81
Website

Re: OpenAPI client working example? Also with OAuth2?

Just use `SetBearer`, you can of course just set header manually.

Offline

#5 2025-04-29 10:30:12

jiwi
Member
Registered: 2025-04-17
Posts: 4

Re: OpenAPI client working example? Also with OAuth2?

Thank you flydev! So to set headers manually should I just send the request using TJsonClient.Http.Request?

Offline

#6 2025-04-29 11:41:51

flydev
Member
From: France
Registered: 2020-11-27
Posts: 81
Website

Re: OpenAPI client working example? Also with OAuth2?

I published a small example on github: flydev-fr/mormot2-jsonclient
and small comments project1.lpr#L29-L38

look how the client is used in `petstore.client` (generated by mopenapi)

Check request headers:

CemnG34.png

Last edited by flydev (2025-04-29 11:43:07)

Offline

#7 2025-05-05 13:26:57

jiwi
Member
Registered: 2025-04-17
Posts: 4

Re: OpenAPI client working example? Also with OAuth2?

Thank you so much flydev, you are amazing! :-)

Offline

Board footer

Powered by FluxBB