App42 Cloud API

Android Cloud SDK

Custom Code

Using Custom Code Service developer can deploy and run their custom code on our servers with out scalability consideration. App42 Custom Code platform is intelligent enought to give the scalibility to your code on the fly. App42 Custom Code platfrom comes with some guideline to write the custom code. Learn How to write Custom Code

App42 Custom code can be used to put the validation and custom logic on your data that you want to run on server instead of client side

Custom code can include App42 APIs inside it, and can be used to aggregate two or more services into your own custom code service

Once Custom code is deployed, developer can access deployed service using REST or any of supported SDKs.

Deployed Custom code service are wrapped in our REST Security model hence as a developer you are free to just focus on business logic

Custom Code API available Through REST and in SDKs can be used to run and deploy the custom code

In order to use the various functions available under a particular API, a developer will have to create an instance of ServiceAPI by passing the apiKey and the secretKey which has been received while creating an app.

	ServiceAPI api = new ServiceAPI("<API_KEY>","<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 CustomCodeService, buildCustomCodeService() method needs to be called.

	CustomCodeService customCodeService = api.buildCustomCodeService();

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

Here is the Sample code snippet to deploy the java custom code jar file.

		String nameOfCustomCodeService = "HelloWorld";
		String jarFilePath = "D:\\myapps\\HelloWorld.jar";      

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

For example, if you are trying to deploy the custom code with the name that already exist, you will get an appErrorCode as 1902 stating that Invalid JSON in request body.

		String nameOfCustomCodeService = "HelloWorld";
		String jarFilePath = "D:\\myapps\\HelloWorld.jar";
		
try {
	customCodeService.runJavaCode(nameOfCustomCodeService, jsonBody);
} 
catch (App42Exception ex) {
	
	if(ex.getAppErrorCode() == 1902) 
	{
		//Handle here for Bad Request (Invalid JSON in request body.)
	}
	else if(ex.getAppErrorCode() == 1900) 
	{
		//Handle here for Bad Request (Jar File contains the invalid content or class usage that is not allowed for deployment)
		//Example of it using System class or putting .sh/.dll file inside jar file
	}
	else if(ex.getAppErrorCode() == 1401)  
    {  
        // handle here for Client is not authorized  
    }  
    else if(ex.getAppErrorCode() == 1500)  
    {  
        // handle here for Internal Server Error  
    }  
    String jsonText = ex.getMessage(); /* returns the Exception text in JSON format. (as shown below)*/ 
}	
{
    "app42Fault": {
        "httpErrorCode": 400,
        "appErrorCode": 1902,
        "message": "Bad Request",
        "details": "Invalid JSON in request body."
    }
}			

Below are the HTTP Error Codes and their description, the function under the Custom Code 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 Custom Code API can throw.

/*
2103 - NOT FOUND - The file with the name '<Name>' does not exist.  
2100 - BAD REQUEST - The request parameters are invalid. File Name '<fileName>' already exists.. 
1900 - BAD REQUEST - Content Inside Jar file not valid. 
*/						

Various functions available under Custom Code API has been explained below.

public JSONObject runJavaCode(String nameOfCustomCodeService, JSONObject jsonBody) throws App42Exception

Run Java Code deployed in App42 Cloud

Parameters:

nameOfCustomCodeService - Name of deployed Custom Code Service
jsonBody - body content in JSON format

Returns:

JSON Response

Response: JSONObject

Exception:

The method throws the following exception with the error code.
/*
1902 - BAD REQUEST - Invalid JSON in request body. 
1903 - BAD REQUEST - Application Exception : Will include exception coming from your deployed code. For example, java.lang.NullPointerException 
1904 - BAD REQUEST - Application Exception : Request Execution Time Exceeded.
*/ 		        
For more details click Exception
Below is the sample code snippet and JSON response for the same
		String nameOfCustomCodeService = "HelloWorld";
		JSONObject jsonBody = new JSONObject();
		jsonBody.put("name", "John");
		jsonBody.put("age", "30");
		
		JSONObject jsonResponse = customCodeService.runJavaCode(nameOfCustomCodeService, jsonBody);
		
 // Return JSON response will differ and is dependent on what you are returning 
 // from your custom code in form of JSON. Here is the sample JSON in which custom
 // code is returning response code and message in JSON format.
{
    "responseCode": 200,
    "message": "Success"
}