what are apis? - uk data service · what are apis? the webinar will begin at 3pm • you now have a...

27
What are APIs? The webinar will begin at 3pm You now have a menu in the top right corner of your screen. The red button with a white arrow allows you to expand and contract the webinar menu, in which you can write questions/comments. We won’t have time to answer questions while we are presenting, but will answer them at the end You will be on mute throughout – we can’t hear you.

Upload: others

Post on 06-Apr-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

What are APIs?

The webinar will begin at 3pm

• You now have a menu in the top right corner of your screen.

• The red button with a white arrow allows you to expand and contract the webinar menu, in which you can write questions/comments.

• We won’t have time to answer questions while we are presenting, but will answer them at the end

• You will be on mute throughout – we can’t hear you.

Page 2: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

What are APIs?

Webinar

22 March 2016

Peter SmythUK Data Service

Page 3: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Can you hear us?

Page 4: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Can you hear us?

• If Not:

• Check your volume, and that your speaker/headset is

plugged in.

• Your invitation also included a phone number, you can

call that to listen in.

o UK +44 (0) 20 3713 5012

o US +1 (415) 930-5229

• We are recording this webinar, so you can always

listen to it later.

Page 5: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Overview of this webinar

• Description of an API

• Examples of APIs

• Finding and using API documentation

• Browser based Demo of API usage

• Code based demo of using an API

Page 6: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Wikipedia - API Definition

• An API is a set of routines, protocols and tools for building software applications

• It expresses a software component in terms of its operations, inputs, outputs and underlying types

• An API defines functionalities that are independent of their respective implementations

• A programmer can use the API to build an application

An API hides the complexity of implementationUsing an API allows the implementation to change, without affecting users

Page 7: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Generic API

Application A Application B

Response

Request

Page 8: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Who needs to know what?

• Application B needs to be able to correctly format the

requests and to be able to interpret the responses

• Application A needs to be able to understand the

requests and correctly return a response

Page 9: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Who doesn’t need to know what?

• Application A doesn’t need know what application B does

with the response

• Application B doesn’t need to know how Application A

create the responses from the requests

Page 10: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Specific cases

• Application A is some kind of Web serviceo Could be simple website retuning Web pages in

response to a URL requesto Could be providing a data stream in response to a

formatted search request• Application B is a user controlled application

o A Smartphone appo A web browsero A development toolo Program code

Page 11: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Generic API

Application A

Re

spo

nse

Re

qu

est

Re

spo

nse

Re

qu

est

Re

spo

nse

Re

qu

est

Re

spo

nse

Re

qu

est

Application B

Page 12: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

The flexibility of APIs

The format of the requests and responses is fixed,

but beyond that:

• the requester could be any of many different

sources (previous slide)

• the responder is free to change the manner in

which the response is calculated or found

Page 13: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Who has an API ? - Some examples• Twitter

• https://dev.twitter.com/• Facebook

• https://developers.facebook.com/• Met Office

• http://www.metoffice.gov.uk/datapoint• The Guardian

• http://open-platform.theguardian.com/• Transport API

• http://www.transportapi.com/• Google Maps

• https://developers.google.com/maps/There are many more Google APIs, but the search API is depreciated.

Page 14: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

How do you use an API?

[Decide how to formulate the request]

Make a requestGet a response[Work out how to interpret the response]

Page 15: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

TransportAPI

• Sample call:

http://transportapi.com/v3/uk/bus/stop/490012745J/live.h

tml?group=route&api_key=d9307fd91b0247c607e098d5

effedc97&app_id=03bf8009

Page 16: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

TransportAPI Response{"atcocode": "490012745J","smscode": "47889","request_time": "2015-09-30T10:55:31+01:00","departures": {"243": [{"mode": "bus","line": "243","direction": "Wood Green","operator": "TFL","aimed_departure_time": null,"expected_departure_time": "11:13","best_departure_estimate": "11:13","source": "Countdown instant"

},...

]"55": [{"mode": "bus","line": "55","direction": "Bakers Arms","operator": "TFL","aimed_departure_time": null,"expected_departure_time": "11:08","best_departure_estimate": "11:08","source": "Countdown instant"

},...

],}

}

Page 17: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

API Documentation

• Essential to read and understand the API

documentation

o How to format requests

o The format and contents of Responses

o The use of keys

o Limitations on use

Page 18: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Example - The Guardian API

• The documentation• http://open-platform.theguardian.com/access/

• The Guardian Open Platform tool• http://open-platform.theguardian.com/explore/

Page 19: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Fiddler

• Web development tool

• Open Source & Free to download and use

• Capable of many things

o We just want to be able to send requests and

examine the responses

Page 20: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Guardian Example - R code

1. Format a request

2. Send request

3. Get response

4. Parse response to extract the weburls

5. Request the web pages for the URLs

6. For each page create a wordcloud

Page 21: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Twitter Example

• You need to have a Twitter account to use the

API

• You need a set of 4 keys

• Keys are obtained by creating an application

• The starting point is https://apps.twitter.com/

Page 22: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Retrieving Tweets

Either make a search of the available Twitter database (approx. last 7 days of tweets)Oruse the Streaming API to capture Tweets as they occur (only a 1% sample is made available)

In either method, you should have no expectation of completeness

Page 23: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Search Twitter - R code

1. Uses the TwitteR library2. Set up authorisation using the 4 keys3. Search for the keyword ‘brexit’ in separate

searches over a 4 day period4. Save the text of the returned tweets into a data

frame5. Extract the hashtags from the Tweets6. Create a word cloud from the hashtag values

Page 24: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Stream Twitter – Python code

1. Uses the Tweepy package

2. Set up authorisation using the 4 keys

3. Arrange to have the returned Tweets written to

a file

4. Arrange to print the Name, Time and Text from

the Tweet on the screen

Page 25: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

What to do when you want to scale up

• Scheduled requesto cron or task scheduler, looping code with

timer delay• More intelligence about the requests to make

(based on previous responses)oMore complex coding

• Persist the returned data for future analysisoWrite responses to a file or a database

Page 26: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

API books

Page 27: What are APIs? - UK Data Service · What are APIs? The webinar will begin at 3pm • You now have a menu in the top right corner of your screen. • The red button with a white arrow

Questions

Peter Smyth

[email protected]

ukdataservice.ac.uk/help/

Subscribe to the UK Data Service news list at https://www.jiscmail.ac.uk/cgi-bin/webadmin?A0=UKDATASERVICE

Follow us on Twitter https://twitter.com/UKDataServiceor Facebook https://www.facebook.com/UKDataService