📜 ⬆️ ⬇️

Create a RESTful API with Dart in minutes



Translator’s note: I would like to give readers one more reason to look at the wonderful Dart programming language, this time it’s about how quickly and easily to create a RESTful API. For those who do not know, it is clearly described what it is.
And for those who know, welcome under cat.

Where to begin


Pub has a great library called RPC. This is a lightweight package for building a Dart server RESTful API. And that's all we need.

Let's start by adding dependencies to our project in pubspec.yaml:
')
dependencies: rpc: "^0.5.5" 

Then we call pub get to upload packages to our project.
Now you can import.

 import 'package:rpc/rpc.dart'; 


We create class API


We need to create an API class that will contain our API. This is a Dart class with a special annotation @ApiClass from the RPC library. The annotation has one mandatory parameter, version, meaning the version of the API provided. For example v1. You can also add other parameters, such as a name, to be able to rename the API.

 @ApiClass(version: 'v1') class MyApi { //     } 

API methods


We want to provide the ability to do GET, POST, DELETE, etc. requests. We all put an API class with a special annotation. Methods can be placed directly in the API class or you can use the API resources.

Classroom methods


Here we create methods GET for animals and POST for animal. Each method must have an @ApiMethod annotation with the required path parameter, which indicates the path to call our method. You can also specify a parameter that indicates the type of method (GET, POST, etc.). If this parameter is not specified, the default is GET.

 @ApiClass(version: 'v1') class MyApi { List _animals = []; @ApiMethod(path: 'animals') List<Animal> getAnimals() => _animals; @ApiMethod(path: 'animals', method: 'POST') Animal postAnimal(Animal animal) { _animals.add(animal); return animal; } } 

Here we use List to store animals. And we provide two methods: getAnimals (), which returns a list of all animals and postAnimal, which adds a new animal entry to the list of animals. Naturally, we can extend this example with other API methods.

To make it all work, we need the Animal class:
 class Animal { int id; String name; int numberOfLegs; } 

API Resources


Usually we want to divide our API into resources that provide specific API methods. For example, we want to have one resource for the API for Animals and one for Person. With the help of the RPC library we can do it easily.

Create a resource class and move all methods related to the resource there:
 class AnimalResource { List _animals = []; @ApiMethod(path: 'animals') List<Animal> getAnimals() => _animals; @ApiMethod(path: 'animals', method: 'POST') Animal postAnimal(Animal animal) { _animals.add(animal); return animal; } } 

Then we create an AnimalResource instance in our API class, over which there is a @ApiResource annotation:
 @ApiClass(version: 'v1') class MyApi { @ApiResource() AnimalResource resource = new AnimalResource(); } 

Testing API


To use or test our API, we need to create a server script in the bin folder. We need to create ApiServer here, add our API there and create an HttpServer that will listen to localhost: 8080.
 library my_server; import 'dart:io'; import 'package:logging/logging.dart'; import 'package:rpc/rpc.dart'; import '../lib/server/api.dart'; final ApiServer apiServer = new ApiServer(prettyPrint: true); main() async { Logger.root..level = Level.INFO..onRecord.listen(print); apiServer.addApi(new MyApi()); HttpServer server = await HttpServer.bind(InternetAddress.ANY_IP_V4, 8080); server.listen(apiServer.httpRequestHandler); print('Server listening on http://${server.address.host}: ${server.port}'); } 

Now run the Dart script in the bin which starts the server. Now our API is available at localhost: 8080 / myApi / v1. If we want to get a list of animals, then create a GET request to localhost: 8080 / myApi / v1 / animals. Or we can add an entry by making a POST request to the same address localhost: 8080 / myApi / v1 / animals.

Testing a POST request is a bit more difficult. for this you need to install a tool like curl or an extension for Postman or Advanced REST client chrome.

That's it, the server API was created in just a few minutes.

Source: https://habr.com/ru/post/274887/


All Articles