SlideShare uma empresa Scribd logo
1 de 37
Baixar para ler offline
Writing REST APIs with OpenAPI
and Swagger Ada
Stéphane Carrez FOSDEM 2018
https://github.com/stcarrez/swagger-ada 2
OpenAPI and Swagger Ada
● Introduction to OpenAPI and Swagger
● Writing a REST Ada client
● Writing a REST Ada server
● Handling security with OAuth2
● Demo
https://github.com/stcarrez/swagger-ada 3
30 years of RPC
● Sun RPC (RFC 1057) in 1988
● CORBA IDL in 1991
● Ada95 Distributed Annex E in 1995
● Java RMI in 2000
● WSDL and SOAP in 2000
● Google gRPC with Protocol Buffers since 2001
https://github.com/stcarrez/swagger-ada 4
30 years but same goals
● Simplify the developer’s job
● Describe the protocol between client & server
● Generate client stubs and server skeleton
● Handle and hide communication details
● Document the client & server interaction
https://github.com/stcarrez/swagger-ada 5
Why REST and OpenAPI?
● REST as an alternative to SOAP since 2000
(Roy Thomas Fielding)
● Easier to use, write, implement, debug
● Can easily be used from browsers
● Increasing usage of REST with mobile applications
● Need for description, documentation
● Need for client language bindings
https://github.com/stcarrez/swagger-ada 6
OpenAPI Specification
● Started in 2010 to describe REST APIs
● OpenAPI Initiative created in Nov 5, 2015
(Google, Microsoft, IBM, Paypal, ...)
● OpenAPI 3.0 released July 26, 2017
● https://github.com/OAI/OpenAPI-Specification
https://github.com/stcarrez/swagger-ada 7
OpenAPI 2.0 Document Structure
info
security
securityDefinitions
consumesproduces
paths
tags externalDocs
definitions
parameters
responses
host
basePath
schemes
Describes security aspects
YAML or JSON file with well defined keywords
Describes REST APIs paths,
operations, can reference definitions,
parameters, responses
Describes data types, parameters,
responses
What the API accepts as input,
what it produces
https://github.com/stcarrez/swagger-ada 8
OpenAPI benefits
info
security
securityDefinitions
consumesproduces
paths
tags externalDocs
definitions
parameters
responses
host
basePath
schemes
Documentation
Client Binding
Server Skeleton
Server Configuration
Online Documentation
API Validation
https://github.com/stcarrez/swagger-ada 9
Swagger: Tools for OpenAPI
● Online Editor: Swagger Editor
● Generator: Swagger Codegen
● Documentation: Swagger UI
● Sources: https://github.com/swagger-api
SWAGGER
https://github.com/stcarrez/swagger-ada 10
Swagger Editor:
https://editor.swagger.io
https://github.com/stcarrez/swagger-ada 11
Swagger Codegen
OpenAPI
Document
YAML
JSON{ }
{...}
Swagger
Codegen
API
Doc
(HTML)
Ada
REST
Client
Ada
REST
Server
Java
REST
Client
Java
REST
Server
...
Python
REST
Client
Python
Flask
Server
...
25+
Programming Languages
https://github.com/stcarrez/swagger-ada 12
Writing a REST Ada client
● Get the OpenAPI/Swagger description file
– From SwaggerHub: https://swaggerhub.com/
– From APIs.guru: https://apis.guru/openapi-directory/
● Generate the Ada client code
● Use the generated code to make API calls
https://github.com/stcarrez/swagger-ada 13
OpenAPI: Info description (1/3)
● YAML or JSON file
● General purpose description of the API
● Describe the service entry point
swagger: "2.0"
info:
version: "1.0"
title: "Todo API"
contact:
email: Stephane.Carrez@gmail.com
license:
name: Apache 2.0
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
host: localhost:8080
basePath: /v1
tags:
- name: tasks
description: Operations to manage tasks
schemes:
- https
- http
https://github.com/stcarrez/swagger-ada 14
OpenAPI: REST operation (2/3)
● Describe the REST operations
paths:
/todos:
get:
tags:
- tasks
summary: List the available tasks
description: List the available tasks
operationId: listTodos
produces:
- application/json
parameters:
- name: status
in: query
description: Filters the task by their status
required: false
type: string
enum:
- done
- waiting
- working
- all
responses:
'200':
description: successful operation
schema:
type: array
items:
$ref: '#/definitions/Todo'
'400':
description: Invalid status value
https://github.com/stcarrez/swagger-ada 15
OpenAPI: Model definitions (3/3)
definitions:
Todo:
type: object
properties:
id:
type: integer
format: int64
description: The todo identifier
title:
type: string
description: The todo title
create_date:
type: string
format: date-time
description: The todo creation date
done_date:
type: string
format: date-time
description: The todo resolution date
status:
type: string
description: The todo state
enum:
- waiting
- done
required:
- id
- title
- status
- create_date
https://github.com/stcarrez/swagger-ada 16
Client: let’s generate the code!
● Generate the client code with Swagger Codegen
$ java -jar swagger-codegen-cli.jar generate -l ada -i todo.yaml 
-DprojectName=Todos --model-package Todos
Client API: package Todos.Clients
Model API: package Todos.Models
Sample: procedure Todos.Client
GNAT project
https://github.com/stcarrez/swagger-ada 17
Ada REST Client
Generated code
Your client code and application
Swagger runtime
Choose between libcurl or AWS
Brings security with OAuth2 support
Brings JSON/XML serialization
deserialization and more
Ada Security
Swagger Ada
Ada Utility Library
CURL AWS
Client API & Model
Client Application
XML/Ada
https://github.com/stcarrez/swagger-ada 18
Client and Server Data Model
● Data types described in the Models package
● Same Models Ada package for client and server
● Operations to serialize and deserialize (JSON/XML)
package Todos.Models is
   type Todo_Type is record
      Id          : Swagger.Long;
      Title       : Swagger.UString;
      Create_Date : Swagger.Datetime;
      Done_Date   : Swagger.Nullable_Date;
      Status      : Swagger.UString;
   end record;
   package Todo_Type_Vectors is
      new Ada.Containers.Vectors
        (Positive, Todo_Type);
