Last updated:
0 purchases
m3o
M3O Dart Client #
This is the Dart client to access APIs on the M3O Platform
What is M3O #
M3O is an attempt to build a new public cloud platform with higher level building blocks for the Next generation of developers. M3O is powered by the open source Micro platform and programmable real world Micro Services.
M3O APIs includes DB, Cache, Stream, MQ, Events, Functions, App, SMS and more.
Usage #
Call a service using the generated client. Populate the M3O_API_TOKEN environment variable.
Import the package and initialise the service with your API token.
import 'dart:io';
import 'package:m3o/src/helloworld/helloworld.dart';
void main() async {
final hwservice = HelloworldService(Platform.environment['M3O_API_TOKEN']!);
CallRequest req1 = CallRequest(name: 'Mighty Zeus');
StreamRequest req2 = StreamRequest(messages: 15, name: 'World');
try {
CallResponse res1 = await hwservice.call(req1);
res1.map((value) => print(value.message),
Merr: (CallResponseMerr err) => print(err.body!['body']));
final st = await hwservice.stream(req2);
await for (var sr in st) {
sr.map((value) => print(value.message),
Merr: (StreamResponseMerr err) => print(err.body));
}
} catch (e) {
print(e);
} finally {
exit(0);
}
}
copied to clipboard
Generic Client #
The generic client enables you to call any endpoint by name.
import 'dart:io';
import 'package:m3o/src/client/client.dart';
void main() async {
final client = Client(Platform.environment['M3O_API_TOKEN']!);
Request request = Request(
service: 'helloworld',
endpoint: 'Call',
body: {
'name': 'John',
},
);
Response res = await client.call(request);
print(res);
exit(0);
}
copied to clipboard
Widget #
Most M3O services have endpoints that return a Future and few that return Stream.
In order to use M3O clients with Flutter, you need to wrap it with FutureBuilder and StreamBuilder.
For example, you can write a function that return Future<NowResponse> from the weather service like this:
Future<NowResponse> londonCurrentWeather() async {
final wservice = WeatherService(Platform.environment['M3O_API_TOKEN']!);
NowRequest req = NowRequest(location: 'lonon');
try {
NowResponse res = await wservice.now(req);
return res;
} catch (e) {
// handle error
}
}
copied to clipboard
and then use FutureBuilder to create a widget like this:
class MyWidget extends StatefulWidget {
// code here ...
@override
_MyWidgetState creatState() => _MyWidgetState();
}
class _MyWidgetState extends State <MyWidget> {
late final nowResponse = londonCurrentWeather();
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: nowResponse,
builder: (context, snapshot) {
if (snapshot.hasError) {
// return a widget in case of an error
}
if (snapshot.hasData) {
// extract the data and return a widget
}
return CircularProgressIndicator();
}
);
}
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.