forked from lmullen/dh-r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apis.Rmd
43 lines (27 loc) · 1.33 KB
/
apis.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
---
title: "Web APIs (Application Programming Interfaces)"
---
## Downloading Files with CURL
## Accessing APIs
```{r load_libraries}
library(httr)
```
## Httpbin
As you are learning to work with APIs, and in particular if you are writing a package or function that wraps an API, you may find the service [httpbin](http://httpbin.org/) to be useful. That service provides many sample endpoints so that you can experiment with APIs. These endpoints will in most cases return the data that you posted to them, so you can see exactly what the service sees, not just what you think it should see.^[I found this service through the vignette for the httr package.]
For example, using the `ip` endpoint for httpbin (which you can [visit in your
browser](http://httpbin.org/ip) to see the JSON that is returned), we can write a simple function that discovers your IP address.
```{r what_is_my_ip}
what_is_my_ip <- function() {
require(httr)
r <- GET("http://httpbin.org/ip")
warn_for_status(r) # Print a warning if request was unsuccessful
content(r)$origin
}
what_is_my_ip()
```
## JSON
## XML
See the section on [scraping websites](scraping.html).
## APIs of interest to historians
The Digital Public Libary of America (DPLA) has a very [useful and well documented API](http://dp.la/info/developers/codex/).
## Packages that provide API wrappers