end Todos.Models;
Todo:
type: object
properties:
id:
type: integer
format: int64
description: The todo identifier
title:
type: string
description: The todo title
create_date:
type: string
format: date-time
description: The todo creation date
done_date:
type: string
format: date-time
description: The todo resolution date
status:
type: string
description: The todo state
enum:
- waiting
- done
https://github.com/stcarrez/swagger-ada 19
Client API
● Represented by the Client_Type tagged record
● Provides operations described by the OpenAPI
● Allows to control the API call (headers, security)
package Todos.Clients is
   type Client_Type is
      new Swagger.Clients.Client_Type with null record; 
   
   procedure Create_Todo (Client : in out Client_Type;
                          Title  : in Swagger.Ustring;
                          Result : out Todos.Models.Todo_Type);
   procedure List_Todos (Client : in out Client_Type;
                         Status : in out Swagger.Nullable_UString;
                         Result : out Todos.Models.Todo_Vector);
end Todos.Clients;
https://github.com/stcarrez/swagger-ada 20
Calling REST in Ada
● Declare a Client_Type instance
● Configure it (server URL, credentials)
● Call the operation with its parameters
with Todos.Clients;
with Todos.Models;
...
  Client : Todos.Clients.Client_Type;
  List   : Todos.Models.Todo_Type_Vectors.Vector;
  Empty  : Swagger.Nullable_String := (Is_Null => True, Value => <>);
  ...
    Client.Set_Server (“http://localhost:8080/v1”);
    Client.List_Todos (Empty, List);
https://github.com/stcarrez/swagger-ada 21
Writing a REST Ada server
● Write the OpenAPI/Swagger description file
● Generate the Ada server code
● Implement the server operations
● Share the OpenAPI description on SwaggerHub!
https://github.com/stcarrez/swagger-ada 22
Server: let’s generate the code!
$ java -jar swagger-codegen-cli.jar generate -l ada-server -i todo.yaml 
-DprojectName=Todos --model-package Todos
● Generate the server code with Swagger Codegen
Server skeleton: package Todos.Skeletons
Model API: package Todos.Models
Server: procedure Todos.Server
GNAT project, server configuration file
Server code: package Todos.Servers
https://github.com/stcarrez/swagger-ada 23
Ada REST Server
Generated code
Your server code and application
Swagger runtime
Brings REST server support with
security and OAuth2 support on
server side
Ada Security
Swagger Ada
Ada Utility Library
Server Skeleton & Model
Server Application
Ada Servlet
XML/Ada AWS
https://github.com/stcarrez/swagger-ada 24
Server Skeleton
● Declares the Server_Type limited interface to describe the operations
● Additional Context_Type object gives access to request, response
● Two generic packages for server skeleton provide two server models:
– Instance per request
– Global shared instance within a protected object
package Todos.Skeletons is
   type Server_Type is limited interface;
   
   procedure Create_Todo
      (Server  : in out Server_Type;
       Title   : in Swagger.Ustring;
       Result  : out Todos.Models.Todo_Type;
       Context : in out Swagger.Servers.Context_Type) is abstract;
   ...
end Todos.Skeletons;
https://github.com/stcarrez/swagger-ada 25
Server Implementation (1/2)
● Implement the Server_Type interface with its operations
● Populate Result or use the Context to send an error
● Serialization/Deserialization handled by the skeleton
package Todos.Servers is
   type Server_Type is limited new Todos.Skeletons.Server_Type ...
   
   overriding procedure Create_Todo
      (Server  : in out Server_Type;
       Title   : in Swagger.Ustring;
       Result  : out Todos.Models.Todo_Type;
       Context : in out Swagger.Servers.Context_Type);
   ...
end Todos.Servers;
https://github.com/stcarrez/swagger-ada 26
Server Implementation (2/2)
● Instantiate one of the two server skeletons
(per-request model or shared model)
● Register the OpenAPI to the application
package Todos.Servers is
   ...
   package Server_Impl is
      new Todos.Skeletons.Shared_Instance (Server_Type);
end Todos.Servers;
procedure Todos.Server is
   App : aliased Swagger.Servers.Applications.Application_Type;
begin
   . . .
   Todos.Servers.Server_Impl.Register (App);
   . . .
end Todos.Server;
https://github.com/stcarrez/swagger-ada 27
OpenAPI: Describing security
● Describe security endpoints
● Describe security scopes
● Assign required security scopes to operations
paths:
/todos:
get:
...
parameters:
...
responses:
...
security:
- todo_auth:
- 'read:todo'
Security:
- todo_auth: []
securityDefinitions:
todo_auth:
type: oauth2
flow: password
tokenUrl: /v1/oauth/token
scopes:
'write:todo': Write a todo
'read:todo': Read a todo
https://github.com/stcarrez/swagger-ada 28
Client Security with OAuth2 (1/2)
● Create and initialize a credentials object
● Obtain the access token and optional refresh token
● Configure the client to use the credentials
with Swagger.Credentials.OAuth;
Cred : aliased Swagger.Credentials.OAuth.OAuth2_Credential_Type;
...
   Cred.Set_Application_Identifier ("todo­app");
   Cred.Set_Application_Secret ("todo­app­secret");
   Cred.Set_Provider_URI ("http://localhost:8080/v1/oauth/token");
   Cred.Request_Token (Username, Password, "read:todo write:todo");
...
   Client.Set_Credentials (Cred’Access);
https://github.com/stcarrez/swagger-ada 29
Client Security with OAuth2 (2/2)
● Make API calls: credentials are passed on the wire within the
‘Authorization’ header
List : Todos.Models.Todo_Type_Vectors.Vector;
...
   Client.List_Todos (Empty, List);
GET /v1/todos HTTP/1.1
Host: localhost:8080
Authorization: Bearer 74rE0wU.d44CPAll_kyyB2krd8bYdVYWqgmtloIR.9zyiBM
Accept: application/json
https://github.com/stcarrez/swagger-ada 30
Server security (1/2)
● Each OpenAPI scope represented by a
permission definition (generated code):
● Authentication and permission check generated in
the server skeleton (generated code):
if not Context.Is_Authenticated then
   Context.Set_Error (401, "Not authenticated");
   return;
end if;
if not Context.Has_Permission (ACL_Read_Todo.Permission) then
   Context.Set_Error (403, "Permission denied");
   return;
end if;
package ACL_Read_Todo
   is new Security.Permissions.Definition ("read:todo");
https://github.com/stcarrez/swagger-ada 31
Server security (2/2)
● Configure the server key for OAuth2 tokens:
● Configure the server to register the client id and secret
● Configure the users allowed to authenticate
app.list=1
app.1.client_id=todo­app
app.1.client_secret=todo­app­secret
app.1.scope=none
users.list=1,2
users.1.username=admin
users.1.password=admin
users.2.username=test
users.2.password=test
swagger.key=Y29naGk5SGkKUG9YaTdhaHgKYWlUaGllM3UK
https://github.com/stcarrez/swagger-ada 32
Demo: Todo client
https://github.com/stcarrez/swagger-ada 33
Demo: Todo server (main)
https://github.com/stcarrez/swagger-ada 34
Demo: Todo server (impl)
https://github.com/stcarrez/swagger-ada 35
Demo: Running the client
Server not started
404 error received
https://github.com/stcarrez/swagger-ada 36
Limitations and improvements
● Ada Strings
(need a lot of To_String/To_Ustring conversions)
● Enums are treated as strings
● Circular type dependencies not handled
● Error model needs some work
● Improvements:
– Upload file support
– Asynchronous client operation call
https://github.com/stcarrez/swagger-ada 37
Conclusion
● OpenAPI describes REST APIs
● Swagger Codegen generates Ada code for you
● Swagger Ada is a runtime library for REST
client and REST server implementation
● More than 500 APIs available, write your own!
● Next step: … GraphQL?

Mais conteúdo relacionado

Mais procurados

Swagger / Quick Start Guide
Swagger / Quick Start GuideSwagger / Quick Start Guide
Swagger / Quick Start GuideAndrii Gakhov
 
OpenAPI development with Python
OpenAPI development with PythonOpenAPI development with Python
OpenAPI development with PythonTakuro Wada
 
Introducing Swagger
Introducing SwaggerIntroducing Swagger
Introducing SwaggerTony Tam
 
Rest API with Swagger and NodeJS
Rest API with Swagger and NodeJSRest API with Swagger and NodeJS
Rest API with Swagger and NodeJSLuigi Saetta
 
APIsecure 2023 - API orchestration: to build resilient applications, Cherish ...
APIsecure 2023 - API orchestration: to build resilient applications, Cherish ...APIsecure 2023 - API orchestration: to build resilient applications, Cherish ...
APIsecure 2023 - API orchestration: to build resilient applications, Cherish ...apidays
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & DevelopmentAshok Pundit
 
Get Your Node.js API Swaggering with OpenAPI Spec
Get Your Node.js API Swaggering with OpenAPI SpecGet Your Node.js API Swaggering with OpenAPI Spec
Get Your Node.js API Swaggering with OpenAPI SpecAdam Paxton
 
Introduction to Swagger
Introduction to SwaggerIntroduction to Swagger
Introduction to SwaggerKnoldus Inc.
 
The Architecture of an API Platform
The Architecture of an API PlatformThe Architecture of an API Platform
The Architecture of an API PlatformJohannes Ridderstedt
 
Swagger APIs for Humans and Robots (Gluecon)
Swagger APIs for Humans and Robots (Gluecon)Swagger APIs for Humans and Robots (Gluecon)
Swagger APIs for Humans and Robots (Gluecon)Tony Tam
 
Introducing OpenAPI Version 3.1
Introducing OpenAPI Version 3.1Introducing OpenAPI Version 3.1
Introducing OpenAPI Version 3.1SmartBear
 
API First Workflow: How could we have better API Docs through DevOps pipeline
API First Workflow: How could we have better API Docs through DevOps pipelineAPI First Workflow: How could we have better API Docs through DevOps pipeline
API First Workflow: How could we have better API Docs through DevOps pipelinePronovix
 
GraphQL vs REST
GraphQL vs RESTGraphQL vs REST
GraphQL vs RESTGreeceJS
 
Document your rest api using swagger - Devoxx 2015
Document your rest api using swagger - Devoxx 2015Document your rest api using swagger - Devoxx 2015
Document your rest api using swagger - Devoxx 2015johannes_fiala
 
Api-First service design
Api-First service designApi-First service design
Api-First service designStefaan Ponnet
 
Understanding REST APIs in 5 Simple Steps
Understanding REST APIs in 5 Simple StepsUnderstanding REST APIs in 5 Simple Steps
Understanding REST APIs in 5 Simple StepsTessa Mero
 
Getting Started with Spring Authorization Server
Getting Started with Spring Authorization ServerGetting Started with Spring Authorization Server
Getting Started with Spring Authorization ServerVMware Tanzu
 

Mais procurados (20)

Swagger / Quick Start Guide
Swagger / Quick Start GuideSwagger / Quick Start Guide
Swagger / Quick Start Guide
 
OpenAPI development with Python
OpenAPI development with PythonOpenAPI development with Python
OpenAPI development with Python
 
Introducing Swagger
Introducing SwaggerIntroducing Swagger
Introducing Swagger
 
Rest API with Swagger and NodeJS
Rest API with Swagger and NodeJSRest API with Swagger and NodeJS
Rest API with Swagger and NodeJS
 
APIsecure 2023 - API orchestration: to build resilient applications, Cherish ...
APIsecure 2023 - API orchestration: to build resilient applications, Cherish ...APIsecure 2023 - API orchestration: to build resilient applications, Cherish ...
APIsecure 2023 - API orchestration: to build resilient applications, Cherish ...
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & Development
 
Effective API Design
Effective API DesignEffective API Design
Effective API Design
 
API Design- Best Practices
API Design-   Best PracticesAPI Design-   Best Practices
API Design- Best Practices
 
Get Your Node.js API Swaggering with OpenAPI Spec
Get Your Node.js API Swaggering with OpenAPI SpecGet Your Node.js API Swaggering with OpenAPI Spec
Get Your Node.js API Swaggering with OpenAPI Spec
 
Introduction to Swagger
Introduction to SwaggerIntroduction to Swagger
Introduction to Swagger
 
The Architecture of an API Platform
The Architecture of an API PlatformThe Architecture of an API Platform
The Architecture of an API Platform
 
Swagger APIs for Humans and Robots (Gluecon)
Swagger APIs for Humans and Robots (Gluecon)Swagger APIs for Humans and Robots (Gluecon)
Swagger APIs for Humans and Robots (Gluecon)
 
Introducing OpenAPI Version 3.1
Introducing OpenAPI Version 3.1Introducing OpenAPI Version 3.1
Introducing OpenAPI Version 3.1
 
API First Workflow: How could we have better API Docs through DevOps pipeline
API First Workflow: How could we have better API Docs through DevOps pipelineAPI First Workflow: How could we have better API Docs through DevOps pipeline
API First Workflow: How could we have better API Docs through DevOps pipeline
 
GraphQL vs REST
GraphQL vs RESTGraphQL vs REST
GraphQL vs REST
 
Document your rest api using swagger - Devoxx 2015
Document your rest api using swagger - Devoxx 2015Document your rest api using swagger - Devoxx 2015
Document your rest api using swagger - Devoxx 2015
 
Api-First service design
Api-First service designApi-First service design
Api-First service design
 
Understanding REST APIs in 5 Simple Steps
Understanding REST APIs in 5 Simple StepsUnderstanding REST APIs in 5 Simple Steps
Understanding REST APIs in 5 Simple Steps
 
GraphQL
GraphQLGraphQL
GraphQL
 
Getting Started with Spring Authorization Server
Getting Started with Spring Authorization ServerGetting Started with Spring Authorization Server
Getting Started with Spring Authorization Server
 

Semelhante a Writing REST APIs with OpenAPI and Swagger Ada

API First with Connexion - PyConWeb 2018
API First with Connexion - PyConWeb 2018API First with Connexion - PyConWeb 2018
API First with Connexion - PyConWeb 2018Henning Jacobs
 
APIs for API Management: Consume and Develop Apps
APIs for API Management: Consume and Develop AppsAPIs for API Management: Consume and Develop Apps
APIs for API Management: Consume and Develop AppsWSO2
 
Gatling Performance Workshop
Gatling Performance WorkshopGatling Performance Workshop
Gatling Performance WorkshopSai Krishna
 
Driving containerd operations with gRPC
Driving containerd operations with gRPCDriving containerd operations with gRPC
Driving containerd operations with gRPCDocker, Inc.
 
Go swagger tutorial how to create golang api documentation using go swagger (1)
Go swagger tutorial how to create golang api documentation using go swagger (1)Go swagger tutorial how to create golang api documentation using go swagger (1)
Go swagger tutorial how to create golang api documentation using go swagger (1)Katy Slemon
 
REST in Peace. Long live gRPC!
REST in Peace. Long live gRPC!REST in Peace. Long live gRPC!
REST in Peace. Long live gRPC!QAware GmbH
 
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...apidays
 
Consuming Restful APIs using Swagger v2.0
Consuming Restful APIs using Swagger v2.0Consuming Restful APIs using Swagger v2.0
Consuming Restful APIs using Swagger v2.0Pece Nikolovski
 
Cloud native IPC for Microservices Workshop @ Containerdays 2022
Cloud native IPC for Microservices Workshop @ Containerdays 2022Cloud native IPC for Microservices Workshop @ Containerdays 2022
Cloud native IPC for Microservices Workshop @ Containerdays 2022QAware GmbH
 
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...Caktus Group
 
Apigility introduction v2 (glasgow php)
Apigility introduction v2 (glasgow php)Apigility introduction v2 (glasgow php)
Apigility introduction v2 (glasgow php)Engineor
 
Where is my scalable api?
Where is my scalable api?Where is my scalable api?
Where is my scalable api?Altoros
 
Session: A Reference Architecture for Running Modern APIs with NGINX Unit and...
Session: A Reference Architecture for Running Modern APIs with NGINX Unit and...Session: A Reference Architecture for Running Modern APIs with NGINX Unit and...
Session: A Reference Architecture for Running Modern APIs with NGINX Unit and...NGINX, Inc.
 
Introduction to CloudStack API
Introduction to CloudStack APIIntroduction to CloudStack API
Introduction to CloudStack APIKrunal Jain
 
how to use openstack api
how to use openstack apihow to use openstack api
how to use openstack apiLiang Bo
 
Connecting the Dots: Kong for GraphQL Endpoints
Connecting the Dots: Kong for GraphQL EndpointsConnecting the Dots: Kong for GraphQL Endpoints
Connecting the Dots: Kong for GraphQL EndpointsJulien Bataillé
 

Semelhante a Writing REST APIs with OpenAPI and Swagger Ada (20)

API First with Connexion - PyConWeb 2018
API First with Connexion - PyConWeb 2018API First with Connexion - PyConWeb 2018
API First with Connexion - PyConWeb 2018
 
APIs for API Management: Consume and Develop Apps
APIs for API Management: Consume and Develop AppsAPIs for API Management: Consume and Develop Apps
APIs for API Management: Consume and Develop Apps
 
Introduction to the Archivematica API (September 2018)
Introduction to the Archivematica API (September 2018)Introduction to the Archivematica API (September 2018)
Introduction to the Archivematica API (September 2018)
 
Gatling Performance Workshop
Gatling Performance WorkshopGatling Performance Workshop
Gatling Performance Workshop
 
Driving containerd operations with gRPC
Driving containerd operations with gRPCDriving containerd operations with gRPC
Driving containerd operations with gRPC
 
Get your teeth into Plack
Get your teeth into PlackGet your teeth into Plack
Get your teeth into Plack
 
Where is my scalable API?
Where is my scalable API?Where is my scalable API?
Where is my scalable API?
 
Go swagger tutorial how to create golang api documentation using go swagger (1)
Go swagger tutorial how to create golang api documentation using go swagger (1)Go swagger tutorial how to create golang api documentation using go swagger (1)
Go swagger tutorial how to create golang api documentation using go swagger (1)
 
Cloud Native API Design and Management
Cloud Native API Design and ManagementCloud Native API Design and Management
Cloud Native API Design and Management
 
REST in Peace. Long live gRPC!
REST in Peace. Long live gRPC!REST in Peace. Long live gRPC!
REST in Peace. Long live gRPC!
 
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
 
Consuming Restful APIs using Swagger v2.0
Consuming Restful APIs using Swagger v2.0Consuming Restful APIs using Swagger v2.0
Consuming Restful APIs using Swagger v2.0
 
Cloud native IPC for Microservices Workshop @ Containerdays 2022
Cloud native IPC for Microservices Workshop @ Containerdays 2022Cloud native IPC for Microservices Workshop @ Containerdays 2022
Cloud native IPC for Microservices Workshop @ Containerdays 2022
 
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
 
Apigility introduction v2 (glasgow php)
Apigility introduction v2 (glasgow php)Apigility introduction v2 (glasgow php)
Apigility introduction v2 (glasgow php)
 
Where is my scalable api?
Where is my scalable api?Where is my scalable api?
Where is my scalable api?
 
Session: A Reference Architecture for Running Modern APIs with NGINX Unit and...
Session: A Reference Architecture for Running Modern APIs with NGINX Unit and...Session: A Reference Architecture for Running Modern APIs with NGINX Unit and...
Session: A Reference Architecture for Running Modern APIs with NGINX Unit and...
 
Introduction to CloudStack API
Introduction to CloudStack APIIntroduction to CloudStack API
Introduction to CloudStack API
 
how to use openstack api
how to use openstack apihow to use openstack api
how to use openstack api
 
Connecting the Dots: Kong for GraphQL Endpoints
Connecting the Dots: Kong for GraphQL EndpointsConnecting the Dots: Kong for GraphQL Endpoints
Connecting the Dots: Kong for GraphQL Endpoints
 

Mais de Stephane Carrez

Implementing a build manager in Ada
Implementing a build manager in AdaImplementing a build manager in Ada
Implementing a build manager in AdaStephane Carrez
 
Porion a new Build Manager
Porion a new Build ManagerPorion a new Build Manager
Porion a new Build ManagerStephane Carrez
 
Protect Sensitive Data with Ada Keystore
Protect Sensitive Data with Ada KeystoreProtect Sensitive Data with Ada Keystore
Protect Sensitive Data with Ada KeystoreStephane Carrez
 
AKT un outil pour sécuriser vos données et documents sensibles
AKT un outil pour sécuriser vos données et documents sensiblesAKT un outil pour sécuriser vos données et documents sensibles
AKT un outil pour sécuriser vos données et documents sensiblesStephane Carrez
 
Secure Web Applications with AWA
Secure Web Applications with AWASecure Web Applications with AWA
Secure Web Applications with AWAStephane Carrez
 
Persistence with Ada Database Objects (ADO)
Persistence with Ada Database Objects (ADO)Persistence with Ada Database Objects (ADO)
Persistence with Ada Database Objects (ADO)Stephane Carrez
 
IP Network Stack in Ada 2012 and the Ravenscar Profile
IP Network Stack in Ada 2012 and the Ravenscar ProfileIP Network Stack in Ada 2012 and the Ravenscar Profile
IP Network Stack in Ada 2012 and the Ravenscar ProfileStephane Carrez
 

Mais de Stephane Carrez (8)

Implementing a build manager in Ada
Implementing a build manager in AdaImplementing a build manager in Ada
Implementing a build manager in Ada
 
Porion a new Build Manager
Porion a new Build ManagerPorion a new Build Manager
Porion a new Build Manager
 
Protect Sensitive Data with Ada Keystore
Protect Sensitive Data with Ada KeystoreProtect Sensitive Data with Ada Keystore
Protect Sensitive Data with Ada Keystore
 
AKT un outil pour sécuriser vos données et documents sensibles
AKT un outil pour sécuriser vos données et documents sensiblesAKT un outil pour sécuriser vos données et documents sensibles
AKT un outil pour sécuriser vos données et documents sensibles
 
Ada for Web Development
Ada for Web DevelopmentAda for Web Development
Ada for Web Development
 
Secure Web Applications with AWA
Secure Web Applications with AWASecure Web Applications with AWA
Secure Web Applications with AWA
 
Persistence with Ada Database Objects (ADO)
Persistence with Ada Database Objects (ADO)Persistence with Ada Database Objects (ADO)
Persistence with Ada Database Objects (ADO)
 
IP Network Stack in Ada 2012 and the Ravenscar Profile
IP Network Stack in Ada 2012 and the Ravenscar ProfileIP Network Stack in Ada 2012 and the Ravenscar Profile
IP Network Stack in Ada 2012 and the Ravenscar Profile
 

Último

Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 

Último (20)

Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 

Writing REST APIs with OpenAPI and Swagger Ada

  • 1. Writing REST APIs with OpenAPI and Swagger Ada Stéphane Carrez FOSDEM 2018
  • 2. https://github.com/stcarrez/swagger-ada 2 OpenAPI and Swagger Ada ● Introduction to OpenAPI and Swagger ● Writing a REST Ada client ● Writing a REST Ada server ● Handling security with OAuth2 ● Demo
  • 3. https://github.com/stcarrez/swagger-ada 3 30 years of RPC ● Sun RPC (RFC 1057) in 1988 ● CORBA IDL in 1991 ● Ada95 Distributed Annex E in 1995 ● Java RMI in 2000 ● WSDL and SOAP in 2000 ● Google gRPC with Protocol Buffers since 2001
  • 4. https://github.com/stcarrez/swagger-ada 4 30 years but same goals ● Simplify the developer’s job ● Describe the protocol between client & server ● Generate client stubs and server skeleton ● Handle and hide communication details ● Document the client & server interaction
  • 5. https://github.com/stcarrez/swagger-ada 5 Why REST and OpenAPI? ● REST as an alternative to SOAP since 2000 (Roy Thomas Fielding) ● Easier to use, write, implement, debug ● Can easily be used from browsers ● Increasing usage of REST with mobile applications ● Need for description, documentation ● Need for client language bindings
  • 6. https://github.com/stcarrez/swagger-ada 6 OpenAPI Specification ● Started in 2010 to describe REST APIs ● OpenAPI Initiative created in Nov 5, 2015 (Google, Microsoft, IBM, Paypal, ...) ● OpenAPI 3.0 released July 26, 2017 ● https://github.com/OAI/OpenAPI-Specification
  • 7. https://github.com/stcarrez/swagger-ada 7 OpenAPI 2.0 Document Structure info security securityDefinitions consumesproduces paths tags externalDocs definitions parameters responses host basePath schemes Describes security aspects YAML or JSON file with well defined keywords Describes REST APIs paths, operations, can reference definitions, parameters, responses Describes data types, parameters, responses What the API accepts as input, what it produces
  • 8. https://github.com/stcarrez/swagger-ada 8 OpenAPI benefits info security securityDefinitions consumesproduces paths tags externalDocs definitions parameters responses host basePath schemes Documentation Client Binding Server Skeleton Server Configuration Online Documentation API Validation
  • 9. https://github.com/stcarrez/swagger-ada 9 Swagger: Tools for OpenAPI ● Online Editor: Swagger Editor ● Generator: Swagger Codegen ● Documentation: Swagger UI ● Sources: https://github.com/swagger-api SWAGGER
  • 11. https://github.com/stcarrez/swagger-ada 11 Swagger Codegen OpenAPI Document YAML JSON{ } {...} Swagger Codegen API Doc (HTML) Ada REST Client Ada REST Server Java REST Client Java REST Server ... Python REST Client Python Flask Server ... 25+ Programming Languages
  • 12. https://github.com/stcarrez/swagger-ada 12 Writing a REST Ada client ● Get the OpenAPI/Swagger description file – From SwaggerHub: https://swaggerhub.com/ – From APIs.guru: https://apis.guru/openapi-directory/ ● Generate the Ada client code ● Use the generated code to make API calls
  • 13. https://github.com/stcarrez/swagger-ada 13 OpenAPI: Info description (1/3) ● YAML or JSON file ● General purpose description of the API ● Describe the service entry point swagger: "2.0" info: version: "1.0" title: "Todo API" contact: email: Stephane.Carrez@gmail.com license: name: Apache 2.0 url: 'http://www.apache.org/licenses/LICENSE-2.0.html' host: localhost:8080 basePath: /v1 tags: - name: tasks description: Operations to manage tasks schemes: - https - http
  • 14. https://github.com/stcarrez/swagger-ada 14 OpenAPI: REST operation (2/3) ● Describe the REST operations paths: /todos: get: tags: - tasks summary: List the available tasks description: List the available tasks operationId: listTodos produces: - application/json parameters: - name: status in: query description: Filters the task by their status required: false type: string enum: - done - waiting - working - all responses: '200': description: successful operation schema: type: array items: $ref: '#/definitions/Todo' '400': description: Invalid status value
  • 15. https://github.com/stcarrez/swagger-ada 15 OpenAPI: Model definitions (3/3) definitions: Todo: type: object properties: id: type: integer format: int64 description: The todo identifier title: type: string description: The todo title create_date: type: string format: date-time description: The todo creation date done_date: type: string format: date-time description: The todo resolution date status: type: string description: The todo state enum: - waiting - done required: - id - title - status - create_date
  • 16. https://github.com/stcarrez/swagger-ada 16 Client: let’s generate the code! ● Generate the client code with Swagger Codegen $ java -jar swagger-codegen-cli.jar generate -l ada -i todo.yaml -DprojectName=Todos --model-package Todos Client API: package Todos.Clients Model API: package Todos.Models Sample: procedure Todos.Client GNAT project
  • 17. https://github.com/stcarrez/swagger-ada 17 Ada REST Client Generated code Your client code and application Swagger runtime Choose between libcurl or AWS Brings security with OAuth2 support Brings JSON/XML serialization deserialization and more Ada Security Swagger Ada Ada Utility Library CURL AWS Client API & Model Client Application XML/Ada
  • 18. https://github.com/stcarrez/swagger-ada 18 Client and Server Data Model ● Data types described in the Models package ● Same Models Ada package for client and server ● Operations to serialize and deserialize (JSON/XML) package Todos.Models is    type Todo_Type is record       Id          : Swagger.Long;       Title       : Swagger.UString;       Create_Date : Swagger.Datetime;       Done_Date   : Swagger.Nullable_Date;       Status      : Swagger.UString;    end record;    package Todo_Type_Vectors is       new Ada.Containers.Vectors         (Positive, Todo_Type); end Todos.Models; Todo: type: object properties: id: type: integer format: int64 description: The todo identifier title: type: string description: The todo title create_date: type: string format: date-time description: The todo creation date done_date: type: string format: date-time description: The todo resolution date status: type: string description: The todo state enum: - waiting - done
  • 19. https://github.com/stcarrez/swagger-ada 19 Client API ● Represented by the Client_Type tagged record ● Provides operations described by the OpenAPI ● Allows to control the API call (headers, security) package Todos.Clients is    type Client_Type is       new Swagger.Clients.Client_Type with null record;         procedure Create_Todo (Client : in out Client_Type;                           Title  : in Swagger.Ustring;                           Result : out Todos.Models.Todo_Type);    procedure List_Todos (Client : in out Client_Type;                          Status : in out Swagger.Nullable_UString;                          Result : out Todos.Models.Todo_Vector); end Todos.Clients;
  • 20. https://github.com/stcarrez/swagger-ada 20 Calling REST in Ada ● Declare a Client_Type instance ● Configure it (server URL, credentials) ● Call the operation with its parameters with Todos.Clients; with Todos.Models; ...   Client : Todos.Clients.Client_Type;   List   : Todos.Models.Todo_Type_Vectors.Vector;   Empty  : Swagger.Nullable_String := (Is_Null => True, Value => <>);   ...     Client.Set_Server (“http://localhost:8080/v1”);     Client.List_Todos (Empty, List);
  • 21. https://github.com/stcarrez/swagger-ada 21 Writing a REST Ada server ● Write the OpenAPI/Swagger description file ● Generate the Ada server code ● Implement the server operations ● Share the OpenAPI description on SwaggerHub!
  • 22. https://github.com/stcarrez/swagger-ada 22 Server: let’s generate the code! $ java -jar swagger-codegen-cli.jar generate -l ada-server -i todo.yaml -DprojectName=Todos --model-package Todos ● Generate the server code with Swagger Codegen Server skeleton: package Todos.Skeletons Model API: package Todos.Models Server: procedure Todos.Server GNAT project, server configuration file Server code: package Todos.Servers
  • 23. https://github.com/stcarrez/swagger-ada 23 Ada REST Server Generated code Your server code and application Swagger runtime Brings REST server support with security and OAuth2 support on server side Ada Security Swagger Ada Ada Utility Library Server Skeleton & Model Server Application Ada Servlet XML/Ada AWS
  • 24. https://github.com/stcarrez/swagger-ada 24 Server Skeleton ● Declares the Server_Type limited interface to describe the operations ● Additional Context_Type object gives access to request, response ● Two generic packages for server skeleton provide two server models: – Instance per request – Global shared instance within a protected object package Todos.Skeletons is    type Server_Type is limited interface;        procedure Create_Todo       (Server  : in out Server_Type;        Title   : in Swagger.Ustring;        Result  : out Todos.Models.Todo_Type;        Context : in out Swagger.Servers.Context_Type) is abstract;    ... end Todos.Skeletons;
  • 25. https://github.com/stcarrez/swagger-ada 25 Server Implementation (1/2) ● Implement the Server_Type interface with its operations ● Populate Result or use the Context to send an error ● Serialization/Deserialization handled by the skeleton package Todos.Servers is    type Server_Type is limited new Todos.Skeletons.Server_Type ...        overriding procedure Create_Todo       (Server  : in out Server_Type;        Title   : in Swagger.Ustring;        Result  : out Todos.Models.Todo_Type;        Context : in out Swagger.Servers.Context_Type);    ... end Todos.Servers;
  • 26. https://github.com/stcarrez/swagger-ada 26 Server Implementation (2/2) ● Instantiate one of the two server skeletons (per-request model or shared model) ● Register the OpenAPI to the application package Todos.Servers is    ...    package Server_Impl is       new Todos.Skeletons.Shared_Instance (Server_Type); end Todos.Servers; procedure Todos.Server is    App : aliased Swagger.Servers.Applications.Application_Type; begin    . . .    Todos.Servers.Server_Impl.Register (App);    . . . end Todos.Server;
  • 27. https://github.com/stcarrez/swagger-ada 27 OpenAPI: Describing security ● Describe security endpoints ● Describe security scopes ● Assign required security scopes to operations paths: /todos: get: ... parameters: ... responses: ... security: - todo_auth: - 'read:todo' Security: - todo_auth: [] securityDefinitions: todo_auth: type: oauth2 flow: password tokenUrl: /v1/oauth/token scopes: 'write:todo': Write a todo 'read:todo': Read a todo
  • 28. https://github.com/stcarrez/swagger-ada 28 Client Security with OAuth2 (1/2) ● Create and initialize a credentials object ● Obtain the access token and optional refresh token ● Configure the client to use the credentials with Swagger.Credentials.OAuth; Cred : aliased Swagger.Credentials.OAuth.OAuth2_Credential_Type; ...    Cred.Set_Application_Identifier ("todo­app");    Cred.Set_Application_Secret ("todo­app­secret");    Cred.Set_Provider_URI ("http://localhost:8080/v1/oauth/token");    Cred.Request_Token (Username, Password, "read:todo write:todo"); ...    Client.Set_Credentials (Cred’Access);
  • 29. https://github.com/stcarrez/swagger-ada 29 Client Security with OAuth2 (2/2) ● Make API calls: credentials are passed on the wire within the ‘Authorization’ header List : Todos.Models.Todo_Type_Vectors.Vector; ...    Client.List_Todos (Empty, List); GET /v1/todos HTTP/1.1 Host: localhost:8080 Authorization: Bearer 74rE0wU.d44CPAll_kyyB2krd8bYdVYWqgmtloIR.9zyiBM Accept: application/json
  • 30. https://github.com/stcarrez/swagger-ada 30 Server security (1/2) ● Each OpenAPI scope represented by a permission definition (generated code): ● Authentication and permission check generated in the server skeleton (generated code): if not Context.Is_Authenticated then    Context.Set_Error (401, "Not authenticated");    return; end if; if not Context.Has_Permission (ACL_Read_Todo.Permission) then    Context.Set_Error (403, "Permission denied");    return; end if; package ACL_Read_Todo    is new Security.Permissions.Definition ("read:todo");
  • 31. https://github.com/stcarrez/swagger-ada 31 Server security (2/2) ● Configure the server key for OAuth2 tokens: ● Configure the server to register the client id and secret ● Configure the users allowed to authenticate app.list=1 app.1.client_id=todo­app app.1.client_secret=todo­app­secret app.1.scope=none users.list=1,2 users.1.username=admin users.1.password=admin users.2.username=test users.2.password=test swagger.key=Y29naGk5SGkKUG9YaTdhaHgKYWlUaGllM3UK
  • 35. https://github.com/stcarrez/swagger-ada 35 Demo: Running the client Server not started 404 error received
  • 36. https://github.com/stcarrez/swagger-ada 36 Limitations and improvements ● Ada Strings (need a lot of To_String/To_Ustring conversions) ● Enums are treated as strings ● Circular type dependencies not handled ● Error model needs some work ● Improvements: – Upload file support – Asynchronous client operation call
  • 37. https://github.com/stcarrez/swagger-ada 37 Conclusion ● OpenAPI describes REST APIs ● Swagger Codegen generates Ada code for you ● Swagger Ada is a runtime library for REST client and REST server implementation ● More than 500 APIs available, write your own! ● Next step: … GraphQL?