Skip to content

Instantly share code, notes, and snippets.

@synopse
Created September 6, 2024 14:38
Show Gist options
  • Save synopse/5274ed2ec2f4f11f339abf95624a5463 to your computer and use it in GitHub Desktop.
Save synopse/5274ed2ec2f4f11f339abf95624a5463 to your computer and use it in GitHub Desktop.
// Client unit for Swagger Petstore
// - This is a sample server Petstore server. You can find out more about Swagger
// at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).
// For this sample, you can use the api key `special-key` to test the authorization
// filters.
// - version 1.0.7
// - OpenAPI definition licensed under Apache 2.0 terms
unit petstore.client;
{$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,
petstore.dto;
type
{ ************ Main TPetStoreClient Class }
// Client class for Swagger Petstore
// - This is a sample server Petstore server. You can find out more about Swagger
// at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).
// For this sample, you can use the api key `special-key` to test the authorization
// filters.
TPetStoreClient = class
private
fClient: IJsonClient;
public
// initialize this Client with an associated HTTP/JSON request
constructor Create(const aClient: IJsonClient = nil);
// -------------
// PET METHODS
// -------------
// - Everything about your Pets
// uploadFile [post] /pet/{petId}/uploadImage
//
// Summary: uploads an image
//
// Params:
// - [path] PetId (required): ID of pet to update
// - [formData] AdditionalMetadata (unsupported): Additional data to pass to server
// - [formData] _File (unsupported): file to upload
//
// Responses:
// - 200 (main): successful operation
function UploadFile(PetId: Int64): TApiResponse;
// addPet [post] /pet
//
// Summary: Add a new pet to the store
//
// Params:
// - [body] Payload (required)
//
// Responses:
// - 405: Invalid input
procedure AddPet(const Payload: TPet);
// updatePet [put] /pet
//
// Summary: Update an existing pet
//
// Params:
// - [body] Payload (required)
//
// Responses:
// - 400: Invalid ID supplied
// - 404: Pet not found
// - 405: Validation exception
procedure UpdatePet(const Payload: TPet);
// findPetsByStatus [get] /pet/findByStatus
//
// Summary: Finds Pets by status
// Description:
// Multiple status values can be provided with comma separated strings
//
// Params:
// - [query] Status (required): Status values that need to be considered for filter
//
// Responses:
// - 200 (main): successful operation
// - 400: Invalid status value
function FindPetsByStatus(Status: TEnumPetStore1Set): TPetDynArray;
// [DEPRECATED] findPetsByTags [get] /pet/findByTags
//
// Summary: Finds Pets by tags
// Description:
// Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
//
// Params:
// - [query] Tags (required): Tags to filter by
//
// Responses:
// - 200 (main): successful operation
// - 400: Invalid tag value
function FindPetsByTags_deprecated(const Tags: TRawUtf8DynArray): TPetDynArray;
// getPetById [get] /pet/{petId}
//
// Summary: Find pet by ID
// Description:
// Returns a single pet
//
// Params:
// - [path] PetId (required): ID of pet to return
//
// Responses:
// - 200 (main): successful operation
// - 400: Invalid ID supplied
// - 404: Pet not found
function GetPetById(PetId: Int64): TPet;
// updatePetWithForm [post] /pet/{petId}
//
// Summary: Updates a pet in the store with form data
//
// Params:
// - [path] PetId (required): ID of pet that needs to be updated
// - [formData] Name (unsupported): Updated name of the pet
// - [formData] Status (unsupported): Updated status of the pet
//
// Responses:
// - 405: Invalid input
procedure UpdatePetWithForm(PetId: Int64);
// deletePet [delete] /pet/{petId}
//
// Summary: Deletes a pet
//
// Params:
// - [header] ApiKey
// - [path] PetId (required): Pet id to delete
//
// Responses:
// - 400: Invalid ID supplied
// - 404: Pet not found
procedure DeletePet(PetId: Int64; const ApiKey: RawUtf8 = '');
// ---------------
// STORE METHODS
// ---------------
// - Access to Petstore orders
// getInventory [get] /store/inventory
//
// Summary: Returns pet inventories by status
// Description:
// Returns a map of status codes to quantities
//
// Responses:
// - 200 (main): successful operation
function GetInventory(): variant;
// placeOrder [post] /store/order
//
// Summary: Place an order for a pet
//
// Params:
// - [body] Payload (required)
//
// Responses:
// - 200 (main): successful operation
// - 400: Invalid Order
function PlaceOrder(const Payload: TOrder): TOrder;
// getOrderById [get] /store/order/{orderId}
//
// Summary: Find purchase order by ID
// Description:
// For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions
//
// Params:
// - [path] OrderId (required): ID of pet that needs to be fetched
//
// Responses:
// - 200 (main): successful operation
// - 400: Invalid ID supplied
// - 404: Order not found
function GetOrderById(OrderId: Int64): TOrder;
// deleteOrder [delete] /store/order/{orderId}
//
// Summary: Delete purchase order by ID
// Description:
// For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors
//
// Params:
// - [path] OrderId (required): ID of the order that needs to be deleted
//
// Responses:
// - 400: Invalid ID supplied
// - 404: Order not found
procedure DeleteOrder(OrderId: Int64);
// --------------
// USER METHODS
// --------------
// - Operations about user
// createUsersWithListInput [post] /user/createWithList
//
// Summary: Creates list of users with given input array
//
// Params:
// - [body] Payload (required)
//
// Responses:
// - default (main): successful operation
procedure CreateUsersWithListInput(const Payload: TUserDynArray);
// getUserByName [get] /user/{username}
//
// Summary: Get user by user name
//
// Params:
// - [path] Username (required): The name that needs to be fetched. Use user1
// for testing.
//
// Responses:
// - 200 (main): successful operation
// - 400: Invalid username supplied
// - 404: User not found
function GetUserByName(const Username: RawUtf8): TUser;
// updateUser [put] /user/{username}
//
// Summary: Updated user
// Description:
// This can only be done by the logged in user.
//
// Params:
// - [path] Username (required): name that need to be updated
// - [body] Payload (required)
//
// Responses:
// - 400: Invalid user supplied
// - 404: User not found
procedure UpdateUser(const Username: RawUtf8; const Payload: TUser);
// deleteUser [delete] /user/{username}
//
// Summary: Delete user
// Description:
// This can only be done by the logged in user.
//
// Params:
// - [path] Username (required): The name that needs to be deleted
//
// Responses:
// - 400: Invalid username supplied
// - 404: User not found
procedure DeleteUser(const Username: RawUtf8);
// loginUser [get] /user/login
//
// Summary: Logs user into the system
//
// Params:
// - [query] Username (required): The user name for login
// - [query] Password (required): The password for login in clear text
//
// Responses:
// - 200 (main): successful operation
// - 400: Invalid username/password supplied
function LoginUser(const Username: RawUtf8; const Password: RawUtf8): RawUtf8;
// logoutUser [get] /user/logout
//
// Summary: Logs out current logged in user session
//
// Responses:
// - default (main): successful operation
procedure LogoutUser();
// createUsersWithArrayInput [post] /user/createWithArray
//
// Summary: Creates list of users with given input array
//
// Params:
// - [body] Payload (required)
//
// Responses:
// - default (main): successful operation
procedure CreateUsersWithArrayInput(const Payload: TUserDynArray);
// createUser [post] /user
//
// Summary: Create user
// Description:
// This can only be done by the logged in user.
//
// Params:
// - [body] Payload (required)
//
// Responses:
// - default (main): successful operation
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;
end.
// DTOs for Swagger Petstore
// - This is a sample server Petstore server. You can find out more about Swagger
// at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).
// For this sample, you can use the api key `special-key` to test the authorization
// filters.
// - version 1.0.7
// - OpenAPI definition licensed under Apache 2.0 terms
unit petstore.dto;
{$I mormot.defines.inc}
interface
{
--------------------------------------------------------------------
SWAGGER PETSTORE DTOs
Generated 6 Sep 2024 by ab via mormot2tests - DO NOT MODIFY BY HAND!
--------------------------------------------------------------------
}
uses
classes,
sysutils,
mormot.core.base,
mormot.core.rtti,
mormot.core.json;
type
{ ************ Enumerations and Sets }
// pet status in the store
TEnumPetStore1 = (
ep1None, ep1Available, ep1Pending, ep1Sold);
TEnumPetStore1Set = set of TEnumPetStore1;
// Order Status
TEnumPetStore2 = (
ep2None, ep2Approved, ep2Delivered, ep2Placed);
{ ************ Data Transfert Objects }
// from #/definitions/ApiResponse
TApiResponse = packed record
Code: integer;
_Type: RawUtf8;
Message: RawUtf8;
end;
PApiResponse = ^TApiResponse;
// from #/definitions/Category
TCategory = packed record
Id: Int64;
Name: RawUtf8;
end;
PCategory = ^TCategory;
// from #/definitions/Tag
TTag = packed record
Id: Int64;
Name: RawUtf8;
end;
PTag = ^TTag;
TTagDynArray = array of TTag;
// from #/definitions/Pet
TPet = packed record
Id: Int64;
Category: TCategory;
Name: RawUtf8;
PhotoUrls: TRawUtf8DynArray;
Tags: TTagDynArray;
// pet status in the store
Status: TEnumPetStore1;
end;
PPet = ^TPet;
TPetDynArray = array of TPet;
// from #/definitions/Order
TOrder = packed record
Id: Int64;
PetId: Int64;
Quantity: integer;
ShipDate: TDateTime;
// Order Status
Status: TEnumPetStore2;
Complete: boolean;
end;
POrder = ^TOrder;
// from #/definitions/User
TUser = packed record
Id: Int64;
Username: RawUtf8;
FirstName: RawUtf8;
LastName: RawUtf8;
Email: RawUtf8;
Password: RawUtf8;
Phone: RawUtf8;
// User Status
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');
implementation
{ ************ 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