I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger

35
1 WORKSHOP Design-First API Development Using Swagger & Node Apigee Engineering

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

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

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

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

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

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

Design-First API Development

6

Page 7: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger

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

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

Swagger-Node

9

Page 10: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger

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

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

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

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

Installation

14

Page 15: I Love APIs 2015 API Lab Design-first API Development Using Node and Swagger

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

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

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

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

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

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

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

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

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

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

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

..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

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

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

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

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

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

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

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

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

Thank You

35