Skip to content

Commit f01e2bd

Browse files
stcarrezfvarose
authored andcommitted
[Ada] Adding Ada client samples (swagger-api#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
1 parent 538cb84 commit f01e2bd

13 files changed

+1564
-0
lines changed

bin/ada-petstore.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
model="modules/swagger-codegen/src/test/resources/2_0/petstore.yaml"
30+
ags="$@ generate --template-dir modules/swagger-codegen/src/main/resources/Ada -l ada"
31+
ags="$ags -i $model -t modules/swagger-codegen/src/main/resources/Ada -o samples/client/petstore/ada"
32+
ags="$ags -DprojectName=Petstore --model-package Samples.Petstore"
33+
34+
java $JAVA_OPTS -jar $executable $ags
35+
rm -rf samples/client/petstore/ada/src/server
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Swagger Codegen Ignore
2+
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.3.0-SNAPSHOT

samples/client/petstore/ada/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Swagger Petstore Ada Client
2+
3+
## Overview
4+
5+
This Ada client uses the Petstore API to demonstrate how to use the generator
6+
and use the generated Ada code. The following files are generated by
7+
[Swagger Codegen](https://github.com/swagger-api/swagger-codegen):
8+
9+
* src/client/samples-petstore-models.ads
10+
* src/client/samples-petstore-models.adb
11+
* src/client/samples-petstore-clients.ads
12+
* src/client/samples-petstore-clients.adb
13+
14+
The 'Models' package contains the definition of types used by the request or response
15+
in the API operations. It also provides operations to serialize and deserialize these
16+
objects in JSON, XML or form-based data streams.
17+
18+
The 'Clients' package contains the definition of operations provided by the Petstore API.
19+
20+
## Requirements.
21+
22+
To build this sample, you must have installed the GNAT Ada compiler as well the following libraries:
23+
24+
* Ada Util (https://github.com/stcarrez/ada-util)
25+
* Swagger Ada (https://github.com/stcarrez/swagger-ada)
26+
* AWS (http://libre.adacore.com/libre/tools/aws/)
27+
28+
## Building the petstore client
29+
30+
Build the petstore client by using the following command:
31+
```sh
32+
gprbuild -Ppetstore -p
33+
```
34+
35+
## Using the Swagger Ada code
36+
37+
### Initialization
38+
39+
The HTTP/REST support is provided by [Ada Util](https://github.com/stcarrez/ada-util)
40+
and encapsulated by [Swagger Ada](https://github.com/stcarrez/swagger-ada). If you want
41+
to use Curl, you should initialize with the following:
42+
43+
```
44+
Util.Http.Clients.Curl.Register;
45+
```
46+
47+
But if you want to use [AWS](http://libre.adacore.com/libre/tools/aws/), you will initialize with:
48+
49+
50+
```
51+
Util.Http.Clients.Web.Register;
52+
```
53+
54+
After the initialization is done, you will declare a client instance to access
55+
the API operations:
56+
57+
```
58+
C : Samples.Petstore.Clients.Client_Type;
59+
```
60+
61+
The 'Client_Type' is the generated type that will implement the operations
62+
described in the OpenAPI description file.
63+
64+
And you should initialize the server base URI you want to connect to:
65+
66+
```
67+
C.Set_Server ("http://petstore.swagger.io/v2");
68+
```
69+
70+
At this stage, you can use the generated operation.
71+
72+
### Calling an operation
73+
74+
Let's retrieve some pet information by calling the 'Get_Pet_By_Id' operation.
75+
This operation needs an integer as input parameter and returns a 'Pet_Type'
76+
object that contains all the pet information. You will first declare
77+
the pet instance as follows:
78+
79+
```
80+
Pet : Samples.Petstore.Models.Pet_Type;
81+
```
82+
83+
And then call the 'Get_Pet_By_Id' operation:
84+
85+
```
86+
C.Get_Pet_By_Id (768, Pet);
87+
```
88+
89+
At this stage, you can access information from the 'Pet' instance:
90+
91+
```
92+
Ada.Text_IO.Put_Line ("Id : " & Swagger.Long'Image (Pet.Id));
93+
Ada.Text_IO.Put_Line ("Name : " & Swagger.To_String (Pet.Name));
94+
Ada.Text_IO.Put_Line ("Status : " & Swagger.To_String (Pet.Status));
95+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
abstract project Config is
2+
for Source_Dirs use ();
3+
4+
type Yes_No is ("yes", "no");
5+
6+
type Library_Type_Type is ("relocatable", "static");
7+
8+
type Mode_Type is ("distrib", "debug", "optimize", "profile");
9+
Mode : Mode_Type := external ("MODE", "debug");
10+
11+
Coverage : Yes_No := External ("COVERAGE", "no");
12+
Processors := External ("PROCESSORS", "1");
13+
14+
package Builder is
15+
case Mode is
16+
when "debug" =>
17+
for Default_Switches ("Ada") use ("-g", "-j" & Processors);
18+
when others =>
19+
for Default_Switches ("Ada") use ("-g", "-O2", "-j" & Processors);
20+
end case;
21+
end Builder;
22+
23+
package compiler is
24+
warnings := ("-gnatwua");
25+
defaults := ("-gnat2012");
26+
case Mode is
27+
when "distrib" =>
28+
for Default_Switches ("Ada") use defaults & ("-gnatafno", "-gnatVa", "-gnatwa");
29+
30+
when "debug" =>
31+
for Default_Switches ("Ada") use defaults & warnings
32+
& ("-gnata", "-gnatVaMI", "-gnaty3abcefhiklmnprstxM99");
33+
34+
when "optimize" =>
35+
for Default_Switches ("Ada") use defaults & warnings
36+
& ("-gnatn", "-gnatp", "-fdata-sections", "-ffunction-sections");
37+
38+
when "profile" =>
39+
for Default_Switches ("Ada") use defaults & warnings & ("-pg");
40+
end case;
41+
42+
case Coverage is
43+
when "yes" =>
44+
for Default_Switches ("ada") use Compiler'Default_Switches ("Ada") &
45+
("-fprofile-arcs", "-ftest-coverage");
46+
when others =>
47+
end case;
48+
end compiler;
49+
50+
package binder is
51+
case Mode is
52+
when "debug" =>
53+
for Default_Switches ("Ada") use ("-E");
54+
55+
when others =>
56+
for Default_Switches ("Ada") use ("-E");
57+
58+
end case;
59+
end binder;
60+
61+
package linker is
62+
case Mode is
63+
when "profile" =>
64+
for Default_Switches ("Ada") use ("-pg");
65+
66+
when "distrib" =>
67+
for Default_Switches ("Ada") use ("-s");
68+
69+
when "optimize" =>
70+
for Default_Switches ("Ada") use ("-Wl,--gc-sections");
71+
72+
when others =>
73+
null;
74+
end case;
75+
76+
case Coverage is
77+
when "yes" =>
78+
for Default_Switches ("ada") use Linker'Default_Switches ("ada") &
79+
("-fprofile-arcs");
80+
when others =>
81+
end case;
82+
end linker;
83+
84+
package Ide is
85+
for VCS_Kind use "git";
86+
end Ide;
87+
88+
end Config;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
with "config";
2+
with "util";
3+
with "util_http";
4+
with "swagger";
5+
project Petstore is
6+
7+
Mains := ("petstore.adb");
8+
for Main use Mains;
9+
for Source_Dirs use ("src", "src/client");
10+
for Object_Dir use "./obj";
11+
for Exec_Dir use "./bin";
12+
13+
package Binder renames Config.Binder;
14+
package Builder renames Config.Builder;
15+
package Compiler renames Config.Compiler;
16+
package Linker renames Config.Linker;
17+
18+
end Petstore;

0 commit comments

Comments
 (0)