Title: | Dynamic OpenAPI/Swagger Client |
---|---|
Description: | Access services specified in OpenAPI (formerly Swagger) format. It is not a code generator. Client is generated dynamically as a list of R functions. |
Authors: | Darko Bergant [aut], Marcel Ramos [cre, ctb] , Alexandru Mahmoud [ctb], Sean Davis [ctb], Martin Morgan [ctb] |
Maintainer: | Marcel Ramos <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.8 |
Built: | 2024-10-28 18:21:28 UTC |
Source: | https://github.com/canmod/rapiclient |
Create R functions directly from OpenAPI (formerly Swagger) specification.
Use get_api
to read the specification,
get_operations
to get client functions and
get_schemas
to create functions for additional schemas.
See usage example at https://github.com/bergant/rapiclient#rapiclient
Check out https://github.com/OAI/OpenAPI-Specification for additional information about Open API specification
Please use https://github.com/bergant/rapiclient/issues for issues
Maintainer: Marcel Ramos [email protected] (ORCID) [contributor]
Authors:
Darko Bergant [email protected]
Other contributors:
Alexandru Mahmoud [contributor]
Sean Davis [contributor]
Martin Morgan [contributor]
Useful links:
## Not run: # Read API description api <- get_api(api_url) # create operation and schema functions operations <- get_operations(api) schemas <- get_schemas(api) # call service operations$some_operation(x, y, schemas$some_structure(u, v, ...)) ## End(Not run)
## Not run: # Read API description api <- get_api(api_url) # create operation and schema functions operations <- get_operations(api) schemas <- get_schemas(api) # call service operations$some_operation(x, y, schemas$some_structure(u, v, ...)) ## End(Not run)
Create API object from Swagger specification
get_api(url, config = NULL, ext)
get_api(url, config = NULL, ext)
url |
API URL or file (can be json or yaml format) |
config |
httr::config() curl options. |
ext |
the file extension of the API file (either 'yaml' or 'json'). By default, it is obtained from the URL with 'tools::file_ext' and should be provided when the file URL is missing an extension. |
API object
See also get_operations
and get_schemas
## Not run: # create operation and schema functions api_url <- "http://petstore.swagger.io/v2/swagger.json" api <- get_api(api_url) operations <- get_operations(api) schemas <- get_schemas(api) ## End(Not run)
## Not run: # create operation and schema functions api_url <- "http://petstore.swagger.io/v2/swagger.json" api <- get_api(api_url) operations <- get_operations(api) schemas <- get_schemas(api) ## End(Not run)
Creates a list of functions from API operations definition. Names in a list are operationIDs from API.
get_operations( api, .headers = NULL, path = NULL, handle_response = identity, auto_unbox = FALSE )
get_operations( api, .headers = NULL, path = NULL, handle_response = identity, auto_unbox = FALSE )
api |
API object (see |
.headers |
Optional headers passed to httr functions. See
|
path |
(optional) filter by path from API specification |
handle_response |
(optional) A function with a single argument: httr response |
auto_unbox |
automatically |
All functions return a response object from httr package or a
value returned by handle_response
function if specified. When
path
is defined, only operations with the specified API path root are
created. Use .headers
parameters to send additional headers when
sending a request.
A list of functions.
If no response handler function is defined, operation functions return response object (httr package). See httr content documentation for extracting content from a request, and functions http_error and http_status how to handle http errors and error messages.
When using simple result_handlers
, operations will return the
content of response instead of httr response object (or handle error as
exception or warning in case of error).
To handle response automatically with custom function, define a function
with httr response object as argument and pass it as handle_response
argument to get_operations
function.
## Not run: # create operation and schema functions api_url <- "http://petstore.swagger.io/v2/swagger.json" api <- get_api(api_url) operations <- get_operations(api) schemas <- get_schemas(api) # get operations which return content or stop on error operations <- get_operations(api, handle_response = content_or_stop) # use .headers when operations must send additional headers when sending operations <- get_operations(api, .headers = c("api-key" = Sys.getenv("SOME_API_KEY"))) ## End(Not run)
## Not run: # create operation and schema functions api_url <- "http://petstore.swagger.io/v2/swagger.json" api <- get_api(api_url) operations <- get_operations(api) schemas <- get_schemas(api) # get operations which return content or stop on error operations <- get_operations(api, handle_response = content_or_stop) # use .headers when operations must send additional headers when sending operations <- get_operations(api, .headers = c("api-key" = Sys.getenv("SOME_API_KEY"))) ## End(Not run)
Returns a list of functions with arguments from API schemas. Elements are named by schema names, each function returns a named list.
get_schemas(api)
get_schemas(api)
api |
Api object |
A list of functions
When creating operations from api one can define how the response from http should be handled. These functions can be used for simple result handling.
content_or_stop(x) content_or_warning(x) content_or_message(x)
content_or_stop(x) content_or_warning(x) content_or_message(x)
x |
A response object from httr package (see response object in httr package documentation) |
See get_operations
for details.
Content of http response
content_or_warning()
: Returns content or issues a warning
content_or_message()
: Returns content or prints a message
api_file <- system.file( "extdata", "sample_specs", "petstore.yaml", package = "rapiclient", mustWork = TRUE ) api <- get_api(api_file) operations <- get_operations(api, handle_response = content_or_stop)
api_file <- system.file( "extdata", "sample_specs", "petstore.yaml", package = "rapiclient", mustWork = TRUE ) api <- get_api(api_file) operations <- get_operations(api, handle_response = content_or_stop)