raw_api_server

Creator: coderz1093

Last updated:

0 purchases

raw_api_server Image
raw_api_server Images
Add to Cart

Description:

raw api server

raw_api_server #
A Dart package for building small and simple socket-based APIs.
Features #

Declarative
Lightweight
Extensible
Determines action via some uint8 of the request (default: 4 bytes)

Getting started #
Start by adding raw_api_server to your pubspec.yaml: dart pub add raw_api_server
Usage #
Server #
The most basic implementation of a server is as follows:
final server = RawApiServer(
port: <port>,
endpoints: [
...
],
);

await server.start();
copied to clipboard
The endpoints parameter expects a list of type ApiEndpoint. An endpoint consists of an id to identify which action we expect, and a function to handle the request.
final echoEndpoint = ApiEndpoint(
id: 0,
handler: (socket, args) {
final argsString = String.fromCharCodes(args);
socket.write('you said: ${argsString}');
}
);
copied to clipboard
The endpoint above occupies id = 0, so any client requests with id = 0 will be directed to our echoEndpoint. This endpoint has the basic functionality of replying to the client with their own arguments.
Note: Endpoints must have unique id values.
Client #
A very basic socket wrapper exists for some mild Dart compatibility with raw_api_server.
final client = RawApiClient(
port: <port>,
host: <host>,
onReceive: (socket, data) {
print('Client received: ${String.fromCharCodes(data)}');
}
);

await client.connect();
copied to clipboard
We can also make requests using the client. Requests are in the format of a List<int> whose first value is an endpoint id. We can build these requests easily using an ApiRequest.
final request = ApiRequest.fromUtf8(id: 0, stringArgs: 'hello!');
copied to clipboard
await client.sendRequest(request);
copied to clipboard

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.

Related Products

More From This Creator