App42 Cloud API

Windows Phone 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.

The Windows Phone API's work in an asynchronous mode that means the response will not be available immediately and all the API methods would not return any value and have return type as void. The response will be returned in callback methods which the caller will provide to the calling method.
As mentioned above that the response will be returned in callback methods, every API method is taking instance of App42Callback (as the last parameter) which will have the implementation of 2 methods: OnSuccess & OnException.(as shown below)

public class CallBack : App42Callback
{
public override void OnSuccess(Object obj)
{
	JObject json = (JObject) obj;
	//Do whatever you want with the JObject object
}
public override void OnException(App42Exception e)
{
	//Do whatever you want with the exception
}
}

The callback methods will return the response in the form of Object which will have to be type cast to the object domain object (e.g. String / JObject in this case) The Domain object 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.

public override void OnSuccess(Object obj)
{
JObject json = (JObject) obj;  
String jsonResponse = json.ToString(); /* returns the response in JSON format. */ 
}

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.

public override void OnException(App42Exception ex)
{
int appErrorCode = ex.GetAppErrorCode();
int httpErrorCode = ex.GetHttpErrorCode();
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 Charge 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 Charge API can throw.

/*
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
*/						

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

public JObject RunJavaCode(String nameOfCustomCodeService, JObject jsonBody)

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: JObject

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";
JObject jsonBody = new JObject();
jsonBody.Add("name", "John");
jsonBody.Add("age", "30");

OnSuccess(Object obj)
{
JObject json = (JObject) obj;
customCodeService.RunJavaCode(nameOfCustomCodeService, jsonBody);    /* returns the JObject. */ 
String jsonResponse = JObject.ToString(); /* returns the response in JSON format. (as shown below)*/
}		
 // 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"
}