REST Controller
Key takeaways
RestController | Component available in the HTTP module, Controller library. This component is used to create REST controllers that receive remote calls via the HTTP/REST protocol. |
Introduction
This tutorial will help you understand how REST controllerss can be created with Pip.Services. It begins by explaining the necessary pre-requisites. Then, it proceeds to explain how to create a simple REST controller using the RestController component, and how to configure and run it. It ends by showing the results on a browser.
Creating our REST Controller
Pre-requisites
In order to create our REST controller, we need to import the RestController class, which is available in the Controller library, controller module. This can be done with the following command:
import { RestController } from "pip-services4-http-node";
from pip_services4_http.controller import RestController
Additionally, we will need to import Bottle, which will be used to access our requests.
from bottle import request
REST Controller
First, we need to create our REST controller. For this, we will create a class that inherits the Pip. Services' component RestController, which has different methods that can be used to handle REST controllers. The most important one is configure, and we will use it to set up our microservice.
In this class, we first define a method to be used as a handler. In it, we define some parameters that will help us to connect to our application . In our example, this method is called my_page and the parameters are name and message.
We also define the register() method to register the controler’s route. We use the GET method, and as we are not using a schema, we define it as None.
The first part of our code will look something like this:
import { RestController } from "pip-services4-http-node";
class MyRestController extends RestController {
constructor(){
super();
this._baseRoute = "/my_service";
}
private async myPage(req, res){
let result = req.params.message + req.query.name;
this.sendResult(req, res, result);
}
public register() {
this.registerRoute("GET", "/my_page/:name", null, this.myPage)
}
}
class MyRestController(RestController):
def __init__(self):
super(MyRestController, self).__init__()
self._base_route = "/my_controller"
def my_page(self, name):
result = f"{request.query.get('message')}, {name}"
return self.send_result(result)
def register(self):
self.register_route(method="GET", route="/my_page/<name>", schema=None, handler=self.my_page)
Configuration
Now that we have our REST controller defined, we will configure and run it. To configure our component, we first need to create an instance of our class and use the method configure to set up our connection protocol, host and port. As the configure method requires a ConfigParams input type, we import this class and use its method “from_tuples” to define our configuration. Lastly, we open our component. The second part of our code will look something like this:
import { ConfigParams } from "pip-services4-components-node";
let myRestService = new MyRestController();
myRestService.configure(ConfigParams.fromTuples(
"connection.protocol", "http",
"connection.host", "localhost",
"connection.port", 15239
));
await myRestService.open(ctx);
from pip_services4_components.config import ConfigParams
my_rest_controller = MyRestController()
my_rest_controller.configure(ConfigParams.from_tuples("connection.protocol", "http",
"connection.host", "localhost",
"connection.port", 15239))
my_rest_controller.open("123")
Our microservice
Our final code will be:
import { RestController } from "pip-services4-http-node";
import { ConfigParams } from "pip-services4-components-node";
class MyRestController extends RestController {
constructor(){
super();
this._baseRoute = "/my_service";
}
private async myPage(req, res){
let result = req.params.message + req.query.name;
this.sendResult(req, res, result);
}
public register() {
this.registerRoute("GET", "/my_page/:name", null, this.myPage)
}
}
export async function main() {
let myRestService = new MyRestController();
myRestService.configure(ConfigParams.fromTuples("connection.protocol", "http",
"connection.host", "localhost",
"connection.port", 15239));
await myRestService.open(ctx);
}
from bottle import request
from pip_services4_components.config import ConfigParams
from pip_services4_http.controller import RestController
class MyRestController(RestController):
def __init__(self):
super(MyRestController, self).__init__()
self._base_route = "/my_controller"
def my_page(self, name):
result = f"{request.query.get('message')}, {name}"
return self.send_result(result)
def register(self):
self.register_route(method="GET", route="/my_page/<name>", schema=None, handler=self.my_page)
my_rest_controller = MyRestController()
my_rest_controller.configure(ConfigParams.from_tuples("connection.protocol", "http",
"connection.host", "localhost",
"connection.port", 15239))
my_rest_controller.open("123")
Running our REST controller
With our REST controller complete, we can now proceed to run it. To see the result of our code, we use the following URL:
http://localhost:15239/my_controller/my_page/John?message=hello
Which should display “Hello John” on our browser
Wrapping up
In this tutorial, we have seen how to create a simple REST controller using the RestController component available in the Pip.Services toolkit, RPC module, Controller library. First, we created our controller class. Then, we configured and run it. Finally, the result was shown as a page on our browser.