BaaS REST API 

This document describes how to interact with MakeItApp BaaS REST API for CRUD operations with custom collections configured via Dashboard (see the BaaS introduction for details).


Request the BaaS for your App 

Ask your App Angel to create a new BaaS for you.
You will see in the Tools tab of your MakeItApp project the BaaS url and the private key.

Schermata 2014-05-06 alle 17.11.40

Now you can access to your BaaS using the Dashboard and  create a collection.


APIs 

Collections can be managed using REST calls. All requests to the BaaS are under https and requires a key on the header. You will receive the key when the App goes in Development status.

Please note: all names are case sensitive!

secret:YOUR_KEY

Get data 

To retrieve a single element of the collection

GET /:collection/:ID

To retrieve all elements of the collection

GET /:collection

Example with CURL
Using curl - http://curl.haxx.se/download.html - you can manage your collection.

URL='YOUR_BAAS_URL'
FILTER=$URL'/YOUR_COLLECTION_NAME'
SECRET='secret: YOUR_KEY'
curl --header $SECRET -H Accept: application/json -H Content-Type: application/json -X GET $FILTER

Filter Data 

You can use all MongoDB query syntax to filter data on our BaaS. Here details about filters on MongoDB.

GET /:collection?{MONGO_FILTER}

Examples:

Filter by field (get cards with color red)

GET /:card?{"color":"red"}

Filter by regex (get cards with colors that start with r)

GET /:card?{"color":{"$regex":"r"}}

Paginate Data 

Pagination uses “skip” and “limit” to identify pages. Skip identify the number of records to skip from the start. Limit the max result. If you have 300 results and you want to get the third page with a dimension of 50 records set skip to 100 and limit to 50.

GET /:card?{"$skip":100,"$limit":50}

Sort Data 

Sort by card name. 1: ascending. -1: descending.

GET /:card?{$sort":{"card":1}}

Comparison Operators 

From Mongo website
$gt Matches values that are greater than the value specified in the query.
$gte Matches values that are equal to or greater than the value specified in the query.
$in Matches any of the values that exist in an array specified in the query.
$lt Matches values that are less than the value specified in the query.
$lte Matches values that are less than or equal to the value specified in the query.
$ne Matches all values that are not equal to the value specified in the query.
$nin Matches values that do not exist in an array specified to the query.

Get players with revenues greater that $100

GET /:player?{"revenue":{"$gt":100}}

A complete Example 

Put all together

GET /:card?{"$skip":0,"$limit":50,"$sort":{"card":1},"card":{"$regex":"c"}}
Post data
FILTER_DATA=
curl --header $SECRET -H "Accept: application/json" -H "Content-Type: application/json" -d $FILTER_DATA -X POST $FILTER
Delete data
curl --header $SECRET -H "Accept: application/json" -H "Content-Type: application/json" -X DELETE $FILTER

User Login 

Setup the user in BaaS table (you can create the User from the dashboard or using REST Post).
Screen Shot 2014-04-01 at 21.23.58
In the example. User: THE_USER. Password: THE_PWD.

URL='YOUR_BAAS_URL'
SECRET='dpd-ssh=key: YOUR_KEY'
LOGIN=$URL'/users/login'
ME=$URL'/users/me'
USR='THE_USER'
PWD='THE_PWD'
curl -H "Accept: application/json" -X POST -d 'username='$USR -d 'password='$PWD $LOGIN

The result will be

{"path":"/users","uid":"e7233e27508feb0f","id":"403a2d32c06cffcc631b769f809bcd02833132cd06e08232daebf8f6cb85ad7c62f548f60b228494cc5595b35692a36758bd4ee305b2a08e997f2892d5b56832"}

You can use the id in the cookie

URL='YOUR_BAAS_URL'
SECRET='dpd-ssh=key: YOUR_KEY'
LOGIN=$URL'/users/login'
ME=$URL'/users/me'
USR='THE_USER'
PWD='THE_PWD'
COOKIE='403a2d32c06cffcc631b769f809bcd02833132cd06e08232daebf8f6cb85ad7c62f548f60b228494cc5595b35692a36758bd4ee305b2a08e997f2892d5b56832'
curl -H "Accept: application/json" --cookie "sid=$COOKIE" -X GET $ME

Obtaining the description of the user or the access to collections protected by a login (you can protect a collection).

{"username":"giulio","email":"giulio@giulio.it","validatedEmail":true,"id":"e7233e27508feb0f"}

Upload files 

The BaaS support also the upload/download and delete of files using the REST api :files.
To upload a file it’s necessary to send a multi-part request to the API. Here is the example with CURL

curl --header $SECRET -F file="myfile.txt" $URL/files/myfile.txt

Where $URL is the baseurl of your BaaS and $SECRET the secret key.

The API reply with a JSON that return the result and all information about the file. Locations contains the url that you can use to download the file.

{"message":"inserted","id":"86e56055-425a-46eb-a4a4-206bbbb56351.jpg","fileName":"67d892e2-7737-4daf-9723-aacaed17f70a.jpg","size":841416,"location":"https://THE_URL"}

You can get the complete list of files with a GET to files collection

curl --header $SECRET $URL/files