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 https://generator.swagger.io/api/gen/clients/ <language> with one of the supported languages:
akka-scala, android, apex, async-scala, bash, clojure, cpprest, csharp, CsharpDotNet2, cwiki, dart,
dynamic-html, eiffel, elixirperl, flash, go, groovy, html, html2, java, javascript, javascript-closure-angular,
jaxrs-cxf-client, jmeter, kotlin, objc, perl, php, powershell, python, qt5cpp, ruby, scala, swagger,
swagger-yaml, swift, swift3, swift4, tizen, typescript-angular, typescript-angular2, typescript-fetch,
typescript-jquery, typescript-node
The list of supported languages can be viewed here.