From cdc83ffd161149a6eddd3f52aa636b88f81876fa Mon Sep 17 00:00:00 2001 From: Stephane Carrez Date: Sun, 8 Oct 2017 06:16:23 +0200 Subject: [PATCH] [Ada] Adding Ada client samples (#6634) * Add Ada client petstore samples - Add script to generate Ada client support with swagger-codegen - Add files to build the Ada sample - Add main program to use the generated client samples API and connect to the server to perform some operations * Add some description for the samples * Update the documentation to explain how to build, how to use the generated Ada client code --- bin/ada-petstore.sh | 35 ++ .../petstore/ada/.swagger-codegen-ignore | 23 ++ .../petstore/ada/.swagger-codegen/VERSION | 1 + samples/client/petstore/ada/README.md | 95 +++++ samples/client/petstore/ada/config.gpr | 88 +++++ samples/client/petstore/ada/petstore.gpr | 18 + .../src/client/samples-petstore-clients.adb | 355 ++++++++++++++++++ .../src/client/samples-petstore-clients.ads | 139 +++++++ .../src/client/samples-petstore-models.adb | 322 ++++++++++++++++ .../src/client/samples-petstore-models.ads | 216 +++++++++++ samples/client/petstore/ada/src/petstore.adb | 268 +++++++++++++ .../petstore/ada/src/samples-petstore.ads | 2 + samples/client/petstore/ada/src/samples.ads | 2 + 13 files changed, 1564 insertions(+) create mode 100755 bin/ada-petstore.sh create mode 100644 samples/client/petstore/ada/.swagger-codegen-ignore create mode 100644 samples/client/petstore/ada/.swagger-codegen/VERSION create mode 100644 samples/client/petstore/ada/README.md create mode 100644 samples/client/petstore/ada/config.gpr create mode 100644 samples/client/petstore/ada/petstore.gpr create mode 100644 samples/client/petstore/ada/src/client/samples-petstore-clients.adb create mode 100644 samples/client/petstore/ada/src/client/samples-petstore-clients.ads create mode 100644 samples/client/petstore/ada/src/client/samples-petstore-models.adb create mode 100644 samples/client/petstore/ada/src/client/samples-petstore-models.ads create mode 100644 samples/client/petstore/ada/src/petstore.adb create mode 100644 samples/client/petstore/ada/src/samples-petstore.ads create mode 100644 samples/client/petstore/ada/src/samples.ads diff --git a/bin/ada-petstore.sh b/bin/ada-petstore.sh new file mode 100755 index 00000000000..d46e5013378 --- /dev/null +++ b/bin/ada-petstore.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +SCRIPT="$0" + +while [ -h "$SCRIPT" ] ; do + ls=`ls -ld "$SCRIPT"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + SCRIPT="$link" + else + SCRIPT=`dirname "$SCRIPT"`/"$link" + fi +done + +if [ ! -d "${APP_DIR}" ]; then + APP_DIR=`dirname "$SCRIPT"`/.. + APP_DIR=`cd "${APP_DIR}"; pwd` +fi + +executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar" + +if [ ! -f "$executable" ] +then + mvn clean package +fi + +# if you've executed sbt assembly previously it will use that instead. +export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" +model="modules/swagger-codegen/src/test/resources/2_0/petstore.yaml" +ags="$@ generate --template-dir modules/swagger-codegen/src/main/resources/Ada -l ada" +ags="$ags -i $model -t modules/swagger-codegen/src/main/resources/Ada -o samples/client/petstore/ada" +ags="$ags -DprojectName=Petstore --model-package Samples.Petstore" + +java $JAVA_OPTS -jar $executable $ags +rm -rf samples/client/petstore/ada/src/server diff --git a/samples/client/petstore/ada/.swagger-codegen-ignore b/samples/client/petstore/ada/.swagger-codegen-ignore new file mode 100644 index 00000000000..c5fa491b4c5 --- /dev/null +++ b/samples/client/petstore/ada/.swagger-codegen-ignore @@ -0,0 +1,23 @@ +# Swagger Codegen Ignore +# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen + +# Use this file to prevent files from being overwritten by the generator. +# The patterns follow closely to .gitignore or .dockerignore. + +# As an example, the C# client generator defines ApiClient.cs. +# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line: +#ApiClient.cs + +# You can match any string of characters against a directory, file or extension with a single asterisk (*): +#foo/*/qux +# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux + +# You can recursively match patterns against a directory, file or extension with a double asterisk (**): +#foo/**/qux +# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux + +# You can also negate patterns with an exclamation (!). +# For example, you can ignore all files in a docs folder with the file extension .md: +#docs/*.md +# Then explicitly reverse the ignore rule for a single file: +#!docs/README.md diff --git a/samples/client/petstore/ada/.swagger-codegen/VERSION b/samples/client/petstore/ada/.swagger-codegen/VERSION new file mode 100644 index 00000000000..f9f7450d135 --- /dev/null +++ b/samples/client/petstore/ada/.swagger-codegen/VERSION @@ -0,0 +1 @@ +2.3.0-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/ada/README.md b/samples/client/petstore/ada/README.md new file mode 100644 index 00000000000..1a055b7a58b --- /dev/null +++ b/samples/client/petstore/ada/README.md @@ -0,0 +1,95 @@ +# Swagger Petstore Ada Client + +## Overview + +This Ada client uses the Petstore API to demonstrate how to use the generator +and use the generated Ada code. The following files are generated by +[Swagger Codegen](https://github.com/swagger-api/swagger-codegen): + +* src/client/samples-petstore-models.ads +* src/client/samples-petstore-models.adb +* src/client/samples-petstore-clients.ads +* src/client/samples-petstore-clients.adb + +The 'Models' package contains the definition of types used by the request or response +in the API operations. It also provides operations to serialize and deserialize these +objects in JSON, XML or form-based data streams. + +The 'Clients' package contains the definition of operations provided by the Petstore API. + +## Requirements. + +To build this sample, you must have installed the GNAT Ada compiler as well the following libraries: + +* Ada Util (https://github.com/stcarrez/ada-util) +* Swagger Ada (https://github.com/stcarrez/swagger-ada) +* AWS (http://libre.adacore.com/libre/tools/aws/) + +## Building the petstore client + +Build the petstore client by using the following command: +```sh +gprbuild -Ppetstore -p +``` + +## Using the Swagger Ada code + +### Initialization + +The HTTP/REST support is provided by [Ada Util](https://github.com/stcarrez/ada-util) +and encapsulated by [Swagger Ada](https://github.com/stcarrez/swagger-ada). If you want +to use Curl, you should initialize with the following: + +``` + Util.Http.Clients.Curl.Register; +``` + +But if you want to use [AWS](http://libre.adacore.com/libre/tools/aws/), you will initialize with: + + +``` + Util.Http.Clients.Web.Register; +``` + +After the initialization is done, you will declare a client instance to access +the API operations: + +``` + C : Samples.Petstore.Clients.Client_Type; +``` + +The 'Client_Type' is the generated type that will implement the operations +described in the OpenAPI description file. + +And you should initialize the server base URI you want to connect to: + +``` + C.Set_Server ("http://petstore.swagger.io/v2"); +``` + +At this stage, you can use the generated operation. + +### Calling an operation + +Let's retrieve some pet information by calling the 'Get_Pet_By_Id' operation. +This operation needs an integer as input parameter and returns a 'Pet_Type' +object that contains all the pet information. You will first declare +the pet instance as follows: + +``` + Pet : Samples.Petstore.Models.Pet_Type; +``` + +And then call the 'Get_Pet_By_Id' operation: + +``` + C.Get_Pet_By_Id (768, Pet); +``` + +At this stage, you can access information from the 'Pet' instance: + +``` + Ada.Text_IO.Put_Line ("Id : " & Swagger.Long'Image (Pet.Id)); + Ada.Text_IO.Put_Line ("Name : " & Swagger.To_String (Pet.Name)); + Ada.Text_IO.Put_Line ("Status : " & Swagger.To_String (Pet.Status)); +``` diff --git a/samples/client/petstore/ada/config.gpr b/samples/client/petstore/ada/config.gpr new file mode 100644 index 00000000000..d3533ef3398 --- /dev/null +++ b/samples/client/petstore/ada/config.gpr @@ -0,0 +1,88 @@ +abstract project Config is + for Source_Dirs use (); + + type Yes_No is ("yes", "no"); + + type Library_Type_Type is ("relocatable", "static"); + + type Mode_Type is ("distrib", "debug", "optimize", "profile"); + Mode : Mode_Type := external ("MODE", "debug"); + + Coverage : Yes_No := External ("COVERAGE", "no"); + Processors := External ("PROCESSORS", "1"); + + package Builder is + case Mode is + when "debug" => + for Default_Switches ("Ada") use ("-g", "-j" & Processors); + when others => + for Default_Switches ("Ada") use ("-g", "-O2", "-j" & Processors); + end case; + end Builder; + + package compiler is + warnings := ("-gnatwua"); + defaults := ("-gnat2012"); + case Mode is + when "distrib" => + for Default_Switches ("Ada") use defaults & ("-gnatafno", "-gnatVa", "-gnatwa"); + + when "debug" => + for Default_Switches ("Ada") use defaults & warnings + & ("-gnata", "-gnatVaMI", "-gnaty3abcefhiklmnprstxM99"); + + when "optimize" => + for Default_Switches ("Ada") use defaults & warnings + & ("-gnatn", "-gnatp", "-fdata-sections", "-ffunction-sections"); + + when "profile" => + for Default_Switches ("Ada") use defaults & warnings & ("-pg"); + end case; + + case Coverage is + when "yes" => + for Default_Switches ("ada") use Compiler'Default_Switches ("Ada") & + ("-fprofile-arcs", "-ftest-coverage"); + when others => + end case; + end compiler; + + package binder is + case Mode is + when "debug" => + for Default_Switches ("Ada") use ("-E"); + + when others => + for Default_Switches ("Ada") use ("-E"); + + end case; + end binder; + + package linker is + case Mode is + when "profile" => + for Default_Switches ("Ada") use ("-pg"); + + when "distrib" => + for Default_Switches ("Ada") use ("-s"); + + when "optimize" => + for Default_Switches ("Ada") use ("-Wl,--gc-sections"); + + when others => + null; + end case; + + case Coverage is + when "yes" => + for Default_Switches ("ada") use Linker'Default_Switches ("ada") & + ("-fprofile-arcs"); + when others => + end case; + end linker; + + package Ide is + for VCS_Kind use "git"; + end Ide; + +end Config; diff --git a/samples/client/petstore/ada/petstore.gpr b/samples/client/petstore/ada/petstore.gpr new file mode 100644 index 00000000000..ffcd99fd76c --- /dev/null +++ b/samples/client/petstore/ada/petstore.gpr @@ -0,0 +1,18 @@ +with "config"; +with "util"; +with "util_http"; +with "swagger"; +project Petstore is + + Mains := ("petstore.adb"); + for Main use Mains; + for Source_Dirs use ("src", "src/client"); + for Object_Dir use "./obj"; + for Exec_Dir use "./bin"; + + package Binder renames Config.Binder; + package Builder renames Config.Builder; + package Compiler renames Config.Compiler; + package Linker renames Config.Linker; + +end Petstore; diff --git a/samples/client/petstore/ada/src/client/samples-petstore-clients.adb b/samples/client/petstore/ada/src/client/samples-petstore-clients.adb new file mode 100644 index 00000000000..37e99fb6575 --- /dev/null +++ b/samples/client/petstore/ada/src/client/samples-petstore-clients.adb @@ -0,0 +1,355 @@ +-- 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. +-- +-- OpenAPI spec version: 1.0.0 +-- Contact: apiteam@swagger.io +-- +-- NOTE: This package is auto generated by the swagger code generator 2.3.0-SNAPSHOT. +-- https://github.com/swagger-api/swagger-codegen.git +-- Do not edit the class manually. +with Swagger.Streams; +package body Samples.Petstore.Clients is + + -- Add a new pet to the store + procedure Add_Pet + (Client : in out Client_Type; + P_Body : in Samples.Petstore.Models.Pet_Type) is + URI : Swagger.Clients.URI_Type; + Req : Swagger.Clients.Request_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + Client.Initialize (Req, (Swagger.Clients.APPLICATION_JSON, + Swagger.Clients.APPLICATION_XML)); + Samples.Petstore.Models.Serialize (Req.Stream, "body", P_Body); + + URI.Set_Path ("/pet"); + Client.Call (Swagger.Clients.POST, URI, Req); + end Add_Pet; + + -- Deletes a pet + procedure Delete_Pet + (Client : in out Client_Type; + Pet_Id : in Swagger.Long; + Api_Key : in Swagger.UString) is + URI : Swagger.Clients.URI_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + + URI.Set_Path ("/pet/{petId}"); + URI.Set_Path_Param ("petId", Swagger.To_String (Pet_Id)); + Client.Call (Swagger.Clients.DELETE, URI); + end Delete_Pet; + + -- Finds Pets by status + -- Multiple status values can be provided with comma separated strings + procedure Find_Pets_By_Status + (Client : in out Client_Type; + Status : in Swagger.UString_Vectors.Vector; + Result : out Samples.Petstore.Models.Pet_Type_Vectors.Vector) is + URI : Swagger.Clients.URI_Type; + Reply : Swagger.Value_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + + URI.Add_Param ("status", Status); + URI.Set_Path ("/pet/findByStatus"); + Client.Call (Swagger.Clients.GET, URI, Reply); + Samples.Petstore.Models.Deserialize (Reply, "", Result); + end Find_Pets_By_Status; + + -- Finds Pets by tags + -- Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. + procedure Find_Pets_By_Tags + (Client : in out Client_Type; + Tags : in Swagger.UString_Vectors.Vector; + Result : out Samples.Petstore.Models.Pet_Type_Vectors.Vector) is + URI : Swagger.Clients.URI_Type; + Reply : Swagger.Value_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + + URI.Add_Param ("tags", Tags); + URI.Set_Path ("/pet/findByTags"); + Client.Call (Swagger.Clients.GET, URI, Reply); + Samples.Petstore.Models.Deserialize (Reply, "", Result); + end Find_Pets_By_Tags; + + -- Find pet by ID + -- Returns a single pet + procedure Get_Pet_By_Id + (Client : in out Client_Type; + Pet_Id : in Swagger.Long; + Result : out Samples.Petstore.Models.Pet_Type) is + URI : Swagger.Clients.URI_Type; + Reply : Swagger.Value_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + + URI.Set_Path ("/pet/{petId}"); + URI.Set_Path_Param ("petId", Swagger.To_String (Pet_Id)); + Client.Call (Swagger.Clients.GET, URI, Reply); + Samples.Petstore.Models.Deserialize (Reply, "", Result); + end Get_Pet_By_Id; + + -- Update an existing pet + procedure Update_Pet + (Client : in out Client_Type; + P_Body : in Samples.Petstore.Models.Pet_Type) is + URI : Swagger.Clients.URI_Type; + Req : Swagger.Clients.Request_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + Client.Initialize (Req, (Swagger.Clients.APPLICATION_JSON, + Swagger.Clients.APPLICATION_XML)); + Samples.Petstore.Models.Serialize (Req.Stream, "body", P_Body); + + URI.Set_Path ("/pet"); + Client.Call (Swagger.Clients.PUT, URI, Req); + end Update_Pet; + + -- Updates a pet in the store with form data + procedure Update_Pet_With_Form + (Client : in out Client_Type; + Pet_Id : in Swagger.Long; + Name : in Swagger.UString; + Status : in Swagger.UString) is + URI : Swagger.Clients.URI_Type; + Req : Swagger.Clients.Request_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + Client.Initialize (Req, (1 => Swagger.Clients.APPLICATION_FORM)); + Req.Stream.Write_Entity ("name", Name); + Req.Stream.Write_Entity ("status", Status); + + URI.Set_Path ("/pet/{petId}"); + URI.Set_Path_Param ("petId", Swagger.To_String (Pet_Id)); + Client.Call (Swagger.Clients.POST, URI); + end Update_Pet_With_Form; + + -- uploads an image + procedure Upload_File + (Client : in out Client_Type; + Pet_Id : in Swagger.Long; + Additional_Metadata : in Swagger.UString; + File : in Swagger.Http_Content_Type; + Result : out Samples.Petstore.Models.ApiResponse_Type) is + URI : Swagger.Clients.URI_Type; + Req : Swagger.Clients.Request_Type; + Reply : Swagger.Value_Type; + begin + Client.Set_Accept ((1 => Swagger.Clients.APPLICATION_JSON)); + Client.Initialize (Req, (1 => Swagger.Clients.APPLICATION_FORM)); + Req.Stream.Write_Entity ("additionalMetadata", Additional_Metadata); + Req.Stream.Write_Entity ("file", File); + + URI.Set_Path ("/pet/{petId}/uploadImage"); + URI.Set_Path_Param ("petId", Swagger.To_String (Pet_Id)); + Client.Call (Swagger.Clients.POST, URI, Reply); + Samples.Petstore.Models.Deserialize (Reply, "", Result); + end Upload_File; + + -- Delete purchase order by ID + -- For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors + procedure Delete_Order + (Client : in out Client_Type; + Order_Id : in Swagger.UString) is + URI : Swagger.Clients.URI_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + + URI.Set_Path ("/store/order/{orderId}"); + URI.Set_Path_Param ("orderId", Order_Id); + Client.Call (Swagger.Clients.DELETE, URI); + end Delete_Order; + + -- Returns pet inventories by status + -- Returns a map of status codes to quantities + procedure Get_Inventory + (Client : in out Client_Type; + Result : out Swagger.Integer_Map) is + URI : Swagger.Clients.URI_Type; + Reply : Swagger.Value_Type; + begin + Client.Set_Accept ((1 => Swagger.Clients.APPLICATION_JSON)); + + URI.Set_Path ("/store/inventory"); + Client.Call (Swagger.Clients.GET, URI, Reply); + Swagger.Streams.Deserialize (Reply, "", Result); + end Get_Inventory; + + -- Find purchase order by ID + -- For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions + procedure Get_Order_By_Id + (Client : in out Client_Type; + Order_Id : in Swagger.Long; + Result : out Samples.Petstore.Models.Order_Type) is + URI : Swagger.Clients.URI_Type; + Reply : Swagger.Value_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + + URI.Set_Path ("/store/order/{orderId}"); + URI.Set_Path_Param ("orderId", Swagger.To_String (Order_Id)); + Client.Call (Swagger.Clients.GET, URI, Reply); + Samples.Petstore.Models.Deserialize (Reply, "", Result); + end Get_Order_By_Id; + + -- Place an order for a pet + procedure Place_Order + (Client : in out Client_Type; + P_Body : in Samples.Petstore.Models.Order_Type; + Result : out Samples.Petstore.Models.Order_Type) is + URI : Swagger.Clients.URI_Type; + Req : Swagger.Clients.Request_Type; + Reply : Swagger.Value_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + Client.Initialize (Req, (1 => Swagger.Clients.APPLICATION_JSON)); + Samples.Petstore.Models.Serialize (Req.Stream, "body", P_Body); + + URI.Set_Path ("/store/order"); + Client.Call (Swagger.Clients.POST, URI, Req, Reply); + Samples.Petstore.Models.Deserialize (Reply, "", Result); + end Place_Order; + + -- Create user + -- This can only be done by the logged in user. + procedure Create_User + (Client : in out Client_Type; + P_Body : in Samples.Petstore.Models.User_Type) is + URI : Swagger.Clients.URI_Type; + Req : Swagger.Clients.Request_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + Client.Initialize (Req, (1 => Swagger.Clients.APPLICATION_JSON)); + Samples.Petstore.Models.Serialize (Req.Stream, "body", P_Body); + + URI.Set_Path ("/user"); + Client.Call (Swagger.Clients.POST, URI, Req); + end Create_User; + + -- Creates list of users with given input array + procedure Create_Users_With_Array_Input + (Client : in out Client_Type; + P_Body : in Samples.Petstore.Models.User_Type_Vectors.Vector) is + URI : Swagger.Clients.URI_Type; + Req : Swagger.Clients.Request_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + Client.Initialize (Req, (1 => Swagger.Clients.APPLICATION_JSON)); + Samples.Petstore.Models.Serialize (Req.Stream, "body", P_Body); + + URI.Set_Path ("/user/createWithArray"); + Client.Call (Swagger.Clients.POST, URI, Req); + end Create_Users_With_Array_Input; + + -- Creates list of users with given input array + procedure Create_Users_With_List_Input + (Client : in out Client_Type; + P_Body : in Samples.Petstore.Models.User_Type_Vectors.Vector) is + URI : Swagger.Clients.URI_Type; + Req : Swagger.Clients.Request_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + Client.Initialize (Req, (1 => Swagger.Clients.APPLICATION_JSON)); + Samples.Petstore.Models.Serialize (Req.Stream, "body", P_Body); + + URI.Set_Path ("/user/createWithList"); + Client.Call (Swagger.Clients.POST, URI, Req); + end Create_Users_With_List_Input; + + -- Delete user + -- This can only be done by the logged in user. + procedure Delete_User + (Client : in out Client_Type; + Username : in Swagger.UString) is + URI : Swagger.Clients.URI_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + + URI.Set_Path ("/user/{username}"); + URI.Set_Path_Param ("username", Username); + Client.Call (Swagger.Clients.DELETE, URI); + end Delete_User; + + -- Get user by user name + procedure Get_User_By_Name + (Client : in out Client_Type; + Username : in Swagger.UString; + Result : out Samples.Petstore.Models.User_Type) is + URI : Swagger.Clients.URI_Type; + Reply : Swagger.Value_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + + URI.Set_Path ("/user/{username}"); + URI.Set_Path_Param ("username", Username); + Client.Call (Swagger.Clients.GET, URI, Reply); + Samples.Petstore.Models.Deserialize (Reply, "", Result); + end Get_User_By_Name; + + -- Logs user into the system + procedure Login_User + (Client : in out Client_Type; + Username : in Swagger.UString; + Password : in Swagger.UString; + Result : out Swagger.UString) is + URI : Swagger.Clients.URI_Type; + Reply : Swagger.Value_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + + URI.Add_Param ("username", Username); + URI.Add_Param ("password", Password); + URI.Set_Path ("/user/login"); + Client.Call (Swagger.Clients.GET, URI, Reply); + Swagger.Streams.Deserialize (Reply, "", Result); + end Login_User; + + -- Logs out current logged in user session + procedure Logout_User + (Client : in out Client_Type) is + URI : Swagger.Clients.URI_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + + URI.Set_Path ("/user/logout"); + Client.Call (Swagger.Clients.GET, URI); + end Logout_User; + + -- Updated user + -- This can only be done by the logged in user. + procedure Update_User + (Client : in out Client_Type; + Username : in Swagger.UString; + P_Body : in Samples.Petstore.Models.User_Type) is + URI : Swagger.Clients.URI_Type; + Req : Swagger.Clients.Request_Type; + begin + Client.Set_Accept ((Swagger.Clients.APPLICATION_XML, + Swagger.Clients.APPLICATION_JSON)); + Client.Initialize (Req, (1 => Swagger.Clients.APPLICATION_JSON)); + Samples.Petstore.Models.Serialize (Req.Stream, "body", P_Body); + + URI.Set_Path ("/user/{username}"); + URI.Set_Path_Param ("username", Username); + Client.Call (Swagger.Clients.PUT, URI, Req); + end Update_User; +end Samples.Petstore.Clients; diff --git a/samples/client/petstore/ada/src/client/samples-petstore-clients.ads b/samples/client/petstore/ada/src/client/samples-petstore-clients.ads new file mode 100644 index 00000000000..e10d1ab4cd1 --- /dev/null +++ b/samples/client/petstore/ada/src/client/samples-petstore-clients.ads @@ -0,0 +1,139 @@ +-- 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. +-- +-- OpenAPI spec version: 1.0.0 +-- Contact: apiteam@swagger.io +-- +-- NOTE: This package is auto generated by the swagger code generator 2.3.0-SNAPSHOT. +-- https://github.com/swagger-api/swagger-codegen.git +-- Do not edit the class manually. +with Samples.Petstore.Models; +with Swagger.Clients; +package Samples.Petstore.Clients is + + type Client_Type is new Swagger.Clients.Client_Type with null record; + + -- Add a new pet to the store + procedure Add_Pet + (Client : in out Client_Type; + P_Body : in Samples.Petstore.Models.Pet_Type); + + -- Deletes a pet + procedure Delete_Pet + (Client : in out Client_Type; + Pet_Id : in Swagger.Long; + Api_Key : in Swagger.UString); + + -- Finds Pets by status + -- Multiple status values can be provided with comma separated strings + procedure Find_Pets_By_Status + (Client : in out Client_Type; + Status : in Swagger.UString_Vectors.Vector; + Result : out Samples.Petstore.Models.Pet_Type_Vectors.Vector); + + -- Finds Pets by tags + -- Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. + procedure Find_Pets_By_Tags + (Client : in out Client_Type; + Tags : in Swagger.UString_Vectors.Vector; + Result : out Samples.Petstore.Models.Pet_Type_Vectors.Vector); + + -- Find pet by ID + -- Returns a single pet + procedure Get_Pet_By_Id + (Client : in out Client_Type; + Pet_Id : in Swagger.Long; + Result : out Samples.Petstore.Models.Pet_Type); + + -- Update an existing pet + procedure Update_Pet + (Client : in out Client_Type; + P_Body : in Samples.Petstore.Models.Pet_Type); + + -- Updates a pet in the store with form data + procedure Update_Pet_With_Form + (Client : in out Client_Type; + Pet_Id : in Swagger.Long; + Name : in Swagger.UString; + Status : in Swagger.UString); + + -- uploads an image + procedure Upload_File + (Client : in out Client_Type; + Pet_Id : in Swagger.Long; + Additional_Metadata : in Swagger.UString; + File : in Swagger.Http_Content_Type; + Result : out Samples.Petstore.Models.ApiResponse_Type); + + -- Delete purchase order by ID + -- For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors + procedure Delete_Order + (Client : in out Client_Type; + Order_Id : in Swagger.UString); + + -- Returns pet inventories by status + -- Returns a map of status codes to quantities + procedure Get_Inventory + (Client : in out Client_Type; + Result : out Swagger.Integer_Map); + + -- Find purchase order by ID + -- For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions + procedure Get_Order_By_Id + (Client : in out Client_Type; + Order_Id : in Swagger.Long; + Result : out Samples.Petstore.Models.Order_Type); + + -- Place an order for a pet + procedure Place_Order + (Client : in out Client_Type; + P_Body : in Samples.Petstore.Models.Order_Type; + Result : out Samples.Petstore.Models.Order_Type); + + -- Create user + -- This can only be done by the logged in user. + procedure Create_User + (Client : in out Client_Type; + P_Body : in Samples.Petstore.Models.User_Type); + + -- Creates list of users with given input array + procedure Create_Users_With_Array_Input + (Client : in out Client_Type; + P_Body : in Samples.Petstore.Models.User_Type_Vectors.Vector); + + -- Creates list of users with given input array + procedure Create_Users_With_List_Input + (Client : in out Client_Type; + P_Body : in Samples.Petstore.Models.User_Type_Vectors.Vector); + + -- Delete user + -- This can only be done by the logged in user. + procedure Delete_User + (Client : in out Client_Type; + Username : in Swagger.UString); + + -- Get user by user name + procedure Get_User_By_Name + (Client : in out Client_Type; + Username : in Swagger.UString; + Result : out Samples.Petstore.Models.User_Type); + + -- Logs user into the system + procedure Login_User + (Client : in out Client_Type; + Username : in Swagger.UString; + Password : in Swagger.UString; + Result : out Swagger.UString); + + -- Logs out current logged in user session + procedure Logout_User + (Client : in out Client_Type); + + -- Updated user + -- This can only be done by the logged in user. + procedure Update_User + (Client : in out Client_Type; + Username : in Swagger.UString; + P_Body : in Samples.Petstore.Models.User_Type); + +end Samples.Petstore.Clients; diff --git a/samples/client/petstore/ada/src/client/samples-petstore-models.adb b/samples/client/petstore/ada/src/client/samples-petstore-models.adb new file mode 100644 index 00000000000..2c2cf97126d --- /dev/null +++ b/samples/client/petstore/ada/src/client/samples-petstore-models.adb @@ -0,0 +1,322 @@ +-- 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. +-- +-- OpenAPI spec version: 1.0.0 +-- Contact: apiteam@swagger.io +-- +-- NOTE: This package is auto generated by the swagger code generator 2.3.0-SNAPSHOT. +-- https://github.com/swagger-api/swagger-codegen.git +-- Do not edit the class manually. + +package body Samples.Petstore.Models is + + use Swagger.Streams; + + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in ApiResponse_Type) is + begin + Into.Start_Entity (Name); + Into.Write_Entity ("code", Value.Code); + Into.Write_Entity ("type", Value.P_Type); + Into.Write_Entity ("message", Value.Message); + Into.End_Entity (Name); + end Serialize; + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in ApiResponse_Type_Vectors.Vector) is + begin + Into.Start_Array (Name); + for Item of Value loop + Serialize (Into, "", Item); + end loop; + Into.End_Array (Name); + end Serialize; + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out ApiResponse_Type) is + Object : Swagger.Value_Type; + begin + Swagger.Streams.Deserialize (From, Name, Object); + Swagger.Streams.Deserialize (Object, "code", Value.Code); + Swagger.Streams.Deserialize (Object, "type", Value.P_Type); + Swagger.Streams.Deserialize (Object, "message", Value.Message); + end Deserialize; + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out ApiResponse_Type_Vectors.Vector) is + List : Swagger.Value_Array_Type; + Item : ApiResponse_Type; + begin + Value.Clear; + Swagger.Streams.Deserialize (From, Name, List); + for Data of List loop + Deserialize (Data, "", Item); + Value.Append (Item); + end loop; + end Deserialize; + + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Category_Type) is + begin + Into.Start_Entity (Name); + Serialize (Into, "id", Value.Id); + Into.Write_Entity ("name", Value.Name); + Into.End_Entity (Name); + end Serialize; + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Category_Type_Vectors.Vector) is + begin + Into.Start_Array (Name); + for Item of Value loop + Serialize (Into, "", Item); + end loop; + Into.End_Array (Name); + end Serialize; + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Category_Type) is + Object : Swagger.Value_Type; + begin + Swagger.Streams.Deserialize (From, Name, Object); + Swagger.Streams.Deserialize (Object, "id", Value.Id); + Swagger.Streams.Deserialize (Object, "name", Value.Name); + end Deserialize; + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Category_Type_Vectors.Vector) is + List : Swagger.Value_Array_Type; + Item : Category_Type; + begin + Value.Clear; + Swagger.Streams.Deserialize (From, Name, List); + for Data of List loop + Deserialize (Data, "", Item); + Value.Append (Item); + end loop; + end Deserialize; + + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Tag_Type) is + begin + Into.Start_Entity (Name); + Serialize (Into, "id", Value.Id); + Into.Write_Entity ("name", Value.Name); + Into.End_Entity (Name); + end Serialize; + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Tag_Type_Vectors.Vector) is + begin + Into.Start_Array (Name); + for Item of Value loop + Serialize (Into, "", Item); + end loop; + Into.End_Array (Name); + end Serialize; + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Tag_Type) is + Object : Swagger.Value_Type; + begin + Swagger.Streams.Deserialize (From, Name, Object); + Swagger.Streams.Deserialize (Object, "id", Value.Id); + Swagger.Streams.Deserialize (Object, "name", Value.Name); + end Deserialize; + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Tag_Type_Vectors.Vector) is + List : Swagger.Value_Array_Type; + Item : Tag_Type; + begin + Value.Clear; + Swagger.Streams.Deserialize (From, Name, List); + for Data of List loop + Deserialize (Data, "", Item); + Value.Append (Item); + end loop; + end Deserialize; + + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in User_Type) is + begin + Into.Start_Entity (Name); + Serialize (Into, "id", Value.Id); + Into.Write_Entity ("username", Value.Username); + Into.Write_Entity ("firstName", Value.First_Name); + Into.Write_Entity ("lastName", Value.Last_Name); + Into.Write_Entity ("email", Value.Email); + Into.Write_Entity ("password", Value.Password); + Into.Write_Entity ("phone", Value.Phone); + Into.Write_Entity ("userStatus", Value.User_Status); + Into.End_Entity (Name); + end Serialize; + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in User_Type_Vectors.Vector) is + begin + Into.Start_Array (Name); + for Item of Value loop + Serialize (Into, "", Item); + end loop; + Into.End_Array (Name); + end Serialize; + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out User_Type) is + Object : Swagger.Value_Type; + begin + Swagger.Streams.Deserialize (From, Name, Object); + Swagger.Streams.Deserialize (Object, "id", Value.Id); + Swagger.Streams.Deserialize (Object, "username", Value.Username); + Swagger.Streams.Deserialize (Object, "firstName", Value.First_Name); + Swagger.Streams.Deserialize (Object, "lastName", Value.Last_Name); + Swagger.Streams.Deserialize (Object, "email", Value.Email); + Swagger.Streams.Deserialize (Object, "password", Value.Password); + Swagger.Streams.Deserialize (Object, "phone", Value.Phone); + Swagger.Streams.Deserialize (Object, "userStatus", Value.User_Status); + end Deserialize; + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out User_Type_Vectors.Vector) is + List : Swagger.Value_Array_Type; + Item : User_Type; + begin + Value.Clear; + Swagger.Streams.Deserialize (From, Name, List); + for Data of List loop + Deserialize (Data, "", Item); + Value.Append (Item); + end loop; + end Deserialize; + + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Order_Type) is + begin + Into.Start_Entity (Name); + Serialize (Into, "id", Value.Id); + Serialize (Into, "petId", Value.Pet_Id); + Into.Write_Entity ("quantity", Value.Quantity); + Into.Write_Entity ("shipDate", Value.Ship_Date); + Into.Write_Entity ("status", Value.Status); + Into.Write_Entity ("complete", Value.Complete); + Into.End_Entity (Name); + end Serialize; + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Order_Type_Vectors.Vector) is + begin + Into.Start_Array (Name); + for Item of Value loop + Serialize (Into, "", Item); + end loop; + Into.End_Array (Name); + end Serialize; + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Order_Type) is + Object : Swagger.Value_Type; + begin + Swagger.Streams.Deserialize (From, Name, Object); + Swagger.Streams.Deserialize (Object, "id", Value.Id); + Swagger.Streams.Deserialize (Object, "petId", Value.Pet_Id); + Swagger.Streams.Deserialize (Object, "quantity", Value.Quantity); + Deserialize (Object, "shipDate", Value.Ship_Date); + Swagger.Streams.Deserialize (Object, "status", Value.Status); + Swagger.Streams.Deserialize (Object, "complete", Value.Complete); + end Deserialize; + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Order_Type_Vectors.Vector) is + List : Swagger.Value_Array_Type; + Item : Order_Type; + begin + Value.Clear; + Swagger.Streams.Deserialize (From, Name, List); + for Data of List loop + Deserialize (Data, "", Item); + Value.Append (Item); + end loop; + end Deserialize; + + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Pet_Type) is + begin + Into.Start_Entity (Name); + Serialize (Into, "id", Value.Id); + Serialize (Into, "category", Value.Category); + Into.Write_Entity ("name", Value.Name); + Serialize (Into, "photoUrls", Value.Photo_Urls); + Serialize (Into, "tags", Value.Tags); + Into.Write_Entity ("status", Value.Status); + Into.End_Entity (Name); + end Serialize; + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Pet_Type_Vectors.Vector) is + begin + Into.Start_Array (Name); + for Item of Value loop + Serialize (Into, "", Item); + end loop; + Into.End_Array (Name); + end Serialize; + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Pet_Type) is + Object : Swagger.Value_Type; + begin + Swagger.Streams.Deserialize (From, Name, Object); + Swagger.Streams.Deserialize (Object, "id", Value.Id); + Deserialize (Object, "category", Value.Category); + Swagger.Streams.Deserialize (Object, "name", Value.Name); + Swagger.Streams.Deserialize (Object, "photoUrls", Value.Photo_Urls); + Deserialize (Object, "tags", Value.Tags); + Swagger.Streams.Deserialize (Object, "status", Value.Status); + end Deserialize; + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Pet_Type_Vectors.Vector) is + List : Swagger.Value_Array_Type; + Item : Pet_Type; + begin + Value.Clear; + Swagger.Streams.Deserialize (From, Name, List); + for Data of List loop + Deserialize (Data, "", Item); + Value.Append (Item); + end loop; + end Deserialize; + + +end Samples.Petstore.Models; diff --git a/samples/client/petstore/ada/src/client/samples-petstore-models.ads b/samples/client/petstore/ada/src/client/samples-petstore-models.ads new file mode 100644 index 00000000000..7bf3095598f --- /dev/null +++ b/samples/client/petstore/ada/src/client/samples-petstore-models.ads @@ -0,0 +1,216 @@ +-- 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. +-- +-- OpenAPI spec version: 1.0.0 +-- Contact: apiteam@swagger.io +-- +-- NOTE: This package is auto generated by the swagger code generator 2.3.0-SNAPSHOT. +-- https://github.com/swagger-api/swagger-codegen.git +-- Do not edit the class manually. +with Swagger.Streams; +with Ada.Containers.Vectors; +package Samples.Petstore.Models is + + + -- ------------------------------ + -- An uploaded response + -- Describes the result of uploading an image resource + -- ------------------------------ + type ApiResponse_Type is + record + Code : Integer; + P_Type : Swagger.UString; + Message : Swagger.UString; + end record; + + package ApiResponse_Type_Vectors is + new Ada.Containers.Vectors (Index_Type => Positive, + Element_Type => ApiResponse_Type); + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in ApiResponse_Type); + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in ApiResponse_Type_Vectors.Vector); + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out ApiResponse_Type); + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out ApiResponse_Type_Vectors.Vector); + + + -- ------------------------------ + -- Pet catehgry + -- A category for a pet + -- ------------------------------ + type Category_Type is + record + Id : Swagger.Long; + Name : Swagger.UString; + end record; + + package Category_Type_Vectors is + new Ada.Containers.Vectors (Index_Type => Positive, + Element_Type => Category_Type); + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Category_Type); + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Category_Type_Vectors.Vector); + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Category_Type); + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Category_Type_Vectors.Vector); + + + -- ------------------------------ + -- Pet Tag + -- A tag for a pet + -- ------------------------------ + type Tag_Type is + record + Id : Swagger.Long; + Name : Swagger.UString; + end record; + + package Tag_Type_Vectors is + new Ada.Containers.Vectors (Index_Type => Positive, + Element_Type => Tag_Type); + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Tag_Type); + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Tag_Type_Vectors.Vector); + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Tag_Type); + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Tag_Type_Vectors.Vector); + + + -- ------------------------------ + -- a User + -- A User who is purchasing from the pet store + -- ------------------------------ + type User_Type is + record + Id : Swagger.Long; + Username : Swagger.UString; + First_Name : Swagger.UString; + Last_Name : Swagger.UString; + Email : Swagger.UString; + Password : Swagger.UString; + Phone : Swagger.UString; + User_Status : Integer; + end record; + + package User_Type_Vectors is + new Ada.Containers.Vectors (Index_Type => Positive, + Element_Type => User_Type); + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in User_Type); + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in User_Type_Vectors.Vector); + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out User_Type); + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out User_Type_Vectors.Vector); + + + -- ------------------------------ + -- Pet Order + -- An order for a pets from the pet store + -- ------------------------------ + type Order_Type is + record + Id : Swagger.Long; + Pet_Id : Swagger.Long; + Quantity : Integer; + Ship_Date : Swagger.Datetime; + Status : Swagger.UString; + Complete : Boolean; + end record; + + package Order_Type_Vectors is + new Ada.Containers.Vectors (Index_Type => Positive, + Element_Type => Order_Type); + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Order_Type); + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Order_Type_Vectors.Vector); + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Order_Type); + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Order_Type_Vectors.Vector); + + + -- ------------------------------ + -- a Pet + -- A pet for sale in the pet store + -- ------------------------------ + type Pet_Type is + record + Id : Swagger.Long; + Category : Samples.Petstore.Models.Category_Type; + Name : Swagger.UString; + Photo_Urls : Swagger.UString_Vectors.Vector; + Tags : Samples.Petstore.Models.Tag_Type_Vectors.Vector; + Status : Swagger.UString; + end record; + + package Pet_Type_Vectors is + new Ada.Containers.Vectors (Index_Type => Positive, + Element_Type => Pet_Type); + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Pet_Type); + + procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class; + Name : in String; + Value : in Pet_Type_Vectors.Vector); + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Pet_Type); + + procedure Deserialize (From : in Swagger.Value_Type; + Name : in String; + Value : out Pet_Type_Vectors.Vector); + + +end Samples.Petstore.Models; diff --git a/samples/client/petstore/ada/src/petstore.adb b/samples/client/petstore/ada/src/petstore.adb new file mode 100644 index 00000000000..b14200f0001 --- /dev/null +++ b/samples/client/petstore/ada/src/petstore.adb @@ -0,0 +1,268 @@ +with Samples.Petstore.Clients; +with Samples.Petstore.Models; +with Swagger; +with Util.Http.Clients.Curl; +with Ada.Text_IO; +with Ada.Command_Line; +with Ada.Calendar.Formatting; +with Ada.Exceptions; +procedure Test is + + use Ada.Text_IO; + + procedure Usage; + procedure Print_Pet (Pet : in Samples.Petstore.Models.Pet_Type); + procedure Print_Order (Order : in Samples.Petstore.Models.Order_Type); + procedure Get_User (C : in out Samples.Petstore.Clients.Client_Type); + procedure Get_Pet (C : in out Samples.Petstore.Clients.Client_Type); + procedure Get_Order (C : in out Samples.Petstore.Clients.Client_Type); + procedure List_Inventory (C : in out Samples.Petstore.Clients.Client_Type); + procedure List_Pet (C : in out Samples.Petstore.Clients.Client_Type); + procedure Delete_Pet (C : in out Samples.Petstore.Clients.Client_Type); + procedure Delete_User (C : in out Samples.Petstore.Clients.Client_Type); + procedure Delete_Order (C : in out Samples.Petstore.Clients.Client_Type); + procedure Add_Pet (C : in out Samples.Petstore.Clients.Client_Type); + procedure Login (C : in out Samples.Petstore.Clients.Client_Type; + Username : in String; + Password : in String); + + Server : constant Swagger.UString := Swagger.To_UString ("http://petstore.swagger.io/v2"); + Arg_Count : constant Natural := Ada.Command_Line.Argument_Count; + Arg : Positive := 1; + + procedure Usage is + begin + Put_Line ("Usage: petstore {list|add|rm|update} {user|order|pet} {params}..."); + Put_Line (" get pet ... Print pet given its id"); + Put_Line (" get user ... Print user given its name"); + Put_Line (" get order ... Print order given its id"); + Put_Line (" list pet ... List the pets with the given status"); + Put_Line (" list inventory List the inventory"); + Put_Line (" add pet ... Remove user with given name"); + Put_Line (" rm order ... Remove order with given id"); + Put_Line (" login Use login operation to get a session"); + end Usage; + + procedure Print_Pet (Pet : in Samples.Petstore.Models.Pet_Type) is + Need_Indent : Boolean := False; + begin + Put_Line ("Id : " & Swagger.Long'Image (Pet.Id)); + Put_Line ("Name : " & Swagger.To_String (Pet.Name)); + Put_Line ("Status : " & Swagger.To_String (Pet.Status)); + if not Pet.Tags.Is_Empty then + Put ("Tags : "); + for Tag of Pet.Tags loop + Put_Line ((if Need_Indent then " " else "") + & Swagger.To_String (Tag.Name)); + Need_Indent := True; + end loop; + end if; + if not Pet.Photo_Urls.Is_Empty then + Need_Indent := False; + Put ("URLs : "); + for Url of Pet.Photo_Urls loop + Put_Line ((if Need_Indent then " " else "") & Url); + Need_Indent := True; + end loop; + end if; + end Print_Pet; + + procedure Print_Order (Order : in Samples.Petstore.Models.Order_Type) is + begin + Put_Line ("Id : " & Swagger.Long'Image (Order.Id)); + Put_Line ("Pet id : " & Swagger.Long'Image (Order.Pet_Id)); + Put_Line ("Quantity : " & Integer'Image (Order.Quantity)); + Put_Line ("Status : " & Swagger.To_String (Order.Status)); + Put_Line ("Ship date : " & Ada.Calendar.Formatting.Image (Order.Ship_Date)); + Put_Line ("Complete : " & Boolean'Image (Order.Complete)); + end Print_Order; + + procedure Get_User (C : in out Samples.Petstore.Clients.Client_Type) is + + User : Samples.Petstore.Models.User_Type; + begin + for I in Arg .. Arg_Count loop + C.Get_User_By_Name (Swagger.To_UString (Ada.Command_Line.Argument (I)), User); + Put_Line ("Id : " & Swagger.Long'Image (User.Id)); + Put_Line ("Username : " & Swagger.To_String (User.Username)); + Put_Line ("Firstname: " & Swagger.To_String (User.First_Name)); + Put_Line ("Lastname : " & Swagger.To_String (User.Last_Name)); + Put_Line ("Email : " & Swagger.To_String (User.Email)); + Put_Line ("Password : " & Swagger.To_String (User.Password)); + Put_Line ("Phone : " & Swagger.To_String (User.Phone)); + end loop; + end Get_User; + + procedure Get_Pet (C : in out Samples.Petstore.Clients.Client_Type) is + Pet : Samples.Petstore.Models.Pet_Type; + begin + C.Set_Server (Server); + for I in Arg .. Arg_Count loop + declare + P : constant String := Ada.Command_Line.Argument (I); + begin + C.Get_Pet_By_Id (Swagger.Long'Value (P), Pet); + Print_Pet (Pet); + end; + end loop; + end Get_Pet; + + procedure Get_Order (C : in out Samples.Petstore.Clients.Client_Type) is + Order : Samples.Petstore.Models.Order_Type; + begin + C.Set_Server (Server); + for I in Arg .. Arg_Count loop + declare + P : constant String := Ada.Command_Line.Argument (I); + begin + C.Get_Order_By_Id (Swagger.Long'Value (P), Order); + Print_Order (Order); + end; + end loop; + end Get_Order; + + procedure List_Pet (C : in out Samples.Petstore.Clients.Client_Type) is + Pets : Samples.Petstore.Models.Pet_Type_Vectors.Vector; + begin + for I in Arg .. Arg_Count loop + declare + Status : Swagger.UString_Vectors.Vector; + P : constant String := Ada.Command_Line.Argument (I); + begin + Status.Append (P); + C.Find_Pets_By_Status (Status, Pets); + for Pet of Pets loop + Print_Pet (Pet); + end loop; + end; + end loop; + end List_Pet; + + procedure List_Inventory (C : in out Samples.Petstore.Clients.Client_Type) is + List : Swagger.Integer_Map; + Iter : Swagger.Integer_Maps.Cursor; + begin + C.Get_Inventory (List); + Ada.Text_IO.Put_Line ("Inventory size " & Natural'Image (Natural (List.Length))); + Iter := List.First; + while Swagger.Integer_Maps.Has_Element (Iter) loop + Put (Swagger.Integer_Maps.Key (Iter)); + Set_Col (70); + Put_Line (Natural'Image (Swagger.Integer_Maps.Element (Iter))); + Swagger.Integer_Maps.Next (Iter); + end loop; + end List_Inventory; + + procedure Login (C : in out Samples.Petstore.Clients.Client_Type; + Username : in String; + Password : in String) is + Session : Swagger.UString; + begin + C.Login_User (Swagger.To_UString (Username), + Swagger.To_UString (Password), + Session); + Put_Line ("New session : " & Swagger.To_String (Session)); + end Login; + + procedure Add_Pet (C : in out Samples.Petstore.Clients.Client_Type) is + Pet : Samples.Petstore.Models.Pet_Type; + begin + if Arg_Count /= 7 then + Put_Line ("Missing some arguments for add pet command"); + Usage; + return; + end if; + Pet.Id := Swagger.Long'Value (Ada.Command_Line.Argument (Arg)); + Pet.Name := Swagger.To_UString (Ada.Command_Line.Argument (Arg + 1)); + Pet.Status := Swagger.To_UString (Ada.Command_Line.Argument (Arg + 2)); + Pet.Category.Id := Swagger.Long'Value (Ada.Command_Line.Argument (Arg + 3)); + Pet.Category.Name := Swagger.To_UString (Ada.Command_Line.Argument (Arg + 4)); + C.Add_Pet (Pet); + end Add_Pet; + + procedure Delete_User (C : in out Samples.Petstore.Clients.Client_Type) is + begin + for I in Arg .. Arg_Count loop + C.Delete_User (Username => Swagger.To_UString (Ada.Command_Line.Argument (I))); + end loop; + end Delete_User; + + procedure Delete_Order (C : in out Samples.Petstore.Clients.Client_Type) is + begin + for I in Arg .. Arg_Count loop + C.Delete_Order (Swagger.To_UString (Ada.Command_Line.Argument (I))); + end loop; + end Delete_Order; + + procedure Delete_Pet (C : in out Samples.Petstore.Clients.Client_Type) is + Key : constant Swagger.UString := Swagger.To_UString (Ada.Command_Line.Argument (Arg)); + begin + Arg := Arg + 1; + for I in Arg .. Arg_Count loop + C.Delete_Pet (Swagger.Long'Value (Ada.Command_Line.Argument (I)), Key); + end loop; + end Delete_Pet; + +begin + if Arg_Count <= 1 then + Usage; + return; + end if; + Util.Http.Clients.Curl.Register; + declare + Command : constant String := Ada.Command_Line.Argument (Arg); + Item : constant String := Ada.Command_Line.Argument (Arg + 1); + C : Samples.Petstore.Clients.Client_Type; + begin + C.Set_Server (Server); + Arg := Arg + 2; + if Command = "login" then + Login (C, Item, Ada.Command_Line.Argument (Arg)); + elsif Command = "get" then + if Item = "user" then + Get_User (C); + elsif Item = "pet" then + Get_Pet (C); + elsif Item = "order" then + Get_Order (C); + else + Usage; + end if; + elsif Command = "list" then + if Item = "pet" then + List_Pet (C); + elsif Item = "inventory" then + List_Inventory (C); + else + Usage; + end if; + elsif Command = "add" then + if Item = "pet" then + Add_Pet (C); + else + Usage; + end if; + elsif Command = "rm" then + if Item = "user" then + Delete_User (C); + elsif Item = "order" then + Delete_Order (C); + elsif Item = "pet" then + Delete_Pet (C); + else + Usage; + end if; + elsif Command = "update" then + Usage; + else + Usage; + end if; + + exception + when E : Constraint_Error => + Put_Line ("Constraint error raised: " & Ada.Exceptions.Exception_Message (E)); + + end; +end Test; diff --git a/samples/client/petstore/ada/src/samples-petstore.ads b/samples/client/petstore/ada/src/samples-petstore.ads new file mode 100644 index 00000000000..c5292a19abf --- /dev/null +++ b/samples/client/petstore/ada/src/samples-petstore.ads @@ -0,0 +1,2 @@ +package Samples.Petstore is +end Samples.Petstore; \ No newline at end of file diff --git a/samples/client/petstore/ada/src/samples.ads b/samples/client/petstore/ada/src/samples.ads new file mode 100644 index 00000000000..af66cc10aa8 --- /dev/null +++ b/samples/client/petstore/ada/src/samples.ads @@ -0,0 +1,2 @@ +package Samples is +end Samples; \ No newline at end of file