Skip to content

lambdaR/just-a-dart-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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/m3o.dart';

void main() async {
  final token = Platform.environment['M3O_API_TOKEN']!;
  final hwservice = HelloWorldService(
    Options(
      token: token,
      address: liveAddress,
    ),
  );

  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);
  }
}

Generic Client

The generic client enables you to call any endpoint by name.

import 'dart:io';

import 'package:M3O/m3o.dart';

void main() async {

    final token = Platform.environment['M3O_API_TOKEN']!;

    final client = Client(
        Options(
            token: token,
            address: liveAddress,
        ),
    );

    Request request = Request(
        service: 'helloworld',
        endpoint: 'Call',
        body: {
        'name': 'John',
        },
    );

    Response res = await client.call(request);

    print(res);

    exit(0);
}

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 token = Platform.environment['M3O_API_TOKEN']!;
  final wservice = WeatherService(
    Options(
      token: token,
      address: liveAddress,
    ),
  );

  NowRequest req = NowRequest(location: 'lonon');
  try {
    NowResponse res = await wservice.now(req);
    return res;
  } catch (e) {
    // handle error
  }
}

and then use FutureBuilder to create a widget like this:

class MyWidget extends StatelessWidget {
  @override
  Widget build(context) {
    return FutureBuilder<NowResponse>(
      future: londonCurrentWeather(),
      builder: (context, AsyncSnapshot<NowResponse> snapshot) {
        if (snapshot.hasData) {
          // extract the data and return a widget
        } else {
          return CircularProgressIndicator();
        }
      }
    );
  }
}