{"id":14066435,"url":"https://github.com/edgararuiz/callthat","last_synced_at":"2025-10-07T01:54:15.543Z","repository":{"id":50083463,"uuid":"368559498","full_name":"edgararuiz/callthat","owner":"edgararuiz","description":"Lets you test {plumber} API endpoints within {testthat}","archived":false,"fork":false,"pushed_at":"2021-06-04T20:04:58.000Z","size":263,"stargazers_count":13,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T12:07:15.701Z","etag":null,"topics":["plumber","r","restapi","testing"],"latest_commit_sha":null,"homepage":"https://edgararuiz.github.io/callthat/","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/edgararuiz.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-18T14:25:31.000Z","updated_at":"2024-06-24T04:49:56.000Z","dependencies_parsed_at":"2022-09-23T07:10:33.078Z","dependency_job_id":null,"html_url":"https://github.com/edgararuiz/callthat","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/edgararuiz/callthat","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgararuiz%2Fcallthat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgararuiz%2Fcallthat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgararuiz%2Fcallthat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgararuiz%2Fcallthat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edgararuiz","download_url":"https://codeload.github.com/edgararuiz/callthat/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgararuiz%2Fcallthat/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278708028,"owners_count":26031932,"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","status":"online","status_checked_at":"2025-10-06T02:00:05.630Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["plumber","r","restapi","testing"],"created_at":"2024-08-13T07:05:05.856Z","updated_at":"2025-10-07T01:54:15.528Z","avatar_url":"https://github.com/edgararuiz.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\", \n  eval = FALSE\n)\n```\n\n# callthat \u003cimg src='man/figures/logo.png' align=\"right\" height=\"138\" /\u003e\n\n\n\u003c!-- badges: start --\u003e\n[![R-CMD-check](https://github.com/edgararuiz/callthat/workflows/R-CMD-check/badge.svg)](https://github.com/edgararuiz/callthat/actions)\n[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)\n[![CRAN status](https://www.r-pkg.org/badges/version/callthat)](https://CRAN.R-project.org/package=callthat)\n[![Codecov test coverage](https://codecov.io/gh/edgararuiz/callthat/branch/master/graph/badge.svg)](https://codecov.io/gh/edgararuiz/callthat?branch=master)\n\u003c!-- badges: end --\u003e\n\n`callthat` is meant for `plumber` API developers who plan to distribute their APIs inside an R package.  This package enables the testing of the API's endpoints within the `testthat` framework.  This allows the R Checks to also confirm that the endpoints still behave as expected.  \n\nThe ultimate goal of `callthat` is to ensure that the package's automated testing catches issues with the API even if the developer did not perform unit testing. \n\n## Installation\n\nThe development version from [GitHub](https://github.com/) with:\n\n``` r\n# install.packages(\"devtools\")\ndevtools::install_github(\"edgararuiz/callthat\")\n```\n\n## Usage\n\n### `plumber` API in your package\n\nPlace your API in the `inst/plumber` folder of your package.  That way  `plumber::available_apis(\"MyPakcageName\")` can locate the APIs, after your package is installed.\n\nIn `MyPakcageName/inst/plumber/sample-api`:\n\n```{r, eval = FALSE}\nlibrary(plumber)\n\n#* Runs a prediction of the Weight\n#* @get /predict\nfunction(weight) {\n  wtg \u003c- as.numeric(weight)\n  model \u003c- stats::lm(mpg ~ wt, data = datasets::mtcars)\n  stats::predict(model, newdata = data.frame(wt = wtg))\n}\n```\n\n### Inside the `testthat` folder\n\n```{r, eval = TRUE}\nlibrary(testthat)\nlibrary(callthat)\nlibrary(httr)\n```\n\nCreate the test using the same name as the name of the API.  Add the prefix `test-plumber`.  \n\nIn `MyPakcageName/tests/testthat/test-plumber-sample-api.R`:\n\n```{r, eval = FALSE}\ntest_that(\"Show how to use test\", {\n  expect_silent({\n    \n    # ------------- Start plumber API ------------------- \n    local_api \u003c- call_that_plumber_start(\n      system.file(\"plumber/sample-api\", package = \"MyPackageName\")\n      )\n    \n    # ------------- Start test session ------------------ \n    api_session \u003c- call_that_session_start(local_api)\n  })\n  expect_s3_class(\n    \n    # ---------------- Make API call --------------------\n    get_predict \u003c- call_that_api_get(\n      api_session, \n      endpoint = \"predict\", \n      query = list(weight = 1)\n      ),\n    \"response\"\n  )\n  \n  # ---------- Run tests against response ---------------\n  ## Test to confirm that the response was a success\n  expect_equal(\n    get_predict$status_code,\n    200\n  )\n  ## Test to confirm the output of the API is correct\n  expect_equal(\n    round(content(get_predict)[[1]]),\n    32\n  )\n  \n  # ----- Close the session, and the plumber API --------\n  expect_null(\n    call_that_session_stop(api_session)\n  )\n})\n```\n\n### Managing the API tests\n\n`callthat` matches the API to the test by name.  The `call_that_available_tests()` returns a table with the available APIs in your package, and it displays if there is a matching test.\n\n```{r, eval = FALSE}\ncall_that_available_tests()\n```\n\n```{r, echo = FALSE, eval = TRUE}\ncall_that_available_tests(\n  plumber_directory = \"inst/plumber\",\n  test_directory = \"inst/tests\"\n)\n```\n\n### Run tests against the published API\n\n`callthat` makes it possible to run the exact same tests used for unit testing, for integration testing.  \n\nOpen a connection with the published API.  If that API is running in RStudio Connect, then use `call_that_rsc_connection()`, otherwise use the generic `call_that_connection()`.\n\n```{r, eval = TRUE}\npublished_api \u003c- call_that_rsc_connection(\n  url = \"https://colorado.rstudio.com/rsc/callthat/testapi\",\n  key = Sys.getenv(\"RSC_TOKEN\") # Optional, only if access to your API is restricted\n  )\n\npublished_api\n```\n\nThe `call_that_test_remote()` functions runs the tests for the API, but it is possible to also pass the published API's connection so that the tests run remotely.\n\n```{r}\ncall_that_test_remote(\"sample-api\", published_api)\n```\n\n```{r, echo = FALSE, eval = TRUE}\ncall_that_test_remote(\"sample-api\", published_api, test_directory = \"inst/tests\")\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedgararuiz%2Fcallthat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedgararuiz%2Fcallthat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedgararuiz%2Fcallthat/lists"}