-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code and slides from web APIs webinar
- Loading branch information
Showing
13 changed files
with
5,006 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
library(httr) | ||
|
||
# OAuth 2 example, lifted from https://github.com/hadley/httr/blob/master/demo/oauth2-github.r | ||
|
||
# 1. Find OAuth settings for github: | ||
# http://developer.github.com/v3/oauth/ | ||
oauth_endpoints("github") | ||
|
||
# 2. To make your own application, register at at | ||
# https://github.com/settings/applications. Use any URL for the homepage URL | ||
# (http://github.com is fine) and http://localhost:1410 as the callback url | ||
# | ||
# IRL, you'd want to set the key and secret as environment variables to keep them out of the code. | ||
ghapp <- oauth_app("github", | ||
key = "56b637a5baffac62cad9", | ||
secret = "8e107541ae1791259e9987d544ca568633da2ebf" | ||
) | ||
# 3. Get OAuth credentials | ||
github_token <- oauth2.0_token(oauth_endpoints("github"), ghapp, cache = TRUE) | ||
|
||
# 4. Use API | ||
gtoken <- config(token = github_token) | ||
req <- GET("https://api.github.com/rate_limit", gtoken) | ||
stop_for_status(req) | ||
rate_limit <- content(req) | ||
rate_limit$resources$core$limit | ||
|
||
# See API documentation for all resources, examples |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4,204 changes: 4,204 additions & 0 deletions
4,204
40-web-apis-with-httr/resources/SupportTickets.html
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.gdbar img { | ||
width: 250px !important; | ||
height: 88px !important; | ||
margin: 8px 8px; | ||
} | ||
|
||
.gdbar { | ||
width: 300px !important; | ||
height: 100px !important; | ||
} | ||
|
||
slides > slide:not(.nobackground):before { | ||
width: 100px; | ||
height: 35px; | ||
background-size: 100px 35px; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
library(httr) | ||
library(jsonlite) | ||
library(magrittr) | ||
|
||
# Goal: Get the data for the planet Alderaan | ||
# verb (method) = GET | ||
# URL (endpoint) = http://swapi.co/api/planets/ | ||
# parameter = search | ||
|
||
alderaan <- GET("http://swapi.co/api/planets/?search=alderaan") | ||
# Same call in a different format | ||
alderaan <- GET("http://swapi.co/api/planets/", query = list(search = "alderaan")) | ||
|
||
names(alderaan) | ||
alderaan$status_code | ||
alderaan$headers$`content-type` | ||
|
||
# Get the content of the response | ||
text_content <- content(alderaan, "text", encoding = "UTF-8") | ||
text_content | ||
|
||
# Parse with httr | ||
parsed_content <- content(alderaan, "parsed") | ||
names(parsed_content) | ||
parsed_content$count | ||
str(parsed_content$results) | ||
parsed_content$results[[1]]$name | ||
parsed_content$results[[1]]$terrain | ||
|
||
# Parse with jsonlite | ||
json_content <- text_content %>% fromJSON | ||
json_content | ||
planetary_data <- json_content$results | ||
names(planetary_data) | ||
planetary_data$name | ||
planetary_data$terrain | ||
|
||
# ------------------------------- | ||
|
||
# Helper function | ||
json_parse <- function(req) { | ||
text <- content(req, "text", encoding = "UTF-8") | ||
if (identical(text, "")) warning("No output to parse.") | ||
fromJSON(text) | ||
} | ||
|
||
# List results | ||
planets <- GET("http://swapi.co/api/planets") %>% stop_for_status() | ||
json_planets <- json_parse(planets) | ||
|
||
# The response includes metadata as well as results | ||
names(json_planets) | ||
json_planets$count | ||
length(json_planets$results$name) | ||
json_planets$`next` | ||
|
||
swapi_planets <- json_planets$results | ||
swapi_planets$name | ||
|
||
# Get the next page of results based on the content of the `next` field | ||
next_page <- GET(json_planets$`next`) %>% stop_for_status() | ||
|
||
# Use a function to parse the results | ||
parsed_next_page <- json_parse(next_page) | ||
parsed_next_page$results$name | ||
|
||
# If the API results come back paged like this, you can write a loop to follow the next URL | ||
# until the there are no more pages, and rbind all the data into a single dataframe. | ||
|
||
# Grab data on all of the Star Wars planets | ||
planets <- GET("http://swapi.co/api/planets") %>% | ||
stop_for_status() %>% | ||
json_parse | ||
swapi_planets <- planets$results | ||
|
||
next_page <- planets$`next` | ||
while (!is.null(next_page)) { | ||
more_planets <- GET(next_page) %>% | ||
stop_for_status() %>% | ||
json_parse | ||
swapi_planets <- rbind(swapi_planets, more_planets$results) | ||
next_page <- more_planets$`next` | ||
} | ||
|
||
length(swapi_planets$name) | ||
swapi_planets$name | ||
|
||
# In real life, you'd also want to handle any errors, headers, proxy, rate limits, etc. as needed. | ||
help(package = httr) | ||
|
||
# Someone wrote a package for swapi: https://github.com/Ironholds/rwars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
library(httr) | ||
|
||
# OAuth 1 example, mostly lifted from https://github.com/hadley/httr/blob/master/demo/oauth1-twitter.r | ||
|
||
# 1. Find OAuth settings for twitter: https://dev.twitter.com/docs/auth/oauth | ||
oauth_endpoints("twitter") | ||
# use oauth_endpoint() if you are accessing an API not covered in the convenience method oauth_endpoints | ||
|
||
# 2. Register an application at https://apps.twitter.com/ | ||
# Make sure to set callback url to "http://127.0.0.1:1410/" | ||
# | ||
# IRL, you'd want to set the key and secret as environment variables to keep them out of the code. | ||
myapp <- oauth_app("twitter", | ||
key = "TYrWFPkFAkn4G5BbkWINYw", | ||
secret = "qjOkmKYU9kWfUFWmekJuu5tztE9aEfLbt26WlhZL8" | ||
) | ||
|
||
# 3. Get OAuth credentials | ||
twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), myapp, cache = TRUE) | ||
|
||
# 4. Use API | ||
req <- GET("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=rstudiotips&count=10", | ||
config(token = twitter_token)) | ||
stop_for_status(req) | ||
rstudio_tips <- content(req) | ||
rstudio_tips[[1]]$text | ||
|
||
# See API documentation for all resources, examples |