-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathREADME.Rmd
181 lines (131 loc) · 5.19 KB
/
README.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
request
=======
```{r echo=FALSE}
hook_output <- knitr::knit_hooks$get("output")
knitr::knit_hooks$set(output = function(x, options) {
lines <- options$output.lines
if (is.null(lines)) {
return(hook_output(x, options)) # pass to default hook
}
x <- unlist(strsplit(x, "\n"))
more <- "..."
if (length(lines) == 1) { # first n lines
if (length(x) > lines) {
# truncate the output, but add ....
x <- c(head(x, lines), more)
}
} else {
x <- c(if (abs(lines[1])>1) more else NULL,
x[lines],
if (length(x)>lines[abs(length(lines))]) more else NULL
)
}
# paste these lines together
x <- paste(c(x, ""), collapse = "\n")
hook_output(x, options)
})
knitr::opts_chunk$set(
warning = FALSE,
message = FALSE,
collapse = TRUE,
comment = "#>"
)
```
[](https://cranchecks.info/pkgs/request)
[](https://travis-ci.org/sckott/request)
[](https://codecov.io/github/sckott/request?branch=master)
[](https://github.com/metacran/cranlogs.app)
[](https://cran.r-project.org/package=request)
`request` is DSL for http requests for R, and is inspired by the CLI tool [httpie](https://github.com/jakubroztocil/httpie).
`request` is built on `httr`, though may allow using the R packages `RCurl` or `curl` as optional backends at some point.
I gave a poster at User2016, its in my [talks repo](https://github.com/sckott/talks/blob/gh-pages/user2016/request.pdf)
## Philosophy
* The web is increasingly a JSON world, so we assume `applications/json` by default, but give back other types if not
* The workflow follows logically, or at least should, from, _hey, I got this url_, to _i need to add some options_, to _execute request_
* Whenever possible, we transform output to data.frame's - facilitating downstream manipulation via `dplyr`, etc.
* We do `GET` requests by default. Specify a different type if you don't want `GET`
* You can use non-standard evaluation to easily pass in query parameters without worrying about `&`'s, URL escaping, etc. (see `api_query()`)
* Same for body params (see `api_body()`)
All of the defaults just mentioned can be changed.
## Auto execute http requests with pipes
When using pipes, we autodetect that a pipe is being used within the function calls, and automatically do the appropriate http request on the last piped function call. When you call a function without using pipes, you have to use the `http()` function explicitly to make the http request.
## low level http
Low level access is available with `http_client()`, which returns an `R6` class with various methods for inspecting http request results.
## Peek at a request
The function `peep()` let's you peek at a request without performing the http request.
## Install
From CRAN
```{r eval=FALSE}
install.packages("request")
```
Development version from GitHub
```{r eval=FALSE}
remotes::install_github("sckott/request")
```
```{r}
library("request")
```
## NSE and SE
NSE is supported
```{r eval=FALSE}
api('https://api.github.com/') %>%
api_path(repos, ropensci, rgbif, issues)
```
as well as SE
```{r eval=FALSE}
api('https://api.github.com/') %>%
api_path_('repos', 'ropensci', 'rgbif', 'issues')
```
## Building API routes
Works with full or partial URLs
```{r}
api('https://api.github.com/')
api('http://api.gbif.org/v1')
api('api.gbif.org/v1')
```
Works with ports, full or partial
```{r}
api('http://localhost:9200')
api('localhost:9200')
api(':9200')
api('9200')
api('9200/stuff')
```
## Make HTTP requests
The above examples with `api()` are not passed through a pipe, so only define a URL, but don't do an HTTP request. To make an HTTP request, you can either pipe a url or partial url to e.g., `api()`, or call `http()` at the end of a string of function calls:
```{r output.lines = 1:10}
'https://api.github.com/' %>% api()
```
Or
```{r output.lines = 1:10}
api('https://api.github.com/') %>% http()
```
`http()` is called at the end of a chain of piped commands, so no need to invoke it. However, you can if you like.
## Templating
```{r}
repo_info <- list(username = 'craigcitro', repo = 'r-travis')
api('https://api.github.com/') %>%
api_template(template = 'repos/{{username}}/{{repo}}/issues', data = repo_info) %>%
peep
```
## Set paths
`api_path()` adds paths to the base URL (see `api_query()`) for query parameters
```{r}
api('https://api.github.com/') %>%
api_path(repos, ropensci, rgbif, issues) %>%
peep
```
## Query
```{r}
api("http://api.plos.org/search") %>%
api_query(q = ecology, wt = json, fl = 'id,journal') %>%
peep
```
## ToDo
See [the issues](https://github.com/sckott/request/issues) for discussion of these
* Paging
* Retry
* Rate limit
## Meta
* Please note that this project is released with a [Contributor Code of Conduct][coc]. By participating in this project you agree to abide by its terms.
[coc]: https://github.com/sckott/request/blob/master/.github/CODE_OF_CONDUCT.md