SlideShare une entreprise Scribd logo
1  sur  26
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
Hammock
A Good Place to REST
June, 2016
Eyal Posener eyal@stratoscale.com
Software Engineer / Stratoscale
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
Agenda
❏ Nice to meet you - I’m Eyal
❏ Software developer at Stratoscale (~2 years)
❏ Why am I presenting Hammock?
❏ Stratoscale has moved to microservices in containers:
❏ Currently 8 internally developed REST services.
❏ Internal & external communication.
❏ Coverage:
1. Creating a simple REST application with Python.
2. What was missing for Stratoscale.
3. Hammock
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
Introducing WSGI - (Web Server Gateway Interface)Web Server
Gateway Interfa
PEP 3333 / PEP 333 Web Server Gateway Interface.
❏ Defines a simple API between the server/gateway and
framework(/application).
Server/
Gateway
Framework
WSGI
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
Many Many WSGI Players
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
Application
An application is developed with a specific framework.
Server/
Gateway
Framework
WSGI
Application
Framework
API
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
Let’s build a REST Server in Falcon
❏ REpresentational State Transfer
❏ A convention for communication:
Path pattern
HTTP
method
Action
Status
(usually)
Response
(usually)
Request Args
Location
.../<item>s POST Create new item 201/202 Dict Body
.../<item>s GET List all Items 200 List of dicts Query
.../<item>s/<item>_id GET Get <item> 200 Dict Query
.../<item>s/<item>_id PATCH/PUT Update <item> 202 Dict Body
.../<item>s/<item>_id DELETE Delete <item> 204 Nothing Query
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
class Files(object):
def on_post(self, req, resp): # create
pass
def on_get(self, req, resp): # list
pass
class File(object):
def on_get(self, req, resp, file_id): # get one
pass
def on_delete(self, req, resp, file_id): # delete
pass
def on_patch(self, req, resp, file_id): # update
pass
app = falcon.API()
app.add_route('/files', Files())
app.add_route('/files/{file_id}', File())
Falcon Application Example
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
import files # Business logic is here
class Files(object):
def on_get(self, req, resp):
"""List files"""
# First, extract request arguments
path = req.get_param('path', required=True)
sort = req.get_param('sort', default='modified')
reverse = req.get_param('reverse', default=False)
# Convert arguments if a specific type is required
reverse = reverse == 'True'
# Here we actually do stuff
try:
result = [f.to_dict for f in files.list(path, sort, reverse)]
except OSError:
raise falcon.HTTPNotFound('No path {}'.format(path))
# When all ready, prepare the response
result_string = json.dumps(result)
resp.status = falcon.HTTP_200
resp.body = result_string
resp.content_type = 'application/json'
Falcon Application Example (cont.)
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
WSGI Deficiencies
To do the same work: Different framework == different application
Once you choose a framework, you are locked in!
Server/
Gateway
Application2
Application1
Application3
WSGI
API 1
API 2
API 3
Application4
API 4
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
REST Frameworks: Same Same but Different
Same same But Different
Methods that do
something with request
and response objects.
● Some accept request and response objects.
● Some accept requests and return responses.
● Some magically have the request.
● Some accept URL arguments.
● Some convert return value to a response.
● Some can read/write from the resource class methods.
Register of Python
methods/classes on URLs.
● Some use resource classes.
● Some use resource methods.
● Some use both.
● For some the class is only for a specific URL, for some it is for a
resource.
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
REST Frameworks: Same Same but Different (cont)
Same same But Different
Error handling ● Some catch a specific exception types.
● Some need a response object returned.
Their own
Request/Response object
They were made for web
serving
With REST extensions.
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
Hammock
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
Hammock
1. Abstraction
2. Built for REST & developer friendly
3. Many cool features
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
1. Hammock Abstraction
ApplicationHammock
Server/
Gateway
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
Hammock Abstraction
Server/
Gateway
Well… to be honest....
ApplicationHammock
WIP
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
2. Ease of Use: Handlers
class Files(hammock.Resource):
@hammock.get('/')
def list(self, path, sort='modified', reverse=False, type=None):
"""
List files in directory.
:param str path: Path to list files in.
:param str type: Show only files of type.
:param str sort: Sort files by attribute
:param bool[False] reverse: Sort reverse.
:return list: List of files.
"""
try:
return [f.to_dict for f in files.list(path, sort, reverse)]
except OSError:
raise exceptions.NotFound('No path {}'.format(path))
@hammock.get('/{file_id}')
def get(self, file_id)
...
Don't worry about
converting requests to
arguments.
Don't worry about
converting results to
responses.
Simply write
Python
functions
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
2. Ease of Use: Some Corner Cases
Hammock has some reserved arguments.
# streams
@hammock.post()
def func(self, _file): # get octet/stream
return open(‘path’, ‘r’) # return octet/stream
# headers
@hammock.post()
def func(self, _headers): # get headers
return {‘_headers’: {‘key1’: ‘value1’}} # return headers
# when request body is a list
@hammock.post()
def func(self, _list): # get a list body
return [] # return list
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
2. Ease of Use: Resource Registering
Use a package structure to build URLs:
resources/
hotels/
hotels.py /hotels
rooms/
rooms.py /hotels/{hotel_id}/rooms
reservations.py /hotels/{hotel_id}/rooms/{room_id}/reservations
customers.py /customers
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
3. Many COOL Features
Hammock
Free Python
client!
Free CLIAuthorization
enforcement
Free API
documentation
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
How does it work?? At Server Startup:
app = hammock.Hammock(‘falcon’, resources)
❏ Iterate over a 'resources' package,
collect all decorated route methods
into a dict:
{path: {method: function}}
❏ Register handlers using a backend API.
❏ The handler is a translator between backend
handler to the function.
class Files(object):
def on_post(self, req, resp):
...
class File(object):
def on_get(self, req, resp, file_id):
...
app.add_route('/files', Files())
app.add_route('/files/{file_id}', File())
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
How does it work?? Upon request:
Backend
Request
Backend
Response
Hammock
Request
Hammock
Response
Method
Arguments
Method
Result
Backend
Framework
Decorated
Method
There is a lot of magic there...
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
Future work
❏ More frameworks! (maybe you wanna help?)
❏ Scheme validation.
❏ Combine with swagger/RAML.
❏ Improve unit testing infra.
❏ And much more :-)
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
Questions?
❏ Contact:
eyal@stratoscale.com
❏ Install:
pip install hammock-rest
>>> import hammock
❏ Contribute:
git clone https://github.com/Stratoscale/hammock.git
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
Performance
Property Falcon Hammock Ratio Remarks
Document Length [bytes] 638 601 1.06 ujson :-)
Time taken for tests [seconds] 36.3 38 0.96
Requests per second [#/sec] (mean) 1378.5 1314.6 1.05
Time per request [ms] (mean) 0.725 0.761 0.95
Transfer rate [Kbytes/sec] received 955.8 864 1.11
Small benchmark with Apache bench for 50,000 GET requests that returns the
same file list method.
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
WSGI
def app(environ, start_response):
start_response('200 OK', [('Content-type', 'text/plain')])
return ['Hello world!n']
® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244
Appendix
List of WSGI compliant servers.
List of WSGI compliant frameworks.

Contenu connexe

Tendances

OpenStack Heat slides
OpenStack Heat slidesOpenStack Heat slides
OpenStack Heat slides
dbelova
 
Apache Stratos Incubator - hangout 2
Apache Stratos Incubator - hangout   2Apache Stratos Incubator - hangout   2
Apache Stratos Incubator - hangout 2
Nirmal Fernando
 

Tendances (20)

OpenStack Tutorial For Beginners | OpenStack Tutorial | OpenStack Training | ...
OpenStack Tutorial For Beginners | OpenStack Tutorial | OpenStack Training | ...OpenStack Tutorial For Beginners | OpenStack Tutorial | OpenStack Training | ...
OpenStack Tutorial For Beginners | OpenStack Tutorial | OpenStack Training | ...
 
Cooking with OpenStack Heat
Cooking with OpenStack HeatCooking with OpenStack Heat
Cooking with OpenStack Heat
 
Red Hat Enteprise Linux Open Stack Platfrom Director
Red Hat Enteprise Linux Open Stack Platfrom DirectorRed Hat Enteprise Linux Open Stack Platfrom Director
Red Hat Enteprise Linux Open Stack Platfrom Director
 
Eric Williams (Rackspace) - Using Heat on OpenStack
Eric Williams (Rackspace) - Using Heat on OpenStackEric Williams (Rackspace) - Using Heat on OpenStack
Eric Williams (Rackspace) - Using Heat on OpenStack
 
OpenStack Orchestration (Heat)
OpenStack Orchestration (Heat)OpenStack Orchestration (Heat)
OpenStack Orchestration (Heat)
 
Understanding AWS with Terraform
Understanding AWS with TerraformUnderstanding AWS with Terraform
Understanding AWS with Terraform
 
Best Practice for Deploying Application with Heat
Best Practice for Deploying Application with HeatBest Practice for Deploying Application with Heat
Best Practice for Deploying Application with Heat
 
Terraform: An Overview & Introduction
Terraform: An Overview & IntroductionTerraform: An Overview & Introduction
Terraform: An Overview & Introduction
 
Spark day 2017 - Spark on Kubernetes
Spark day 2017 - Spark on KubernetesSpark day 2017 - Spark on Kubernetes
Spark day 2017 - Spark on Kubernetes
 
Adam Dagnall: Advanced S3 compatible storage integration in CloudStack
Adam Dagnall: Advanced S3 compatible storage integration in CloudStackAdam Dagnall: Advanced S3 compatible storage integration in CloudStack
Adam Dagnall: Advanced S3 compatible storage integration in CloudStack
 
Introduction openstack horizon
Introduction openstack horizonIntroduction openstack horizon
Introduction openstack horizon
 
Red Hat OpenStack - Open Cloud Infrastructure
Red Hat OpenStack - Open Cloud InfrastructureRed Hat OpenStack - Open Cloud Infrastructure
Red Hat OpenStack - Open Cloud Infrastructure
 
Best Practices of Infrastructure as Code with Terraform
Best Practices of Infrastructure as Code with TerraformBest Practices of Infrastructure as Code with Terraform
Best Practices of Infrastructure as Code with Terraform
 
OpenStack Heat slides
OpenStack Heat slidesOpenStack Heat slides
OpenStack Heat slides
 
Terraform
TerraformTerraform
Terraform
 
Case Study: Using Terraform and Packer to deploy go applications to AWS
Case Study: Using Terraform and Packer to deploy go applications to AWSCase Study: Using Terraform and Packer to deploy go applications to AWS
Case Study: Using Terraform and Packer to deploy go applications to AWS
 
Oracle on kubernetes 101 - Dec/2021
Oracle on kubernetes 101 - Dec/2021Oracle on kubernetes 101 - Dec/2021
Oracle on kubernetes 101 - Dec/2021
 
DevEx | there’s no place like k3s
DevEx | there’s no place like k3sDevEx | there’s no place like k3s
DevEx | there’s no place like k3s
 
On CloudStack, Docker, Kubernetes, and Big Data…Oh my ! By Sebastien Goasguen...
On CloudStack, Docker, Kubernetes, and Big Data…Oh my ! By Sebastien Goasguen...On CloudStack, Docker, Kubernetes, and Big Data…Oh my ! By Sebastien Goasguen...
On CloudStack, Docker, Kubernetes, and Big Data…Oh my ! By Sebastien Goasguen...
 
Apache Stratos Incubator - hangout 2
Apache Stratos Incubator - hangout   2Apache Stratos Incubator - hangout   2
Apache Stratos Incubator - hangout 2
 

En vedette

En vedette (10)

Muli Ben-Yehuda, Stratoscale - The Road to a Hyper-Converged OpenStack, OpenS...
Muli Ben-Yehuda, Stratoscale - The Road to a Hyper-Converged OpenStack, OpenS...Muli Ben-Yehuda, Stratoscale - The Road to a Hyper-Converged OpenStack, OpenS...
Muli Ben-Yehuda, Stratoscale - The Road to a Hyper-Converged OpenStack, OpenS...
 
Stratosphere hotel presentation
Stratosphere hotel presentationStratosphere hotel presentation
Stratosphere hotel presentation
 
Developing the Stratoscale System at Scale - Muli Ben-Yehuda, Stratoscale - D...
Developing the Stratoscale System at Scale - Muli Ben-Yehuda, Stratoscale - D...Developing the Stratoscale System at Scale - Muli Ben-Yehuda, Stratoscale - D...
Developing the Stratoscale System at Scale - Muli Ben-Yehuda, Stratoscale - D...
 
Avishay Traeger & Shimshon Zimmerman, Stratoscale - Deploying OpenStack Cinde...
Avishay Traeger & Shimshon Zimmerman, Stratoscale - Deploying OpenStack Cinde...Avishay Traeger & Shimshon Zimmerman, Stratoscale - Deploying OpenStack Cinde...
Avishay Traeger & Shimshon Zimmerman, Stratoscale - Deploying OpenStack Cinde...
 
Architectural Patterns for Scaling Microservices and APIs - GlueCon 2015
Architectural Patterns for Scaling Microservices and APIs - GlueCon 2015Architectural Patterns for Scaling Microservices and APIs - GlueCon 2015
Architectural Patterns for Scaling Microservices and APIs - GlueCon 2015
 
Approaching hyperconvergedopenstack
Approaching hyperconvergedopenstackApproaching hyperconvergedopenstack
Approaching hyperconvergedopenstack
 
Stateless authentication for microservices
Stateless authentication for microservicesStateless authentication for microservices
Stateless authentication for microservices
 
From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.
 
Open API and API Management - Introduction and Comparison of Products: TIBCO ...
Open API and API Management - Introduction and Comparison of Products: TIBCO ...Open API and API Management - Introduction and Comparison of Products: TIBCO ...
Open API and API Management - Introduction and Comparison of Products: TIBCO ...
 
The Role of Enterprise Integration in Digital Transformation
The Role of Enterprise Integration in Digital TransformationThe Role of Enterprise Integration in Digital Transformation
The Role of Enterprise Integration in Digital Transformation
 

Similaire à Hammock, a Good Place to Rest

Going live with BommandBox and docker Into The Box 2018
Going live with BommandBox and docker Into The Box 2018Going live with BommandBox and docker Into The Box 2018
Going live with BommandBox and docker Into The Box 2018
Ortus Solutions, Corp
 
Terrastore - A document database for developers
Terrastore - A document database for developersTerrastore - A document database for developers
Terrastore - A document database for developers
Sergio Bossa
 
From HelloWorld to Configurable and Reusable Apache Spark Applications in Sca...
From HelloWorld to Configurable and Reusable Apache Spark Applications in Sca...From HelloWorld to Configurable and Reusable Apache Spark Applications in Sca...
From HelloWorld to Configurable and Reusable Apache Spark Applications in Sca...
Databricks
 
REST made simple with Java
REST made simple with JavaREST made simple with Java
REST made simple with Java
elliando dias
 

Similaire à Hammock, a Good Place to Rest (20)

Intro to Rack
Intro to RackIntro to Rack
Intro to Rack
 
Arquitecturas de microservicios - Medianet Software
Arquitecturas de microservicios   -  Medianet SoftwareArquitecturas de microservicios   -  Medianet Software
Arquitecturas de microservicios - Medianet Software
 
Frontend Servers and NGINX: What, Where and How
Frontend Servers and NGINX: What, Where and HowFrontend Servers and NGINX: What, Where and How
Frontend Servers and NGINX: What, Where and How
 
Rack
RackRack
Rack
 
Using and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareUsing and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middleware
 
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディングXitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
 
Xitrum @ Scala Matsuri Tokyo 2014
Xitrum @ Scala Matsuri Tokyo 2014Xitrum @ Scala Matsuri Tokyo 2014
Xitrum @ Scala Matsuri Tokyo 2014
 
Into The Box 2018 Going live with commandbox and docker
Into The Box 2018 Going live with commandbox and dockerInto The Box 2018 Going live with commandbox and docker
Into The Box 2018 Going live with commandbox and docker
 
Going live with BommandBox and docker Into The Box 2018
Going live with BommandBox and docker Into The Box 2018Going live with BommandBox and docker Into The Box 2018
Going live with BommandBox and docker Into The Box 2018
 
Terrastore - A document database for developers
Terrastore - A document database for developersTerrastore - A document database for developers
Terrastore - A document database for developers
 
From HelloWorld to Configurable and Reusable Apache Spark Applications in Sca...
From HelloWorld to Configurable and Reusable Apache Spark Applications in Sca...From HelloWorld to Configurable and Reusable Apache Spark Applications in Sca...
From HelloWorld to Configurable and Reusable Apache Spark Applications in Sca...
 
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiGrâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
 
REST made simple with Java
REST made simple with JavaREST made simple with Java
REST made simple with Java
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and Scala
 
Presentation on Japanese doc sprint
Presentation on Japanese doc sprintPresentation on Japanese doc sprint
Presentation on Japanese doc sprint
 
Just one-shade-of-openstack
Just one-shade-of-openstackJust one-shade-of-openstack
Just one-shade-of-openstack
 
REST made simple with Java
REST made simple with JavaREST made simple with Java
REST made simple with Java
 
Taking Jenkins Pipeline to the Extreme
Taking Jenkins Pipeline to the ExtremeTaking Jenkins Pipeline to the Extreme
Taking Jenkins Pipeline to the Extreme
 
Scale Your Data Tier With Windows Server App Fabric
Scale Your Data Tier With Windows Server App FabricScale Your Data Tier With Windows Server App Fabric
Scale Your Data Tier With Windows Server App Fabric
 
Nginx: Accelerate Rails, HTTP Tricks
Nginx: Accelerate Rails, HTTP TricksNginx: Accelerate Rails, HTTP Tricks
Nginx: Accelerate Rails, HTTP Tricks
 

Dernier

Dernier (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Hammock, a Good Place to Rest

  • 1. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 Hammock A Good Place to REST June, 2016 Eyal Posener eyal@stratoscale.com Software Engineer / Stratoscale
  • 2. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 Agenda ❏ Nice to meet you - I’m Eyal ❏ Software developer at Stratoscale (~2 years) ❏ Why am I presenting Hammock? ❏ Stratoscale has moved to microservices in containers: ❏ Currently 8 internally developed REST services. ❏ Internal & external communication. ❏ Coverage: 1. Creating a simple REST application with Python. 2. What was missing for Stratoscale. 3. Hammock
  • 3. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 Introducing WSGI - (Web Server Gateway Interface)Web Server Gateway Interfa PEP 3333 / PEP 333 Web Server Gateway Interface. ❏ Defines a simple API between the server/gateway and framework(/application). Server/ Gateway Framework WSGI
  • 4. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 Many Many WSGI Players
  • 5. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 Application An application is developed with a specific framework. Server/ Gateway Framework WSGI Application Framework API
  • 6. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 Let’s build a REST Server in Falcon ❏ REpresentational State Transfer ❏ A convention for communication: Path pattern HTTP method Action Status (usually) Response (usually) Request Args Location .../<item>s POST Create new item 201/202 Dict Body .../<item>s GET List all Items 200 List of dicts Query .../<item>s/<item>_id GET Get <item> 200 Dict Query .../<item>s/<item>_id PATCH/PUT Update <item> 202 Dict Body .../<item>s/<item>_id DELETE Delete <item> 204 Nothing Query
  • 7. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 class Files(object): def on_post(self, req, resp): # create pass def on_get(self, req, resp): # list pass class File(object): def on_get(self, req, resp, file_id): # get one pass def on_delete(self, req, resp, file_id): # delete pass def on_patch(self, req, resp, file_id): # update pass app = falcon.API() app.add_route('/files', Files()) app.add_route('/files/{file_id}', File()) Falcon Application Example
  • 8. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 import files # Business logic is here class Files(object): def on_get(self, req, resp): """List files""" # First, extract request arguments path = req.get_param('path', required=True) sort = req.get_param('sort', default='modified') reverse = req.get_param('reverse', default=False) # Convert arguments if a specific type is required reverse = reverse == 'True' # Here we actually do stuff try: result = [f.to_dict for f in files.list(path, sort, reverse)] except OSError: raise falcon.HTTPNotFound('No path {}'.format(path)) # When all ready, prepare the response result_string = json.dumps(result) resp.status = falcon.HTTP_200 resp.body = result_string resp.content_type = 'application/json' Falcon Application Example (cont.)
  • 9. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 WSGI Deficiencies To do the same work: Different framework == different application Once you choose a framework, you are locked in! Server/ Gateway Application2 Application1 Application3 WSGI API 1 API 2 API 3 Application4 API 4
  • 10. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 REST Frameworks: Same Same but Different Same same But Different Methods that do something with request and response objects. ● Some accept request and response objects. ● Some accept requests and return responses. ● Some magically have the request. ● Some accept URL arguments. ● Some convert return value to a response. ● Some can read/write from the resource class methods. Register of Python methods/classes on URLs. ● Some use resource classes. ● Some use resource methods. ● Some use both. ● For some the class is only for a specific URL, for some it is for a resource.
  • 11. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 REST Frameworks: Same Same but Different (cont) Same same But Different Error handling ● Some catch a specific exception types. ● Some need a response object returned. Their own Request/Response object They were made for web serving With REST extensions.
  • 12. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 Hammock
  • 13. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 Hammock 1. Abstraction 2. Built for REST & developer friendly 3. Many cool features
  • 14. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 1. Hammock Abstraction ApplicationHammock Server/ Gateway
  • 15. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 Hammock Abstraction Server/ Gateway Well… to be honest.... ApplicationHammock WIP
  • 16. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 2. Ease of Use: Handlers class Files(hammock.Resource): @hammock.get('/') def list(self, path, sort='modified', reverse=False, type=None): """ List files in directory. :param str path: Path to list files in. :param str type: Show only files of type. :param str sort: Sort files by attribute :param bool[False] reverse: Sort reverse. :return list: List of files. """ try: return [f.to_dict for f in files.list(path, sort, reverse)] except OSError: raise exceptions.NotFound('No path {}'.format(path)) @hammock.get('/{file_id}') def get(self, file_id) ... Don't worry about converting requests to arguments. Don't worry about converting results to responses. Simply write Python functions
  • 17. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 2. Ease of Use: Some Corner Cases Hammock has some reserved arguments. # streams @hammock.post() def func(self, _file): # get octet/stream return open(‘path’, ‘r’) # return octet/stream # headers @hammock.post() def func(self, _headers): # get headers return {‘_headers’: {‘key1’: ‘value1’}} # return headers # when request body is a list @hammock.post() def func(self, _list): # get a list body return [] # return list
  • 18. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 2. Ease of Use: Resource Registering Use a package structure to build URLs: resources/ hotels/ hotels.py /hotels rooms/ rooms.py /hotels/{hotel_id}/rooms reservations.py /hotels/{hotel_id}/rooms/{room_id}/reservations customers.py /customers
  • 19. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 3. Many COOL Features Hammock Free Python client! Free CLIAuthorization enforcement Free API documentation
  • 20. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 How does it work?? At Server Startup: app = hammock.Hammock(‘falcon’, resources) ❏ Iterate over a 'resources' package, collect all decorated route methods into a dict: {path: {method: function}} ❏ Register handlers using a backend API. ❏ The handler is a translator between backend handler to the function. class Files(object): def on_post(self, req, resp): ... class File(object): def on_get(self, req, resp, file_id): ... app.add_route('/files', Files()) app.add_route('/files/{file_id}', File())
  • 21. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 How does it work?? Upon request: Backend Request Backend Response Hammock Request Hammock Response Method Arguments Method Result Backend Framework Decorated Method There is a lot of magic there...
  • 22. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 Future work ❏ More frameworks! (maybe you wanna help?) ❏ Scheme validation. ❏ Combine with swagger/RAML. ❏ Improve unit testing infra. ❏ And much more :-)
  • 23. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 Questions? ❏ Contact: eyal@stratoscale.com ❏ Install: pip install hammock-rest >>> import hammock ❏ Contribute: git clone https://github.com/Stratoscale/hammock.git
  • 24. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 Performance Property Falcon Hammock Ratio Remarks Document Length [bytes] 638 601 1.06 ujson :-) Time taken for tests [seconds] 36.3 38 0.96 Requests per second [#/sec] (mean) 1378.5 1314.6 1.05 Time per request [ms] (mean) 0.725 0.761 0.95 Transfer rate [Kbytes/sec] received 955.8 864 1.11 Small benchmark with Apache bench for 50,000 GET requests that returns the same file list method.
  • 25. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 WSGI def app(environ, start_response): start_response('200 OK', [('Content-type', 'text/plain')]) return ['Hello world!n']
  • 26. ® Copyright Stratoscale www.stratoscale.com @stratoscale +1 877 420 3244 Appendix List of WSGI compliant servers. List of WSGI compliant frameworks.

Notes de l'éditeur

  1. סטרארטוסקייל מספקת פתרונות ענן פרטי שנותן חויה כמו של הענן הציבורי (למשל AWS) עם OPENSTACK APIS
  2. ה WSGI הוא פרוטוקול שנכתב ב 2003 כדי לקשר בין סרבר לפריימוורק מהו הסרבר ניהול הקונקשנים ניהול הוורקרים הפיכת בתים מ TCP לאובייקטים פייתונים. ותרגום חזרה מאובייקטים פייתונים לבתים ב TCP. מהו הפריימוורק ניהול הראוטים. ניהול שגיאות. הפיכת ה WSGI לידידותי למשתמש. למה האפליקיישן בסוגריים
  3. יש הרבה שחקנים. חלק ממשו את צד הסרבר, חלק את הפריימוורק וחלק את שניהם.
  4. לא להתעכב פה באנו לדבר על מימוש רסט מעל HTTP, לא על ווב. REST זה קונוונציה. יש 5 פעולות עיקריות. לא כולם עוקבים אחרי זה. מפתח לא צריך ללמוד את זה אם יש לך מפתח שלא מכיר, אתה API לא קונסיסטנטי.
  5. בוא ניקח מימוש של 5 הפעולות העיקריות על resource של קבצים בפלקון. הפונקציות האלו נקראות handlers או views. קליק - צריך שתי מחלקות. קליק - לרשום אותם פעמיים לאפליקציה. אוקיי, קליק - בוא נתרכז רגע בפונקציה הזו. - בשקף הבא
  6. דוגמא ל route שמחזיר רשימה של קבצים. קליק - במקום להתעסק רק ב business logic למפתח יש overhead קבוע, עבור כל פונקציה. המפתח מקבל שני אבייקטים, רקווסט ורספונס. בחלק הראשון הוא מתעסק בלהוציא את הארגומנטים מהרקווסט בחלק השני הוא מתעסק בלדחוף נתונים לרספונס.
  7. רובם נועדו לשרת בקשות ווב, והוסיפו להם אקסטנשנז לרסט.
  8. ארגומנטים: פשוט רושמים פונקציה פייתונית. ארגומנטים מנדטורים, אופציונלים, טייפים. מחזירים רשימה/ דיקט. מומר לריספונס אוטומטית. קליק קליק קליק
  9. בכל זאת מדובר ב REST, ועדיין יש כמה דברים שהם לא כמו פונקציות פייתוניות רגילות. השתדלנו להסתיר אותם ככל שאפשר.
  10. משתמשים בהיררכית ספריות בשביל ליצור את ה path.
  11. קליק: דוגמא של FALCON
  12. לתמוך בשפה עשירה יותר של קלטים.