Created
September 6, 2024 15:01
-
-
Save synopse/b7e3251cb1aa9647939049ebb64405bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit petstore.api; | |
{$I mormot.defines.inc} | |
interface | |
{ | |
-------------------------------------------------------------------- | |
SWAGGER PETSTORE client as TPetStoreClient class | |
Generated 6 Sep 2024 by ab via mormot2tests - DO NOT MODIFY BY HAND! | |
-------------------------------------------------------------------- | |
} | |
uses | |
classes, | |
sysutils, | |
mormot.core.base, | |
mormot.core.unicode, | |
mormot.core.text, | |
mormot.core.buffers, | |
mormot.core.datetime, | |
mormot.core.rtti, | |
mormot.core.json, | |
mormot.core.variants, | |
mormot.net.client; | |
type | |
{ ************ Enumerations and Sets } | |
TEnumPetStore1 = ( | |
ep1None, ep1Available, ep1Pending, ep1Sold); | |
TEnumPetStore1Set = set of TEnumPetStore1; | |
TEnumPetStore2 = ( | |
ep2None, ep2Approved, ep2Delivered, ep2Placed); | |
{ ************ Data Transfert Objects } | |
TApiResponse = packed record | |
Code: integer; | |
_Type: RawUtf8; | |
Message: RawUtf8; | |
end; | |
PApiResponse = ^TApiResponse; | |
TCategory = packed record | |
Id: Int64; | |
Name: RawUtf8; | |
end; | |
PCategory = ^TCategory; | |
TTag = packed record | |
Id: Int64; | |
Name: RawUtf8; | |
end; | |
PTag = ^TTag; | |
TTagDynArray = array of TTag; | |
TPet = packed record | |
Id: Int64; | |
Category: TCategory; | |
Name: RawUtf8; | |
PhotoUrls: TRawUtf8DynArray; | |
Tags: TTagDynArray; | |
Status: TEnumPetStore1; | |
end; | |
PPet = ^TPet; | |
TPetDynArray = array of TPet; | |
TOrder = packed record | |
Id: Int64; | |
PetId: Int64; | |
Quantity: integer; | |
ShipDate: TDateTime; | |
Status: TEnumPetStore2; | |
Complete: boolean; | |
end; | |
POrder = ^TOrder; | |
TUser = packed record | |
Id: Int64; | |
Username: RawUtf8; | |
FirstName: RawUtf8; | |
LastName: RawUtf8; | |
Email: RawUtf8; | |
Password: RawUtf8; | |
Phone: RawUtf8; | |
UserStatus: integer; | |
end; | |
PUser = ^TUser; | |
TUserDynArray = array of TUser; | |
const | |
// define how enums/sets are actually transmitted as JSON array of string | |
ENUMPETSTORE1_TXT: array[TEnumPetStore1] of RawUtf8 = ( | |
'', 'available', 'pending', 'sold'); | |
ENUMPETSTORE2_TXT: array[TEnumPetStore2] of RawUtf8 = ( | |
'', 'approved', 'delivered', 'placed'); | |
type | |
{ ************ Main TPetStoreClient Class } | |
TPetStoreClient = class | |
private | |
fClient: IJsonClient; | |
public | |
// initialize this Client with an associated HTTP/JSON request | |
constructor Create(const aClient: IJsonClient = nil); | |
// pet methods | |
function UploadFile(PetId: Int64): TApiResponse; | |
procedure AddPet(const Payload: TPet); | |
procedure UpdatePet(const Payload: TPet); | |
function FindPetsByStatus(Status: TEnumPetStore1Set): TPetDynArray; | |
function FindPetsByTags_deprecated(const Tags: TRawUtf8DynArray): TPetDynArray; | |
function GetPetById(PetId: Int64): TPet; | |
procedure UpdatePetWithForm(PetId: Int64); | |
procedure DeletePet(PetId: Int64; const ApiKey: RawUtf8 = ''); | |
// store methods | |
function GetInventory(): variant; | |
function PlaceOrder(const Payload: TOrder): TOrder; | |
function GetOrderById(OrderId: Int64): TOrder; | |
procedure DeleteOrder(OrderId: Int64); | |
// user methods | |
procedure CreateUsersWithListInput(const Payload: TUserDynArray); | |
function GetUserByName(const Username: RawUtf8): TUser; | |
procedure UpdateUser(const Username: RawUtf8; const Payload: TUser); | |
procedure DeleteUser(const Username: RawUtf8); | |
function LoginUser(const Username: RawUtf8; const Password: RawUtf8): RawUtf8; | |
procedure LogoutUser(); | |
procedure CreateUsersWithArrayInput(const Payload: TUserDynArray); | |
procedure CreateUser(const Payload: TUser); | |
// access to the associated HTTP/JSON request | |
property JsonClient: IJsonClient | |
read fClient write fClient; | |
end; | |
implementation | |
{ ************ Main TPetStoreClient Class } | |
{ TPetStoreClient} | |
constructor TPetStoreClient.Create(const aClient: IJsonClient); | |
begin | |
fClient := aClient; | |
fClient.UrlEncoder := | |
[ueEncodeNames, ueSkipVoidString, ueSkipVoidValue, ueStarNameIsCsv]; | |
end; | |
function TPetStoreClient.UploadFile(PetId: Int64): TApiResponse; | |
begin | |
fClient.Request('POST', '/v2/pet/%/uploadImage', [PetId], [], [], | |
result, TypeInfo(TApiResponse)); | |
end; | |
procedure TPetStoreClient.AddPet(const Payload: TPet); | |
begin | |
fClient.Request('POST', '/v2/pet', [], [], [], | |
Payload, {dummy:}self, TypeInfo(TPet), nil); | |
end; | |
procedure TPetStoreClient.UpdatePet(const Payload: TPet); | |
begin | |
fClient.Request('PUT', '/v2/pet', [], [], [], | |
Payload, {dummy:}self, TypeInfo(TPet), nil); | |
end; | |
function TPetStoreClient.FindPetsByStatus(Status: TEnumPetStore1Set): TPetDynArray; | |
begin | |
fClient.Request('GET', '/v2/pet/findByStatus', [], [ | |
'*status', GetSetNameCustom(TypeInfo(TEnumPetStore1), Status, @ENUMPETSTORE1_TXT)], [], | |
result, TypeInfo(TPetDynArray)); | |
end; | |
function TPetStoreClient.FindPetsByTags_deprecated(const Tags: TRawUtf8DynArray): TPetDynArray; | |
begin | |
fClient.Request('GET', '/v2/pet/findByTags', [], [ | |
'*tags', RawUtf8ArrayToCsv(Tags)], [], | |
result, TypeInfo(TPetDynArray)); | |
end; | |
function TPetStoreClient.GetPetById(PetId: Int64): TPet; | |
begin | |
fClient.Request('GET', '/v2/pet/%', [PetId], [], [], | |
result, TypeInfo(TPet)); | |
end; | |
procedure TPetStoreClient.UpdatePetWithForm(PetId: Int64); | |
begin | |
fClient.Request('POST', '/v2/pet/%', [PetId], [], []); | |
end; | |
procedure TPetStoreClient.DeletePet(PetId: Int64; const ApiKey: RawUtf8); | |
begin | |
fClient.Request('DELETE', '/v2/pet/%', [PetId], [], [ | |
'api_key', ApiKey]); | |
end; | |
function TPetStoreClient.GetInventory(): variant; | |
begin | |
fClient.Request('GET', '/v2/store/inventory', [], [], [], | |
result, TypeInfo(variant)); | |
end; | |
function TPetStoreClient.PlaceOrder(const Payload: TOrder): TOrder; | |
begin | |
fClient.Request('POST', '/v2/store/order', [], [], [], | |
Payload, result, TypeInfo(TOrder), TypeInfo(TOrder)); | |
end; | |
function TPetStoreClient.GetOrderById(OrderId: Int64): TOrder; | |
begin | |
fClient.Request('GET', '/v2/store/order/%', [OrderId], [], [], | |
result, TypeInfo(TOrder)); | |
end; | |
procedure TPetStoreClient.DeleteOrder(OrderId: Int64); | |
begin | |
fClient.Request('DELETE', '/v2/store/order/%', [OrderId], [], []); | |
end; | |
procedure TPetStoreClient.CreateUsersWithListInput(const Payload: TUserDynArray); | |
begin | |
fClient.Request('POST', '/v2/user/createWithList', [], [], [], | |
Payload, {dummy:}self, TypeInfo(TUserDynArray), nil); | |
end; | |
function TPetStoreClient.GetUserByName(const Username: RawUtf8): TUser; | |
begin | |
fClient.Request('GET', '/v2/user/%', [Username], [], [], | |
result, TypeInfo(TUser)); | |
end; | |
procedure TPetStoreClient.UpdateUser(const Username: RawUtf8; const Payload: TUser); | |
begin | |
fClient.Request('PUT', '/v2/user/%', [Username], [], [], | |
Payload, {dummy:}self, TypeInfo(TUser), nil); | |
end; | |
procedure TPetStoreClient.DeleteUser(const Username: RawUtf8); | |
begin | |
fClient.Request('DELETE', '/v2/user/%', [Username], [], []); | |
end; | |
function TPetStoreClient.LoginUser(const Username: RawUtf8; const Password: RawUtf8): RawUtf8; | |
begin | |
fClient.Request('GET', '/v2/user/login', [], [ | |
'username', Username, | |
'password', Password], [], | |
result, TypeInfo(RawUtf8)); | |
end; | |
procedure TPetStoreClient.LogoutUser(); | |
begin | |
fClient.Request('GET', '/v2/user/logout', [], [], []); | |
end; | |
procedure TPetStoreClient.CreateUsersWithArrayInput(const Payload: TUserDynArray); | |
begin | |
fClient.Request('POST', '/v2/user/createWithArray', [], [], [], | |
Payload, {dummy:}self, TypeInfo(TUserDynArray), nil); | |
end; | |
procedure TPetStoreClient.CreateUser(const Payload: TUser); | |
begin | |
fClient.Request('POST', '/v2/user', [], [], [], | |
Payload, {dummy:}self, TypeInfo(TUser), nil); | |
end; | |
{ ************ Custom RTTI/JSON initialization } | |
const | |
// exact definition of the DTOs expected JSON serialization | |
_TApiResponse = 'code:integer type:RawUtf8 message:RawUtf8'; | |
_TCategory = 'id:Int64 name:RawUtf8'; | |
_TTag = 'id:Int64 name:RawUtf8'; | |
_TPet = 'id:Int64 category:TCategory name:RawUtf8 photoUrls:TRawUtf8DynArray ' + | |
'tags:array of TTag status:TEnumPetStore1'; | |
_TOrder = 'id:Int64 petId:Int64 quantity:integer shipDate:TDateTime status:TEnumPetStore2 ' + | |
'complete:boolean'; | |
_TUser = 'id:Int64 username:RawUtf8 firstName:RawUtf8 lastName:RawUtf8 ' + | |
'email:RawUtf8 password:RawUtf8 phone:RawUtf8 userStatus:integer'; | |
procedure RegisterRtti; | |
begin | |
TRttiJson.RegisterCustomEnumValues([ | |
TypeInfo(TEnumPetStore1), TypeInfo(TEnumPetStore1Set), @ENUMPETSTORE1_TXT, | |
TypeInfo(TEnumPetStore2), nil, @ENUMPETSTORE2_TXT]); | |
Rtti.RegisterFromText([ | |
TypeInfo(TApiResponse), _TApiResponse, | |
TypeInfo(TCategory), _TCategory, | |
TypeInfo(TTag), _TTag, | |
TypeInfo(TPet), _TPet, | |
TypeInfo(TOrder), _TOrder, | |
TypeInfo(TUser), _TUser]); | |
end; | |
initialization | |
RegisterRtti; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment