Slide 1

Slide 1 text

ACCESSING APIS USING R Manish Kukreja https://datamak.ml #ResBaz2019 https://resbaz.auckland.ac.nz/schedule/#session-018

Slide 2

Slide 2 text

BACKGROUND ON APIS • API stands for Application Programming Interface • GUI or CLI is for human-computer interaction • API is for program-program interaction • Some examples • Get data and plot it • Add a node to a computing cluster • Recognize faces as soon as an image is uploaded • APIs used on the web are mostly HTTP RESTful APIs or REST APIs

Slide 3

Slide 3 text

REST API path to the endpoint http://example.com/api/v2/fetch/data URL to a domain

Slide 4

Slide 4 text

HTTP VERBS • POST – used to create a record, data is passed using JSON or XML • GET – read single or list of records, response can be JSON or XML • PUT – update/replace single or entire list of records • PATCH – update/modify single or entire list of records • DELETE – delete the whole collection

Slide 5

Slide 5 text

STEPS TO ACCESS API 1. Obtain the key for using the web service 2. Find the URL and endpoint path of the API 3. Convert input data to JSON format using toJSON() in the jsonlite package [optional] 4. Send input data to the API service using the appropriate HTTP VERB function in the httr package. Include API key in the function call. 5. Extract your results from the API response using the fromJSON() function

Slide 6

Slide 6 text

JOIN THE WORKSPACE IN RSTUDIO.CLOUD https://tiny.cc/resbaz-api

Slide 7

Slide 7 text

JOIN THE WORKSPACE IN RSTUDIO.CLOUD

Slide 8

Slide 8 text

JOIN THE WORKSPACE IN RSTUDIO.CLOUD

Slide 9

Slide 9 text

JOIN THE WORKSPACE IN RSTUDIO.CLOUD

Slide 10

Slide 10 text

JOIN THE WORKSPACE IN RSTUDIO.CLOUD

Slide 11

Slide 11 text

JOIN THE WORKSPACE IN RSTUDIO.CLOUD

Slide 12

Slide 12 text

1. OBTAIN THE SECRET KEY https://darksky.net/dev/register

Slide 13

Slide 13 text

2. FIND URL AND ENDPOINT PATH https://darksky.net/dev/docs#forecast-request

Slide 14

Slide 14 text

3. CONVERT INPUT DATA TO JSON SKIPPED

Slide 15

Slide 15 text

4. SEND REQUEST •Send GET request •Check response

Slide 16

Slide 16 text

5. EXTRACT RESULTS FROM RESPONSE •Use httr::content() to extract data from response object •Transform it for easy manipulation •Visualize, merge or analyse it

Slide 17

Slide 17 text

WORKSHOP CODE • Darksky weather forecast API notebook • https://datamak.ml/projects/data-analysis/Darksky-API • Microsoft sentiment analysis API notebook • https://datamak.ml/projects/data-analysis/Sentiment-API

Slide 18

Slide 18 text

THANK YOU