Using R to generate API Client from Swagger
Microsoft R Server’s operationalization feature enables data scientists to deploy and consume web services to operationalize their R analytics. The publishService() function in mrsdeploy package can be used to publish an R code block as a new web service. After it has been deployed, the web service can be:
- Consumed directly in R by another data scientist, for testing purposes for example
- Integrated into an application by an application developer using the Swagger-based JSON file produced when the web service was published.
There are several tools like Azure AutoRest, Swagger Codegen, Swagger.io which can be used to generate API client library from swagger.json file in your preferred programming language. Now, we will look into generating API client library in R using the httr package. Using the below R code snippet, data scientists/application developers can quickly generate API Client Library in over 40 different languages immediately after publishing the web service in their R session. This method requires no extra setup, no software download/installation.
After running the above code snippet, you will find the client library in a zip file in current working directory. Unzip the file, add nuget package dependencies, add authentication workflow, add client code and begin consuming the APIs. Here is a detailed article.
To generate Client Library in your preferred language, just replace the language in httr::POST URL http://generator.swagger.io/api/gen/clients/ <language> with one of the supported languages:
The list of supported languages can be viewed here.