Skip to content

Commit

Permalink
[Ada] Adding Ada client samples (#6634)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
stcarrez authored and wing328 committed Oct 8, 2017
1 parent 7a7dd6d commit cdc83ff
Show file tree
Hide file tree
Showing 13 changed files with 1,564 additions and 0 deletions.
35 changes: 35 additions & 0 deletions bin/ada-petstore.sh
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions samples/client/petstore/ada/.swagger-codegen-ignore
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions samples/client/petstore/ada/.swagger-codegen/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0-SNAPSHOT
95 changes: 95 additions & 0 deletions samples/client/petstore/ada/README.md
Original file line number Diff line number Diff line change
@@ -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));
```
88 changes: 88 additions & 0 deletions samples/client/petstore/ada/config.gpr
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 18 additions & 0 deletions samples/client/petstore/ada/petstore.gpr
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit cdc83ff

Please sign in to comment.