rapiclient

Dynamic Open API (Swagger) Client for R

https://github.com/bergant/rapiclient

Science Score: 36.0%

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

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    1 of 5 committers (20.0%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (13.3%) to scientific vocabulary

Keywords

api openapi r swagger

Keywords from Contributors

bioconductor-package genomics u24ca289073
Last synced: 6 months ago · JSON representation

Repository

Dynamic Open API (Swagger) Client for R

Basic Info
  • Host: GitHub
  • Owner: bergant
  • License: other
  • Language: R
  • Default Branch: master
  • Size: 207 KB
Statistics
  • Stars: 67
  • Watchers: 7
  • Forks: 18
  • Open Issues: 9
  • Releases: 7
Topics
api openapi r swagger
Created over 9 years ago · Last pushed about 1 year ago
Metadata Files
Readme Changelog License

readme.Rmd

---
output: 
  html_document: 
    keep_md: yes
---

# __rapiclient__

[![Build Status](https://travis-ci.org/bergant/rapiclient.svg?branch=master)](https://travis-ci.org/bergant/rapiclient)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/rapiclient)](http://cran.r-project.org/package=rapiclient)




```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, comment = "#", collapse = TRUE)
```

Access services specified in [OpenAPI](https://openapis.org) (formerly Swagger) format.

**rapiclient** is not a code generator. Client is generated dynamically as 
a list of R functions.


## Installation
Install the current released version from CRAN:

```{r eval=FALSE}
install.packages("rapiclient")
```

Or get the current development version from github:

```{r eval=FALSE}
# install.packages("devtools")
devtools::install_github("bergant/rapiclient")
```


## Usage

### Prepare API Operations and Schemas

```{r}
library(rapiclient)
```

This example uses the [sample petstore service](http://petstore.swagger.io)
and its OpenAPI definition at (http://petstore.swagger.io/v2/swagger.json).

```{r api, cache=TRUE}
pet_api <- get_api(url = "http://petstore.swagger.io/v2/swagger.json")
operations <- get_operations(pet_api)
schemas <- get_schemas(pet_api)
```

Function `get_operations` returns a **list of functions**. 
Each function takes named arguments, converts the values to JSON 
according to API operation definition and performs a service call which
returns a http response object.

Function `get_schemas` returns a list of functions where each function returns 
an object according to the related schema in the API.


### Calling Service Operations

#### Find a Pet
Let's try to find a pet with Id = 42 (see operation [definition](http://petstore.swagger.io/#!/pet/getPetById)):
```{r getPetById, cache=TRUE}
res <- operations$getPetById(petId = 42)

res$status_code
str(httr::content(res))
```

#### New Pet
OK, there is no pet with Id = 42, so let's [add a pet](http://petstore.swagger.io/#!/pet/addPet):

```{r addPet, cache=TRUE}
res <- 
  operations$addPet(
    id = 42,
    category = schemas$Category(
      id = 1,
      name = "Undefined"
    ),
    name = "Agrajag",
    photoUrls = list(),
    tags = list(
      schemas$Tag(id = 1, name = "Wild"),
      schemas$Tag(id = 2, name = "Furry")
    ),
    status = "available"
  )

res$status_code
```

Check:

```{r findPet2, cache=TRUE}
res <- operations$getPetById(petId = 42)

res$status_code
str(httr::content(res))
```

### Response Handlers

If all operations are handled identically (e.g. reading content or stop 
on http exception), it is more convenient to create the API functions
with this functionality. `get_operations` accepts an optional handler
function which must accept a httr response object as an argument.

Some handler functions are already predefined. For example `content_or_stop`
returns a content or throws an exception.

```{r, cache = TRUE}
operations <- get_operations(pet_api, handle_response = content_or_stop)

pet_data <- operations$getPetById(42)
str(pet_data)
```

Note that you can always trace the communication between client and server with `httr::with_verbose`:

```{r, eval=FALSE, echo = TRUE}
httr::with_verbose({
  # get pet data
  pet_data <- operations$getPetById(42)
  # delete a pet entry
  operations$deletePet(api_key = "special-key", petId = 42)
})
```

```{r, cache=TRUE, eval = TRUE, echo=FALSE}
cat(capture.output(type = "message",
  
  httr::with_verbose({
    # get pet data
    pet_data <- operations$getPetById(42)
    # delete a pet entry
    operations$deletePet(api_key = "special-key", petId = 42)
  })

))
```



### Help on API Operations

The good news is that autocomplete in RStudio editor works fine with dynamically created functions. The bad news: R documentation is not available 
with `help` or `?`. To lookup the operation definition
just print the function (write it down without parenthesis):

Let's get help for `getPetById`:
```{r print}
operations$getPetById
```

More complicated `addPet` also describes the nested schemas:

```{r print2}
operations$addPet
```

For more detailed operation description use the operation's "definition" attribute :

```{r operation_definition}
definition <- attr(operations$getPetById, "definition")
str(definition)
```


### Using Additional Headers

Set additional http headers at the time of creating operation functions
in `get_operations` function.

The following example uses New York Times API from [developer.nytimes.com](http://developer.nytimes.com/)
with API key authentication.


```{r nyt_api_test, cache=TRUE}
nyt_api <- get_api("http://developer.nytimes.com/top_stories_v2.json/swagger.json")

nyt_operations <- 
  get_operations( nyt_api, .headers = c("api-key" = Sys.getenv("NYT_API_KEY")))

res <- nyt_operations$Top_Stories(section = "science", format = "json")

res$status_code
 
content <- httr::content(res)
str(content, max.level = 1)

```




Owner

  • Name: Darko Bergant
  • Login: bergant
  • Kind: user

GitHub Events

Total
  • Watch event: 3
  • Issue comment event: 3
  • Pull request event: 3
  • Create event: 1
Last Year
  • Watch event: 3
  • Issue comment event: 3
  • Pull request event: 3
  • Create event: 1

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 103
  • Total Committers: 5
  • Avg Commits per committer: 20.6
  • Development Distribution Score (DDS): 0.515
Past Year
  • Commits: 43
  • Committers: 1
  • Avg Commits per committer: 43.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
LiNk-NY m****s@s****u 50
Martin Morgan m****n@r****g 24
Bergant d****t@a****i 24
Sean Davis s****i@g****m 4
Alexandru Mahmoud m****u@g****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 7 months ago

All Time
  • Total issues: 15
  • Total pull requests: 15
  • Average time to close issues: almost 2 years
  • Average time to close pull requests: 3 months
  • Total issue authors: 13
  • Total pull request authors: 7
  • Average comments per issue: 2.0
  • Average comments per pull request: 0.67
  • Merged pull requests: 10
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 2
  • Pull requests: 4
  • Average time to close issues: 7 days
  • Average time to close pull requests: 4 days
  • Issue authors: 2
  • Pull request authors: 4
  • Average comments per issue: 1.5
  • Average comments per pull request: 1.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • slodge (2)
  • seandavi (2)
  • LiNk-NY (1)
  • WouterdH (1)
  • ataiprojects (1)
  • jws-kantar (1)
  • schaffman5 (1)
  • ttriche (1)
  • aedin (1)
  • jcolomb (1)
  • the-mad-statter (1)
  • ronlo04 (1)
  • reijmer (1)
Pull Request Authors
  • mtmorgan (4)
  • LiNk-NY (4)
  • almahmoud (4)
  • seandavi (2)
  • stevencarlislewalker (2)
  • mustberuss (1)
  • slodge (1)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 2
  • Total downloads:
    • cran 934 last-month
  • Total docker downloads: 136,599
  • Total dependent packages: 0
    (may contain duplicates)
  • Total dependent repositories: 6
    (may contain duplicates)
  • Total versions: 7
  • Total maintainers: 1
cran.r-project.org: rapiclient

Dynamic OpenAPI/Swagger Client

  • Versions: 6
  • Dependent Packages: 0
  • Dependent Repositories: 6
  • Downloads: 934 Last month
  • Docker Downloads: 136,599
Rankings
Forks count: 4.4%
Stargazers count: 5.7%
Dependent repos count: 12.0%
Average: 15.6%
Docker downloads count: 20.1%
Downloads: 22.4%
Dependent packages count: 28.8%
Maintainers (1)
Last synced: 6 months ago
conda-forge.org: r-rapiclient
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent repos count: 34.0%
Stargazers count: 35.5%
Forks count: 36.1%
Average: 39.2%
Dependent packages count: 51.2%
Last synced: 6 months ago

Dependencies

DESCRIPTION cran
  • R >= 3.3 depends
  • httr * imports
  • jsonlite * imports
  • yaml * imports
  • testthat * suggests