App42 Cloud API

iOS cloud SDK

NoSQL Storage

Storage service on cloud provides the way to store the JSON document in NoSQL database running on cloud. One can store the JSON document, update it , search it and can apply the map-reduce search on stored documents. Example : If one will store JSON doc {"date":"5Feb"} it will be stored with unique Object Id and stored JSON object will loook like { "date" : "5Feb" , "_id" : { "$oid" : "4f423dcce1603b3f0bd560cf"}}. This oid can further be used to access/search the document.

In order to use the various functions available under a particular API, a developer will have to allocate and initialize an instance of ServiceAPI. After initialization, apiKey and secretKey needs to be set which has been received while creating an application under App42.

ServiceAPI *serviceAPIObj = [[ServiceAPI alloc]init];
serviceAPIObj.apiKey = @"<API_KEY>";
serviceAPIObj.secretKey = @"<SECRET_KEY>";	

After initialization, developer needs to call the buildXXXService method on ServiceAPI instance to get the instance of the particular API that you wish to build. For example, To build an instance of StorageService, buildStorageService() method needs to be called.

	StorageService *storageService = [serviceAPIObj buildStorageService];

Once the API instance has been retrieved, You are ready to use the functions available for that particular API.

The methods available under a particular API will return the domain object (like Storage in this case) as a response which will have the accessor / mutator to access the available properties for that object. You can get the response in the form of JSON as well. We have provided the JSON response with every function detail which can be retrieved by calling the toString() on the returned object.

NSString *dbName = @"test";
NSString *collectionName = @"foo";
NSString *json = @"date\":\"31 April";
						
Storage *storage = [storageService insertJSONDocument:dbName collectionName:collectionName json:json]; /* returns the Storage object. */ 
NSLog(@"dbName is = %@",storage.dbName);
NSLog(@"collectionName is = %@",storage.collectionName);
NSMutableArray *jsonDocArray = storage.jsonDocArray;             
for(JSONDocument *jsonDoc in jsonDocList)
{
    NSLog(@"docId is = %@ " , jsonDoc.docId);
    NSLog(@"JsonDoc is = %@" , jsonDoc.jsonDoc);
}

NSString *jsonResponse = [storage toString]; /* returns the response in JSON format. */
                            

The functions available under Storage API can throw some exceptions in abnormal conditions. Example of the same has been given below.

E.g. If App developer is requesting for the document that does not exist, the function will throw the App42Exception (as shown below) with message as "Not Found" and the appErrorCode as "2600" and the details as "Document by the Id '${docId}' does not Exist".

NSString *dbName = @"test";
NSString *collectionName = @"foo";
NSString *docId = @"4faa3f1ac68df147a51f8bd7";

try
{
	Storage *storage = [storageService findDocumentById:dbName collectionName:collectionName docId:docId];
}
catch(App42Exception ex) 
{
	int appErrorCode = ex.appErrorCode;
	int httpErrorCode = ex.httpErrorCode;
	if(appErrorCode == 2600)
	{
		// Handle here for Not Found (Document by the Id '<docId>' does not exist.)
	}
	else if(appErrorCode  == 2606)
	{
		// Handle here for Bad Request (Document Id '<docId>' is not valid.)
	}
	else if(appErrorCode == 1401)
	{
		// handle here for Client is not authorized
	}
	else if(appErrorCode == 1500)
	{
		// handle here for Internal Server Error
	}
	NSString *jsonText = ex.reason; /* returns the Exception text in JSON format. (as shown below)*/	
}   
	{
	   "app42Fault": {
		   "httpErrorCode": 404,
		   "appErrorCode": 2600,
		   "message": "Not Found",
		   "details": "Document by the Id '4faa3f1ac68df147a51f8bd7' does not Exist"
	   }
	}			

Below are the HTTP Error Codes and their description, the function under the Storage API can throw.

/*
1400 - BAD REQUEST - The Request parameters are invalid
1401 - UNAUTHORIZED - Client is not authorized
1500 - INTERNAL SERVER ERROR - Internal Server Error. Please try again	
*/						

Below are the Application Error Codes and their description, the function under the Storage API can throw.

/*
2600 - NOT FOUND - Document by the Id '<docId>' does not exist.
2601 - NOT FOUND - Document by key '<key>' and value '<value>' does not exist. 
2602 - NOT FOUND - No document in the collection '<collectionName>' exists.
2603 - BAD REQUEST - The request parameters are invalid. Make Sure DB and collection name exists and functions are syntactically correct.
2604 - NOT FOUND - No storage exist with the name '<storageName>'.
2605 - BAD REQUEST - Passed JSON string '<newjsonDoc>' is not valid.
2606 - BAD REQUEST - Document Id '<docId>' is not valid.	
2607 - NOT FOUND - The number of documents in the collection '<collectionName>' are less than the specified offset : <offset>.
*/					

