Nom Nom: Consuming REST APIs

Post on 07-Jan-2017

166 views 0 download

transcript

ZendCon Conference October 2016Consuming Rest APIs

Follow me on Twitter and/or tweet at me. @tessamero

By Tessa Mero

Nom Nom: Consuming Rest APIs

2© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

CONSUME THE REST APIS!

3© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

• Who Am I?• WHAT ARE APIs• Requests and Responses• Intro to APIs• Playing with Spark API• Playing with Tropo API• Taking Knowledge Home With You

Overview

@tessamero

4© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

• Developer Advocate• Teacher/Mentor• Previously Web Developer• <3 Free & Open Source• Community Leader for

Joomla and PHP

• Mother of 2• I love APIs• Addicted to Twitter

(@tessamero)

Who Am I?

5© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Cisco DevNet? Why Am I Here?

• What is DevNet?• What is the Purpose?• Why do we go to programming

conferences?

• Not Selling Anything• We Like Giving Out Swag• We Like attention

@ciscodevnet

6© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Why are API’s so important?

7© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

API Growth from 2000 to 2016

2000 2005 2007 2009 2011 2013 2015 20160

5000

10000

15000

20000

25000

30000

35000API's

API's

Source: 2016 data from nordicapis.com. 2005-2015 data from rubenverborgh.github.io and 2000-2004 data from blog.cutter.com

8© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Okay, What is an API?

9© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Ready for a lot of examples? I’ll make it sound easy

10© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

“It’s a way for two pieces of software to talk to each other”

Application Programming Interface

11© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

The API is the User Interface for software systems

12© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

APIs are sets of requirements that govern how one application can talk to another.

13© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

An API is like …

An API (Application Programming Interface) is best thought of as a contract provided by one piece of computer software to another.

14© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

APIs help developers create apps that benefit the end user.

Yelp asks for Map Data

Google Maps returns map data

via API

Users sees list of

restaurants close to

them

15© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

15Presentation ID

-- Programmable Web

APIs are often referred to as “an engine of innovation.”

16© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

REST API versus Web API

• No difference at all. It doesn’t give a different output.

• HTTP implements methods.

• A REST interface simply sends request to server

17© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Three Types of Web Services for Providing APIsRemote Procedure Call (RPC) or XML-RPC

• Single URI

• Response is in a Structured Format

• Lacks Responses

Simple Object Access Protocol (SOAP)

• More powerful, preferred by software vendors (MSFT,.NET,Java Entprse Edition, etc)

• SOAP most used tool 2001-2007

Representational State Transfer (REST)

• Most popular web service

• Each unique URL is a representational of some object

• Easy to read results

• Light Weight – Not much XML markup

http://tiny.cc/webservices

18© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Requests and Responses

19© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

19

View a Web Page

Presentation ID

20© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

20

View a Web Page

Presentation ID

Request GET /index.htm HTTP/1.1

ResponseHTTP 200 OK

<html>

21© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

REST APIs use Request and Response too

22© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

• 200 OK• 301 Moved Permanently• 302 Found• 307 Temporary Redirect• 400 Bad Request• 403 Forbidden

Common HTTP Status Codes

• 404 Not Found• 500 Internal Server

Error• 550 Permission Denied

23© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Using Cisco Spark API as a Use Case. Of course =P

24© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

So how do you interact with this API?

25© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

25

Get Data using an API

Presentation ID

26© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

26

Get Data using an API

Presentation ID

Request GET /rooms

ResponseHTTP 200 OK

{json data}

API Consumer API Provider

27© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

28© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Tools to Debug/Test http://tiny.cc/60tools

• Webhook Debugging (RequestBin formerly requestbin.org…)• Webhook Utilities (Torpio…)• Local Tunneling (ngrok...)• API Monitoring (Runscope...)• Response Mocking (mocky.io...) • JSON Utilities (JSONFormat...)• OAUTH Utilities (oauth.io...)• API Directories (APIS.io, ProgrammableWeb...)• API Testing (Runscope Radar...)• Load Testing (loader.io...)• GUI HTTP Clients (POSTMAN...)

29© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Postman!!!! =)

30© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Test Your REST API Requests = FUN

31© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

31

method

url

requestheaders

status code

response headers

content-type

responsebody

query parameters

32© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Now What? How Do You Figure Out How To Use an API?

33© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

One Word: Documentation

34© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

1. First, find the documentation page. If you cannot find it, then that’s a problem.

2. Find the API Reference.

3. Figure out what you want to do.

4. Test your API request via Postman because it’s fun :P

Reviewing the Documentation

35© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

The API Reference!

36© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

API Reference: Rooms

37© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Room Created!

38© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

PHPCruise Room Created!

Whoa!!!!

39© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

You created a room, now how do you post a message to it through an API request?

40© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

1. Take Note of Key generated when you created a room, so now your room has an ID.

2. Refer to documentation for Creating a Message3. Test the API request.

4. Make Magic Happen.

Create Message API Request

41© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Oh yeah, the documentation….

42© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

But what about request parameters?

43© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Oh yeah, the documentation!

44© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Create your request parameters in the body.Feelin’ 200 OK

Oh my gosh I can’t see it what does it say?

45© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

One important thing to do when you receive a 200 OK HTTP Status:(Please take note of this)

46© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

47© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Alright I get it. Now show me something else that’s cool!

48© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

How About… Applications Interacting With a Cell Phone?!

49© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Introducing the Tropo API! IT’S SO MUCH FUNZ

50© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

What Can You Do With Tropo?

51© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Create an Application:

Register (anyone can, free)

Click on “My Apps”

Click on “Create New App”

52© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

1. Create Name2. Click on “Edit Script” and give it a name. This is: textService.js

53© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

3. Add a Phone Number (Free for Dev)

54© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

4. Write some JavaScript

call("+14258791911", { network:"SMS”

});

say("Don't forget your meeting at 2 p.m. on Wednesday!");

55© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

5. LAUNCH IT!

You can put your token URL in the browser to make a request and fire off your app

https://api.tropo.com/1.0/sessions?action=create&token=6c7a56555271337716e76596b6266456254566e4e625058657133776456d6d7a504f4b4b41337a71624c

56© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Magic!

57© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

You can do it too!

58© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

• call();• answer();• _log();• say();• ask();• record();

• Makes a call• Answers the call (when called)• Logs the info about the call• Says something• Says something in a question• Records the call (like

voicemail), with certain parameters, you can transcribe caller input too!

Other Functions To Use

59© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Easy? I know…

60© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Spark Innovation Fund?

61© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

$1,000,000 x 150….

62© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

I like money too, trust me…

https://developer.ciscospark.com/fund/

Also, don’t forget to tweet at me @tessamero

63© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

Thank You