request

http requests DSL for R

https://github.com/sckott/request

Science Score: 10.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
    Links to: plos.org
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.7%) to scientific vocabulary

Keywords

api curl curl-library http r r-stats
Last synced: 6 months ago · JSON representation

Repository

http requests DSL for R

Basic Info
  • Host: GitHub
  • Owner: sckott
  • License: other
  • Language: R
  • Default Branch: master
  • Homepage:
  • Size: 157 KB
Statistics
  • Stars: 36
  • Watchers: 9
  • Forks: 3
  • Open Issues: 11
  • Releases: 0
Topics
api curl curl-library http r r-stats
Created almost 11 years ago · Last pushed over 5 years ago
Metadata Files
Readme License Code of conduct

README.Rmd

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 = "#>"
)
```

[![cran checks](https://cranchecks.info/badges/worst/request)](https://cranchecks.info/pkgs/request)
[![Build Status](https://travis-ci.org/sckott/request.svg)](https://travis-ci.org/sckott/request)
[![codecov.io](https://codecov.io/github/sckott/request/coverage.svg?branch=master)](https://codecov.io/github/sckott/request?branch=master)
[![rstudio mirror downloads](http://cranlogs.r-pkg.org/badges/request?color=F3B1FF)](https://github.com/metacran/cranlogs.app)
[![cran version](http://www.r-pkg.org/badges/version/request)](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

Owner

  • Name: Scott Chamberlain
  • Login: sckott
  • Kind: user
  • Location: Oregon
  • Company: Fred Hutch Data Science Lab/Office of the Chief Data Officer

Software Engineer @ Fred Hutch

GitHub Events

Total
Last Year

Committers

Last synced: 8 months ago

All Time
  • Total Commits: 138
  • Total Committers: 1
  • Avg Commits per committer: 138.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Scott Chamberlain m****s@g****m 138

Issues and Pull Requests

Last synced: 8 months ago

All Time
  • Total issues: 22
  • Total pull requests: 0
  • Average time to close issues: 9 months
  • Average time to close pull requests: N/A
  • Total issue authors: 4
  • Total pull request authors: 0
  • Average comments per issue: 1.18
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • sckott (19)
  • ngmacha (1)
  • yina (1)
  • tobefreeman (1)
Pull Request Authors
Top Labels
Issue Labels
bug (2)
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 176 last-month
  • Total docker downloads: 43,390
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 1
  • Total maintainers: 1
cran.r-project.org: request

High Level 'HTTP' Client

  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 176 Last month
  • Docker Downloads: 43,390
Rankings
Stargazers count: 8.5%
Forks count: 14.9%
Average: 27.3%
Dependent packages count: 29.8%
Dependent repos count: 35.5%
Downloads: 47.9%
Maintainers (1)
Last synced: 6 months ago

Dependencies

DESCRIPTION cran
  • httr >= 1.2.0 depends
  • R6 >= 2.2.0 imports
  • curl >= 2.2 imports
  • digest * imports
  • jsonlite >= 1.1 imports
  • lazyeval >= 0.2.0 imports
  • magrittr >= 1.5 imports
  • methods * imports
  • rappdirs * imports
  • stats * imports
  • tibble >= 1.2 imports
  • utils * imports
  • whisker >= 0.3 imports
  • data.table * suggests
  • testthat * suggests