Various functions available under Storage API has been explained below.

-(Storage*)insertJSONDocument:(NSString*)dbName collectionName:(NSString*)collectionName json:(NSString*)json

Save the JSON document in given database name and collection name.

Parameters:

dbName - Unique handler for storage name
collectionName - Name of collection under which JSON doc has to be saved
json - Target JSON document to be saved

Returns:

Storage object

Response: Storage Object

Exception:

The method throws the following exception with the error code.
/*
2605 - BAD REQUEST - Passed JSON string '<newjsonDoc>' is not valid.
*/	        
For more details click Exception
NSString *dbName = @"test";
NSString *collectionName = @"foo";
NSString *json = @"date\":\"31 April";
Storage *storage = [storageService insertJSONDocument:dbName collectionName:collectionName json:json]; /* returns the Storage object. */ 
NSLog(@"dbName is = %@",storage.dbName);
NSLog(@"collectionName is = %@",storage.collectionName);
NSMutableArray *jsonDocArray = storage.jsonDocArray;             
for(JSONDocument *jsonDoc in jsonDocList)
{
    NSLog(@"docId is = %@ " , jsonDoc.docId);
    NSLog(@"JsonDoc is = %@" , jsonDoc.jsonDoc);
}

NSString *jsonResponse = [storage toString]; /* returns the response in JSON format. */								
{
    "app42": {
        "response": {
            "success": true,
            "storage": {
                "dbName": "test",
                "collectionName": "foo",
                "jsonDoc": {
                    "date": "31 April",
                    "_id": {
                        "$oid": "4faa3f1ac68df147a51f8bd7"
                    }
                }
            }
        }
    }
}
-(Storage*)findAllDocuments:(NSString*)dbName collectionName:(NSString*)collectionName

Find all documents stored in given database and collection.

Parameters:

dbName - Unique handler for storage name
collectionName - Name of collection under which JSON doc needs to be searched

Returns:

Storage object

Response: Storage Object

Exception:

The method throws the following exception with the error code.
/*
2602 - NOT FOUND - No document in the collection '<collectionName>' exists.
*/		       
For more details click Exception
NSString *dbName = @"test";
NSString *collectionName = @"foo";		
Storage *storage = [storageService findAllDocuments:dbName collectionName:collectionName]; /* returns the Storage object. */ 
NSLog(@"dbName is = %@",storage.dbName);
NSLog(@"collectionName is = %@",storage.collectionName);
NSMutableArray *jsonDocArray = storage.jsonDocArray;             
for(JSONDocument *jsonDoc in jsonDocList)
{
    NSLog(@"docId is = %@ " , jsonDoc.docId);
    NSLog(@"JsonDoc is = %@" , jsonDoc.jsonDoc);
}

NSString *jsonResponse = [storage toString]; /* returns the response in JSON format. */		  
{
    "app42": {
        "response": {
            "success": true,
            "storage": {
                "dbName": "test",
                "collectionName": "foo",
                "jsonDoc": [
                    {
                        "_id": {
                            "$oid": "4faa3f1ac68df147a51f8bd7"
                        },
                        "date": "31jan"
                    },
                    {
                        "_id": {
                            "$oid": "4faa3f60c68df147a51f8bd8"
                        },
                        "date": "31 Jan"
                    }
                ]
            }
        }
    }
}
-(App42Response*)findAllDocumentsCount:(NSString*)dbName collectionName:(NSString*)collectionName

Find all documents stored in given database and collection.

Parameters:

dbName - Unique handler for storage name
collectionName - Name of collection under which JSON doc needs to be searched

Returns:

App42Response object

Response: App42Response Object

For more details click Exception
NSString *dbName = @"test";
NSString *collectionName = @"foo";						

App42Response *response = [storageService findAllDocumentsCount:dbName collectionName:collectionName]; /* returns the App42Response objects. */
BOOL success = response.isResponseSuccess;
int totalRecords = response.totalRecords;
NSString *jsonResponse = [response toString]; /* returns the response in JSON format. (as shown below)*/ 
{
    "app42": {
        "response": {
            "success": true,
            "totalRecords": 96
        }
    }
}
-(Storage*)findAllDocuments:(NSString*)dbName collectionName:(NSString*)collectionName max:(int)max offset:(int)offset

