Suppose you’ve developed a very useful algorithm or statistical model and you need to itegrate it with some external system. Nowdays HTTP became de-facto a lingua-franca for this kind of tasks.
In this article we will demonstrate how to use RestRserve to build a basic REST API.
Genrerally RestRserve workflow consists of several major steps:
For simplicity we will use Fibonacci number calculation as an algorithm we want to expose.
Create function which will handle requests.
Now we can test our appliaction without starting it:
It is generally a good idea to write unit tests against application. One can use a common framework such as tinytest.
Generally it is a good idea to provide documentation along with the API. Convenient way to do that is to supply a openapi specification. This as simple as adding a yaml file as an additional endpoint:
openapi: 3.0.1 info: title: RestRserve OpenAPI version: '1.0' servers: - url: / paths: /fib: get: description: Calculates Fibonacci number parameters: - name: "n" description: "x for Fibonnacci number" in: query schema: type: integer example: 10 required: true responses: 200: description: API response content: text/plain: schema: type: string example: 5 400: description: Bad Request
Now all is ready and we can start application with Rserve backend. It will block R session and start listening for incoming requests.
Check app can calculate fibonacci numbers:
Check out a swagger UI in the browser: