Client for Maya CMS

A client for your Maya CMS content.

Version GitHub license By lynn

How Install

Flutter:

$ flutter pub add flutter_maya_sdk

Pubspec.yaml

Add a line like this to your package's pubspec.yaml

dependencies:
  flutter_maya_sdk: ^0.0.1

And run if your editor don't support flutter

flutter pub get

And import it

Now in your Dart code, add

import 'package:flutter_maya_sdk/flutter_maya_sdk.dart';

How Initialize


Minimal

import 'package:flutter_maya_sdk/flutter_maya_sdk.dart';

// Minimal Initialisation of global instance
Maya.init(
    server: Uri.parse("http://[SERVER HOST]"),
    token: "[API TOKEN]",
);
// globale instance is accesible by `Maya.instance`

// Or 
Maya maya = Maya(
    server: Uri.parse("http://[SERVER HOST]"),
    token: "[API TOKEN]",
)

Maya is not in root web folder

import 'package:flutter_maya_sdk/flutter_maya_sdk.dart';

// Custom path 
Maya.init(
    server: Uri.https("[SERVER HOST]"."/Maya/folder"),
    // OR
    //server: Uri.http("[SERVER HOST]"."/Maya/folder"),
    // OR
    //server: Uri.parse("http://[SERVER HOST]/Maya/folder"),
    token: "[API TOKEN]",
);

// Or 
Maya maya = Maya(
    server: Uri.parse("http://[SERVER HOST]"),    
    // OR
    //server: Uri.http("[SERVER HOST]"."/Maya/folder"),
    // OR
    //server: Uri.parse("http://[SERVER HOST]/Maya/folder"),
    token: "[API TOKEN]",
)

Filter for all request

import 'package:flutter_maya_sdk/flutter_maya_sdk.dart';

// never get deleted fields
Maya.init(
    server: Uri.https("[SERVER HOST]"."/Maya/folder"),
    token: "[API TOKEN]",
    defaultFilter : {
      r"$or" :[
        { "delete" : {$eq : false} },
        { "delete" : {$eq : null} },
        {
          "delete" : {
            r"$exist" : false
          }
        }
      ]
    }
);

(Optional) Declare endpoint

import 'package:flutter_maya_sdk/flutter_maya_sdk.dart';

// Api declaration
Maya.init(
    server: Uri.https("[SERVER HOST]"."/Maya/folder"),
    token: "[API TOKEN]",
    api : { 
      // Collection
      "myCollection" :  {
        "collection": "users", // real collection name
        "sort": {"login": 1}, // default sort
        "fields": [ // get only this fields
          "nom",
          "prenom",
          "parent",
          "login",
          "enabled",
          "_create_by"
        ]
      },
      // Form
      "myForm" :  {
        "form": "sendmail" // real form name
      },
      // Singleton
      "mySingleton" : {
        "singleton": "configurations" // real singleton name
      },
      // clollection with virtual properties (map)
      "slides" : {
        "collection": "collection_name", //collection name in Maya
        "limit": 5, // limit when get data from server
        "sort": { // sort results
          "_o": 1
        },
        "fields": [ // fields to gets from the server, other will be ignored
          "title",
          "image",
          "description",
          "backgroundColor",
          "fontColor"
        ],
        "map": { // change value of a property or set  a new property in result object
          // you can build string from a template
          "image": "{{SERVER}}{{image.path}}",
          // or map a property's value to another
          "body" : "{{description}}"
        },
      },
    }
);

How Use

Read data

// get all elements
List<Map<String, dynamic>> results = await MayaCollection("api_access").find(
  cache: Duration(hours : 1), // [optional] cache result
);

// get first element
Map<String, dynamic> result = await MayaCollection("user").findOne(
  filter: {
    "login" : "root",
    "pwd" : "secret",
    r"$or": [
      {"disable": false},
      {
        "disable": {
          r"$exists": false,
        },
      },
    ],
  },
);

// get specific element
Map<String, dynamic> result = await MayaCollection("user").get("[My Super ID]");
// get only some fields
Map<String, dynamic> result = await MayaCollection("user").get("[My Super ID]", fields : ["nom", "prenom"]
);

// get one page of elements
List<Map<String, dynamic>> results = await MayaCollection("api_access").find(
  limit : 10,
  page : 2
); // page start by 0, also set page to 0 for the first page, page to 1 for the second page

// get filtered elements , you can use page, limit, sort, etc. with filter
List<Map<String, dynamic>> results = await MayaCollection("api_access").find(
  filter: {
    published : true,
  },
);

Read undeclared endpoint

To read an undelared endpoint you can just pass the name and use a prefix to specify the type (form, singleton,collection or custom url)

Prefix Type Usage
* Collection Maya("my_collection") OR Maya("*my_collection")
@ Singleton Maya("@my_singleton")
# Form Maya("#my_form")
! Custom Maya Api Maya("!my/custom/url")

Save data

// post data to Maya (form and collection)
Map<String, dynamic> data = await MayaCollection("api_access").save(
  data : {
    published : false,
    title : "Cool",
    description : "I'm juste a test :-p",
  },
);

post to a Custom Url

// post data to a custom Maya url
Map<String, dynamic> data = await MayaApi("!/my/custom/url/gps").save(
  data : {
    lat : -19.016682,
    long : 26.806641,
    alt : 15,
  },
);

Maya urls

Login as system user

Map<String, dynamic> user = Maya.instance.login('username','xxpasswordxx')

Logout as system user

bool logout = await Maya.instance.logout()

Create / Update user

Maya.instance.saveSystemUser({...}) // user data (user, name, email, active, group)

Get users.

Maya.instance.systemUsers([Map<String, dynamic> filter]) // (optional) you can pass filter

Get assets

Maya.instance.assets([Map<String, dynamic> filter]) // (optional) you can pass filter

Get thumbnail url

Maya.instance.image(imagePath, 
  width : width,
  height : height,
  quality : quality,
  domain : domain,
  o : o,
  base64 : base64,
);

Get all singletons

Maya.listSingletons()

Get all collections

Maya.listCollections();

Get collection schema

Maya("collectionname").schema();

Update collection schema

Maya("collectionname").updateSchema(fields); // fields is List<Map<String,dynamic>>

Libraries

maya