Find all documents stored in given database and collection.

Parameters:

dbName - Unique handler for storage name
collectionName - Name of collection under which JSON doc needs to be searched
max - Maximum number of records to be fetched
offset - From where the records are to be fetched

Returns:

Storage object

Response: Storage Object

Exception:

This method throws following exception.
/*
2607 - NOT FOUND - The number of documents in the collection '<collectionName>' are less than the specified offset : <offset>.
*/		       
For more details click Exception
NSString *dbName = @"test";
NSString *collectionName = @"foo";
int max = 1;
int offset = 0;						

Storage *storage = [storageService findAllDocuments:dbName collectionName:collectionName max:max offset:offset]; /* returns the Storage object. */ 
NSLog(@"dbName is = %@",storage.dbName);
NSLog(@"collectionName is = %@",storage.collectionName);
NSMutableArray *jsonDocArray = storage.jsonDocArray;             
for(JSONDocument *jsonDoc in jsonDocList)
{
    NSLog(@"docId is = %@ " , jsonDoc.docId);
    NSLog(@"JsonDoc is = %@" , jsonDoc.jsonDoc);
}

NSString *jsonResponse = [storage toString]; /* returns the response in JSON format. */								
{
    "app42": {
        "response": {
            "success": true,
            "storage": {
                "dbName": "dbName1231343049167564",
                "collectionName": "collection1231343049167564",
                "jsonDoc": [
                    {
                        "_id": {
                            "$oid": "4faa3f1ac68df147a51f8bd7"
                        },
                        "date": "31jan"
                    }
                ]
            }
        }
    }
}
-(Storage*)findDocumentById:(NSString*)dbName collectionName:(NSString*)collectionName docId:(NSString*)docId

Find target document by given unique object id.

Parameters:

dbName - Unique handler for storage name
collectionName - Name of collection under which JSON doc needs to be searched
docId - Unique Object Id handler

Returns:

Storage object

Response: Storage Object

Exception:

The method throws the following exception with the error code.
/*
2600 - NOT FOUND - Document by the Id '<docId>' does not exist.
2606 - BAD REQUEST - Document Id '<docId>' is not valid.	
*/		       
For more details click Exception
NSString *dbName = @"test";
NSString *collectionName = @"foo";
NSString *docId = @"4faa3f1ac68df147a51f8bd7";
						
Storage *storage = [storageService findDocumentById:dbName collectionName:collectionName docId:docId]; /* returns the Storage object. */ 
NSLog(@"dbName is = %@",storage.dbName);
NSLog(@"collectionName is = %@",storage.collectionName);
NSMutableArray *jsonDocArray = storage.jsonDocArray;             
for(JSONDocument *jsonDoc in jsonDocList)
{
    NSLog(@"docId is = %@ " , jsonDoc.docId);
    NSLog(@"JsonDoc is = %@" , jsonDoc.jsonDoc);
}

NSString *jsonResponse = [storage toString]; /* returns the response in JSON format. */			
{
    "app42": {
        "response": {
            "success": true,
            "storage": {
                "dbName": "Test",
                "collectionName": "foo",
                "jsonDoc": {
                    "_id": {
                        "$oid": "4faa3f1ac68df147a51f8bd7"
                    },
                    "date": "31jan"
                }
            }
        }
    }
}
-(Storage*)findDocumentByKeyValue:(NSString*)dbName collectionName:(NSString*)collectionName key:(NSString*)key value:(NSString*)value

Find target document using key value search parameter. This key value pair will be searched in the JSON doc stored on the cloud and matching Doc will be returned as a result of this method.

Parameters:

dbName - Unique handler for storage name
collectionName - Name of collection under which JSON doc needs to be searched
key - Key to be searched for target JSON doc
value - Value to be searched for target JSON doc

Returns:

Storage object

Response: Storage Object

Exception:

The method throws the following exception with the error code.
/*
2601 - NOT FOUND - Document by key '<key>' and value '<value>' does not exist.
*/		        
For more details click Exception
NSString *dbName = @"test";
NSString *collectionName = @"foo";
NSString *key = @"foo";
NSString *value = @"30July";				

Storage *storage = [storageService findDocumentByKeyValue:dbName collectionName:collectionName key:key value:value]; /* returns the Storage object. */ 
NSLog(@"dbName is = %@",storage.dbName);
NSLog(@"collectionName is = %@",storage.collectionName);
NSMutableArray *jsonDocArray = storage.jsonDocArray;             
for(JSONDocument *jsonDoc in jsonDocList)
{
    NSLog(@"docId is = %@ " , jsonDoc.docId);
    NSLog(@"JsonDoc is = %@" , jsonDoc.jsonDoc);
}
NSString *jsonResponse = [storage toString]; /* returns the response in JSON format. (as shown below)*/
{
    "app42": {
        "response": {
            "success": true,
            "storage": {
                "dbName": "test",
                "collectionName": "foo",
                "jsonDoc": [
                    {
                        "_id": {
                            "$oid": "4faa3f1ac68df147a51f8bd7"
                        },
                        "date": "3oJuly"
                    },
                    {
                        "_id": {
                            "$oid": "4faa4707c68df147a51f8be1"
                        },
                        "date": "3oJuly"
                    }
                ]
            }
        }
    }
}
-(Storage*) updateDocumentByDocId:(NSString*)dbName collectionName:(NSString*)collectionName docId:(NSString*)docId newJsonDoc:(NSString*)newJsonDoc

Update target document using the document id.

Parameters:

dbName - Unique handler for storage name
collectionName - Name of collection under which JSON doc needs to be searched.
docId - Id of the document to be updated.
newJsonDoc - New Json document to be added.

Returns:

Storage object

Response: Storage Object

Exception:

The method throws the following exception with the error code.
/*
2605 - BAD REQUEST - Passed JSON string '<newjsonDoc>' is not valid.
*/		        
For more details click Exception
NSString *dbName = @"test";
NSString *collectionName = @"foo";
NSString *docId = @"4faa4707c68df147a51f8be1";
NSString *newJsonDoc = @"{'new31jan123' : 'new31jan'}";						

Storage *storage = [storageService updateDocumentByDocId:dbName collectionName:collectionName docId:docId newJsonDoc:newJsonDoc]; /* returns the Storage object. */ 
NSLog(@"dbName is = %@",storage.dbName);
NSLog(@"collectionName is = %@",storage.collectionName);
NSMutableArray *jsonDocArray = storage.jsonDocArray;             
for(JSONDocument *jsonDoc in jsonDocList)
{
    NSLog(@"docId is = %@ " , jsonDoc.docId);
    NSLog(@"JsonDoc is = %@" , jsonDoc.jsonDoc);
}
NSString *jsonResponse = [storage toString]; /* returns the response in JSON format. (as shown below)*/
{
    "app42": {
        "response": {
            "success": true,
            "storage": {
                "dbName": "test",
                "collectionName": "foo",
                "jsonDoc": {
                    "new31jan123": "new31jan"
                }
            }
        }
    }
}
-(Storage*)updateDocumentByKeyValue:(NSString*)dbName collectionName:(NSString*)collectionName key:(NSString*)key value:(NSString*)value newJsonDoc:(NSString*)newJsonDoc

Update target document using key value search parameter. This key value pair will be searched in the JSON doc stored in the cloud and matching Doc will be updated with new value passed.

Parameters:

dbName - Unique handler for storage name.
collectionName - Name of collection under which JSON doc needs to be searched
key - Key to be searched for target JSON doc
value - Value to be searched for target JSON doc.
newJsonDoc - New Json document to be added.

Returns:

Storage object

Response: Storage Object

Exception:

The method throws the following exception with the error code.
/*
2601 - NOT FOUND - Document by key '<key>' and value '<value>' does not exist. 
2605 - BAD REQUEST - Passed JSON string '<newjsonDoc>' is not valid.
*/		        
For more details click Exception
NSString *dbName = @"test";
NSString *collectionName = @"foo";
NSString *key = @"date";
NSString *value = @"31jan";
NSString *newJsonDoc = @"{'new31jan123' : 'new31jan'}";						
  
Storage *storage = [storageService updateDocumentByKeyValue:dbName collectionName:collectionName key:key value:value newJsonDoc:newJsonDoc]; /* returns the Storage object. */ 
NSLog(@"dbName is = %@",storage.dbName);
NSLog(@"collectionName is = %@",storage.collectionName);
NSMutableArray *jsonDocArray = storage.jsonDocArray;             
for(JSONDocument *jsonDoc in jsonDocList)
{
    NSLog(@"docId is = %@ " , jsonDoc.docId);
    NSLog(@"JsonDoc is = %@" , jsonDoc.jsonDoc);
}
NSString *jsonResponse = [storage toString]; /* returns the response in JSON format. (as shown below)*/
{
    "app42": {
        "response": {
            "success": true,
            "storage": {
                "dbName": "test",
                "collectionName": "foo",
                "jsonDoc": [
                    {
                        "_id": {
                            "$oid": "4fb0dc0bc68d13dfac87d37a"
                        },
                        "31jan2012": "31jan2012"
                    }
                ]
            }
        }
    }
}
-(App42Response*)deleteDocumentById:(NSString*)dbName collectionName:(NSString*)collectionName docId:(NSString*)docId

Delete target document using Object Id from given db and collection. The Object Id will be searched in the JSON doc stored on the cloud and matching Doc will be deleted.

Parameters:

dbName - Unique handler for storage name
collectionName - Name of collection under which JSON doc needs to be searched
docId - Unique Object Id handler

Returns:

App42Response object if deleted successfully

Response: App42Response Object

Exception:

The method throws the following exception with the error code.
/*
2600 - NOT FOUND - Document by the Id '<docId>' does not exist.
*/		        
For more details click Exception
NSString *dbName = @"test";
NSString *collectionName = @"foo";
NSString *docId = @"4faa3f1ac68df147a51f8bd7";			
       
App42Response response = [storageService deleteDocumentById:dbName collectionName:collectionName docId:docId]; /* returns the App42Response object. */
BOOL success = response.isResponseSuccess;
NSString *jsonResponse = [response toString]; /* returns the response in JSON format. (as shown below)*/ 
{
    "app42": {
        "response": {
            "success": true,
            "storage": {
                "dbName": "test",
                "collectionName": "foo",
                "jsonDoc": {
                    "_id": {
                        "$oid": "4faa3f1ac68df147a51f8bd7"
                    },
                    "date": "31jan"
                }
            }
        }
    }
}
-(NSString*)mapReduce:(NSString*)dbName collectionName:(NSString*)collectionName mapFunction:(NSString*)mapFunction reduceFunction:(NSString*)reduceFunction

Map reduce function to search the target document. Please see detail information on map-reduce http://en.wikipedia.org/wiki/MapReduce .

Parameters:

dbName - Unique handler for storage name.
collectionName - Name of collection under which JSON doc needs to be searched.
mapFunction - Map function to be used to search the document
reduceFunction - Reduce function to be used to search the document

Returns:

The target JSON document.

Response: String Object

Exception:

The method throws the following exception with the error code.
/*
2603 - BAD REQUEST - The request parameters are invalid. Make Sure DB and collection name exists and functions are syntactically correct.
*/		        
For more details click Exception
NSString *dbName = @"test";
NSString *collectionName = @"foo";
NSString *mapFunction = @"function map(){ emit(this.user,1);}";
NSString *reduceFunction = @"function reduce(key, val){var sum = 0; for(var n=0;n< val.length;n++){ sum = sum + val[n]; } return sum;}";		 

NSString *response = [storageService mapReduce:dbName collectionName:collectionName mapFunction:mapFunction reduceFunction:reduceFunction] /* returns the Storage object. */ 
{
    "app42": {
        "response": {
            "success": true,
            "storage": {
                "dbName": "test",
                "collectionName": "foo",
                "jsonDoc": [
                    {
                        "_id": null,
                        "value": 2
                    }
                ]
            }
        }
    }
}
-(Storage*)insertJsonDocUsingMap:(NSString*)dbName collectionName:(NSString*)collectionName map:(NSMutableDictionary*)map

Save the JSON document in given database name and collection name. It accepts the HashMap containing key-value and convert it into JSON. Converted JSON doc further saved on the cloud using given db name and collection name.

Parameters:

dbName - Unique handler for storage name.
collectionName - Name of collection under which JSON doc has to be saved
map - HashMap containing key-value pairs

Returns:

Storage object

Response: Storage Object

For more details click Exception
NSString *dbName = @"test";
NSString *collectionName = @"foo";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"nick" forKey:@"name"];
[dict setObject:@"28" forKey:@"age"];
  
Storage *storage = [storageService insertJsonDocUsingMap:dbName collectionName:collectionName map:dict]; /* returns the Storage object. */ 
NSLog(@"dbName is = %@",storage.dbName);
NSLog(@"collectionName is = %@",storage.collectionName);
NSString *jsonResponse = [storage toString]; /* returns the response in JSON format. (as shown below)*/
{
    "app42": {
        "response": {
            "success": true,
            "storage": {
                "dbName": "test",
                "collectionName": "foo",
                "jsonDoc": {
                    "age": "28",
                    "name": "Nick",
                    "_id": {
                        "$oid": "4faa3f1ac68df147a51f8bd7"
                    }
                }
            }
        }
    }
}