{"id":14067103,"url":"https://github.com/bergant/rapiclient","last_synced_at":"2025-04-12T19:43:24.599Z","repository":{"id":8726934,"uuid":"59492122","full_name":"bergant/rapiclient","owner":"bergant","description":"Dynamic Open API (Swagger) Client for R","archived":false,"fork":false,"pushed_at":"2024-10-09T18:26:25.000Z","size":217,"stargazers_count":66,"open_issues_count":10,"forks_count":18,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-30T00:33:03.604Z","etag":null,"topics":["api","openapi","r","swagger"],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bergant.png","metadata":{"files":{"readme":"readme.Rmd","changelog":"NEWS.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-23T15:00:21.000Z","updated_at":"2024-10-28T15:01:03.000Z","dependencies_parsed_at":"2024-05-10T19:30:15.354Z","dependency_job_id":"2f8a8d6d-d89f-4da6-b6da-8379a78a3fc5","html_url":"https://github.com/bergant/rapiclient","commit_stats":{"total_commits":103,"total_committers":10,"mean_commits":10.3,"dds":0.7669902912621359,"last_synced_commit":"8969fbf2fb0a4ef1718e6d0431e23df9f54b4f19"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bergant%2Frapiclient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bergant%2Frapiclient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bergant%2Frapiclient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bergant%2Frapiclient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bergant","download_url":"https://codeload.github.com/bergant/rapiclient/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625478,"owners_count":21135512,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","openapi","r","swagger"],"created_at":"2024-08-13T07:05:26.103Z","updated_at":"2025-04-12T19:43:24.572Z","avatar_url":"https://github.com/bergant.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"---\noutput: \n  html_document: \n    keep_md: yes\n---\n\n# __rapiclient__\n\n[![Build Status](https://travis-ci.org/bergant/rapiclient.svg?branch=master)](https://travis-ci.org/bergant/rapiclient)\n[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/rapiclient)](http://cran.r-project.org/package=rapiclient)\n\n\n\u003cimg align=\"right\" src=\"img/rapiclient_ani.gif\"/\u003e\n\n```{r setup, include=FALSE}\nknitr::opts_chunk$set(echo = TRUE, comment = \"#\", collapse = TRUE)\n```\n\nAccess services specified in [OpenAPI](https://openapis.org) (formerly Swagger) format.\n\n**rapiclient** is not a code generator. Client is generated dynamically as \na list of R functions.\n\n\n## Installation\nInstall the current released version from CRAN:\n\n```{r eval=FALSE}\ninstall.packages(\"rapiclient\")\n```\n\nOr get the current development version from github:\n\n```{r eval=FALSE}\n# install.packages(\"devtools\")\ndevtools::install_github(\"bergant/rapiclient\")\n```\n\n\n## Usage\n\n### Prepare API Operations and Schemas\n\n```{r}\nlibrary(rapiclient)\n```\n\nThis example uses the [sample petstore service](http://petstore.swagger.io)\nand its OpenAPI definition at (http://petstore.swagger.io/v2/swagger.json).\n\n```{r api, cache=TRUE}\npet_api \u003c- get_api(url = \"http://petstore.swagger.io/v2/swagger.json\")\noperations \u003c- get_operations(pet_api)\nschemas \u003c- get_schemas(pet_api)\n```\n\nFunction `get_operations` returns a **list of functions**. \nEach function takes named arguments, converts the values to JSON \naccording to API operation definition and performs a service call which\nreturns a http response object.\n\nFunction `get_schemas` returns a list of functions where each function returns \nan object according to the related schema in the API.\n\n\n### Calling Service Operations\n\n#### Find a Pet\nLet's try to find a pet with Id = 42 (see operation [definition](http://petstore.swagger.io/#!/pet/getPetById)):\n```{r getPetById, cache=TRUE}\nres \u003c- operations$getPetById(petId = 42)\n\nres$status_code\nstr(httr::content(res))\n```\n\n#### New Pet\nOK, there is no pet with Id = 42, so let's [add a pet](http://petstore.swagger.io/#!/pet/addPet):\n\n```{r addPet, cache=TRUE}\nres \u003c- \n  operations$addPet(\n    id = 42,\n    category = schemas$Category(\n      id = 1,\n      name = \"Undefined\"\n    ),\n    name = \"Agrajag\",\n    photoUrls = list(),\n    tags = list(\n      schemas$Tag(id = 1, name = \"Wild\"),\n      schemas$Tag(id = 2, name = \"Furry\")\n    ),\n    status = \"available\"\n  )\n\nres$status_code\n```\n\nCheck:\n\n```{r findPet2, cache=TRUE}\nres \u003c- operations$getPetById(petId = 42)\n\nres$status_code\nstr(httr::content(res))\n```\n\n### Response Handlers\n\nIf all operations are handled identically (e.g. reading content or stop \non http exception), it is more convenient to create the API functions\nwith this functionality. `get_operations` accepts an optional handler\nfunction which must accept a httr response object as an argument.\n\nSome handler functions are already predefined. For example `content_or_stop`\nreturns a content or throws an exception.\n\n```{r, cache = TRUE}\noperations \u003c- get_operations(pet_api, handle_response = content_or_stop)\n\npet_data \u003c- operations$getPetById(42)\nstr(pet_data)\n```\n\nNote that you can always trace the communication between client and server with `httr::with_verbose`:\n\n```{r, eval=FALSE, echo = TRUE}\nhttr::with_verbose({\n  # get pet data\n  pet_data \u003c- operations$getPetById(42)\n  # delete a pet entry\n  operations$deletePet(api_key = \"special-key\", petId = 42)\n})\n```\n\n```{r, cache=TRUE, eval = TRUE, echo=FALSE}\ncat(capture.output(type = \"message\",\n  \n  httr::with_verbose({\n    # get pet data\n    pet_data \u003c- operations$getPetById(42)\n    # delete a pet entry\n    operations$deletePet(api_key = \"special-key\", petId = 42)\n  })\n\n))\n```\n\n\n\n### Help on API Operations\n\nThe good news is that autocomplete in RStudio editor works fine with dynamically created functions. The bad news: R documentation is not available \nwith `help` or `?`. To lookup the operation definition\njust print the function (write it down without parenthesis):\n\nLet's get help for `getPetById`:\n```{r print}\noperations$getPetById\n```\n\nMore complicated `addPet` also describes the nested schemas:\n\n```{r print2}\noperations$addPet\n```\n\nFor more detailed operation description use the operation's \"definition\" attribute :\n\n```{r operation_definition}\ndefinition \u003c- attr(operations$getPetById, \"definition\")\nstr(definition)\n```\n\n\n### Using Additional Headers\n\nSet additional http headers at the time of creating operation functions\nin `get_operations` function.\n\nThe following example uses New York Times API from [developer.nytimes.com](http://developer.nytimes.com/)\nwith API key authentication.\n\n\n```{r nyt_api_test, cache=TRUE}\nnyt_api \u003c- get_api(\"http://developer.nytimes.com/top_stories_v2.json/swagger.json\")\n\nnyt_operations \u003c- \n  get_operations( nyt_api, .headers = c(\"api-key\" = Sys.getenv(\"NYT_API_KEY\")))\n\nres \u003c- nyt_operations$Top_Stories(section = \"science\", format = \"json\")\n\nres$status_code\n \ncontent \u003c- httr::content(res)\nstr(content, max.level = 1)\n\n```\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbergant%2Frapiclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbergant%2Frapiclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbergant%2Frapiclient/lists"}