bones_api 1.0.1
bones_api: ^1.0.1 copied to clipboard
Bones_API - Simple and easy API framework, with routes and HTTP Server.
Bones_API #
Bones_API - Simple and easy API framework, with routes and HTTP Server.
Usage #
A simple usage example:
import 'package:bones_api/bones_api.dart';
import 'package:bones_api/src/bones_api_server.dart';
void main() async {
var api = MyAPI();
// Calling the API directly:
var r1 = await api.call(APIRequest.get('/service/base/foo'));
print(r1);
// Serving the API trough a HTTP Server:
var apiServer = APIServer(api, '*', 8088);
await apiServer.start();
print('Running: $apiServer');
print('URL: ${apiServer.url}');
}
class MyAPI extends APIRoot {
MyAPI() : super('example', '1.0');
@override
Set<APIModule> loadModules() => {MyBaseModule()};
}
class MyBaseModule extends APIModule {
MyBaseModule() : super('base');
@override
String? get defaultRouteName => '404';
@override
void configure() {
routes.get('foo', (request) => APIResponse.ok('Hi[GET]!'));
routes.pos(
'foo', (request) => APIResponse.ok('Hi[POST]! ${request.parameters}'));
routes.any('time', (request) => APIResponse.ok(DateTime.now()));
routes.any('404',
(request) => APIResponse.notFound(payload: '404: ${request.path}'));
}
}
CLI #
You can use the built-in command-line interface (CLI) bones_api
.
To activate it globally:
$> dart pub global activate bones_api
Now tou can use the CLI directly:
$> bones_api --help
To serve an API project:
$> bones_api serve --directory path/to/project --class MyAPIRoot
Bones_UI #
See also the package Bones_UI, a simple and easy Web User Interface Framework for Dart.
Features and bugs #
Please file feature requests and bugs at the issue tracker.
Colossus.Services #
This is an open-source project from Colossus.Services: the gateway for smooth solutions.
Author #
Graciliano M. Passos: gmpassos@GitHub.