I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger
-
Upload
apigee-google-cloud -
Category
Software
-
view
1.079 -
download
0
Transcript of I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger
![Page 1: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/1.jpg)
1
WORKSHOPDesign-First API Development Using Swagger & Node
Apigee Engineering
![Page 2: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/2.jpg)
Agenda
2
1. What is Design-First API Development?
2. Installation
3. The famous hello-world
4. Project Conventions
5. Mock mode i.e response simulation
6. Query Parameters
7. Adding a new operation
8. How about POST?
9. Deploy to Apigee or Your PAAS.©2015 Apigee. All Rights Reserved.
![Page 3: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/3.jpg)
Some social media love
3©2015 Apigee. All Rights Reserved.
![Page 4: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/4.jpg)
Some social media love
@apigee Awesome API development workshop #iloveapis #nodejs
4©2015 Apigee. All Rights Reserved.
![Page 5: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/5.jpg)
APIs are for…
Developers
Apps :
● External
● Internal
Microservices
Devices
5©2015 Apigee. All Rights Reserved.
![Page 6: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/6.jpg)
Design-First API Development
6
![Page 7: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/7.jpg)
Zen of API Development
The code defines the API
The API generates the code
The code is the API
API-driven code
7©2015 Apigee. All Rights Reserved.
![Page 8: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/8.jpg)
API-driven code philosophy
8©2015 Apigee. All Rights Reserved.
The API must be designed first.
The artifact that represents the API design must drive the API runtime.
The API design will change, and the framework must make it possible to adapt quickly without letting the code, design, and documentation fall out of sync.
The "DRY" Principle
![Page 9: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/9.jpg)
Swagger-Node
9
![Page 10: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/10.jpg)
Swagger-Node
10©2015 Apigee. All Rights Reserved.
The API is written in Swagger, optionally using Swagger-Editor
The Swagger API document is parsed when server starts
Incoming calls are classified, validated, and routed in real time
Integrates with Connect, Express, Hapi, Restify, Sails...
Incorporates a plugin model for Swagger (or non-Swagger) extensions
![Page 11: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/11.jpg)
Swagger-Node: Flow Diagram
11©2015 Apigee. All Rights Reserved.
![Page 12: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/12.jpg)
But why Swagger ?
12©2015 Apigee. All Rights Reserved.
![Page 13: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/13.jpg)
But why Node.js ?
13©2015 Apigee. All Rights Reserved.
![Page 14: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/14.jpg)
Installation
14
![Page 15: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/15.jpg)
Installation -1
1. Make sure you have node.js installed. v4.1.2 preferred.
https://nodejs.org/en/
$ node --version
15©2015 Apigee. All Rights Reserved.
![Page 16: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/16.jpg)
Installation -2
Install
$ sudo npm install -g swagger
Verify:
$ swagger --version
0.7.4
DONE
16©2015 Apigee. All Rights Reserved.
![Page 17: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/17.jpg)
What do you get?
• CLI• project scaffolding• project lifecycle
• Your own API Studio (sort of :-))• Write YAML• Immediate feedback loop• Try-it on the fly
• Runtime
17©2015 Apigee. All Rights Reserved.
![Page 18: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/18.jpg)
Let’s take a tour of API Studio
• Code Completion• Immediate feedback loop• Simulated Response aka “Mock Mode”• Collaboration• Download YAML/JSON & Node.js project• Generated doc• Raw Spec endpoint
18©2015 Apigee. All Rights Reserved.
![Page 19: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/19.jpg)
Let’s create a project
$ swagger project create
19©2015 Apigee. All Rights Reserved.
![Page 20: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/20.jpg)
Let’s run the project
$ cd $project-name
$ swagger project start
20©2015 Apigee. All Rights Reserved.
![Page 21: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/21.jpg)
Let’s make some API call
$ curl http://127.0.0.1:10010/hello?name=Scott
21©2015 Apigee. All Rights Reserved.
![Page 22: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/22.jpg)
Let’s not forget the tests
$ swagger project generate-test
$ swagger project test
22©2015 Apigee. All Rights Reserved.
![Page 23: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/23.jpg)
How about editing ?
$ swagger project edit
23©2015 Apigee. All Rights Reserved.
![Page 24: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/24.jpg)
Dissecting it
• Project Conventions• swagger spec• controllers• helpers
24©2015 Apigee. All Rights Reserved.
![Page 25: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/25.jpg)
Let’s add something new
• A new path that uses POST operation
25©2015 Apigee. All Rights Reserved.
![Page 26: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/26.jpg)
..which requires
• A new controller
26©2015 Apigee. All Rights Reserved.
![Page 27: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/27.jpg)
Try it out
curl -X POST http://127.0.0.1:10010/conf/add -H "content-type:application/json" -d '{"x":5,"y":6}'
27©2015 Apigee. All Rights Reserved.
![Page 28: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/28.jpg)
How about some API Management
• Let’s add quota
28©2015 Apigee. All Rights Reserved.
![Page 29: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/29.jpg)
Quota : Get the bits
• Let’s add quota [This is subject to change. See latest at https://github.com/apigee-127/volos-swagger-apply/blob/master/README.md]
npm install --save volos-swagger-apply
npm install --save volos-quota-memory
29©2015 Apigee. All Rights Reserved.
![Page 30: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/30.jpg)
Quota : Annotate your Swaggerx-volos-resources:
MyQuota:
provider: volos-quota-memory
options:
timeUnit: minute
interval: 1
allow: 1
x-volos-apply:
MyQuota: {}
30©2015 Apigee. All Rights Reserved.
![Page 31: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/31.jpg)
Quota : Tell the framework about it.
Add fitting to config/default.yaml to swagger_controllers :
- volos-swagger-apply
31©2015 Apigee. All Rights Reserved.
![Page 32: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/32.jpg)
Quota : Verify that it works
curl -X POST http://127.0.0.1:10010/conf/add -H "content-type:application/json" -d '{"x":5,"y":6}'
{"message":"exceeded quota","status":403}
32©2015 Apigee. All Rights Reserved.
![Page 33: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/33.jpg)
Deploy to Apigee
sudo npm install -g apigeetool
apigeetool deploynodeapp -u [email protected] -o sdoe -e test -n 'Test Node App 2' -d . -m app.js -b /node2
OR
a127 project deploy
33©2015 Apigee. All Rights Reserved.
![Page 34: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/34.jpg)
Source: https://github.com/prabhatjha/iloveapi2015Community: https://community.apigee.com
Github: github.com/apigeegithub.com/apigee-127
Where can I get help?
34©2015 Apigee. All Rights Reserved.
![Page 35: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger](https://reader031.fdocuments.net/reader031/viewer/2022022205/58d001841a28abfc0a8b65ef/html5/thumbnails/35.jpg)
Thank You
35