Writing Custom Code — Your Code, Our Cloud

custom code bg2 191x300 Writing Custom Code    Your Code, Our CloudApp42 platform has various technical and business services using which app development can be done without writing a single line of backend code. However there might be a case where the app developer might need to have some server side custom code for their app. There could be multiple scenarios for example-

  1. You want to add server side validation and or custom logic on the input data instead of on the client.
  2. You want to make a custom service by aggregating two or more App42 services. For example aggregating Push and Storage service for custom requirement.
  3. App logic that might get changed in future and hence you don’t want to bundle it in your app. Instead you would like to put it on the server where it can be accessed through REST calls from your app.

App42 platform now gives you the power to host your custom server side code in our cloud. Below are the listed features and advantages for the same.

  1. You don’t have to pay for an entire VM/Instance for hosting, you have to just pay based on your usage which will include the number of API calls made to your custom code.
  2. You don’t need to worry about scalability and high availability issues with your custom code, App42 Custom Code platform is intelligent enough to give scalibility to your code on the fly.
  3. Deployed Custom code services are wrapped in our REST Security model, hence as a developer you are free to just focus on the business logic keeping aside any security concerns.
  4. App42 Custom code can be written using powerful and scalable JAVA language.

Writing Custom Code

  1. Download the sample code from GitHub.
  2. Import the Sample Project in your Eclipse IDE.
  3. Open Java source file MyCustomCode.java inside com.shephertz.app42.paas.customcode.sample folder and modify the execute method of it as shown below.
package com.shephertz.app42.paas.customcode.sample;
public class MyCustomCode implements Executor {
private ServiceAPI sp = new ServiceAPI("YOUR_APIKEY","YOUR_SECRETKEY");
private final int HTTP_STATUS_SUCCESS = 200;
private String moduleName = "App42CustomCodeTest";
/**
* Write your custom code inside this method
*/
@Override
public HttpResponseObject execute(HttpRequestObject request) {
JSONObject body = request.getBody();
// Build Log Service For logging in Your Code
LogService logger = sp.buildLogService();
logger.debug(" Recieved Request Body : :" + body.toString(), moduleName);
 
// Write Your Custom Code Here
// ......//
 
logger.info("Running Custom Code ", moduleName);
// Create JSON Response Based on Your business logic
JSONObject jsonResponse = new JSONObject();
try {
jsonResponse.put("status", "success");
jsonResponse.put("message", "From Custom Code");
//....//
} catch (JSONException e) {
// Do exception Handling for JSON Parsing
}
// Return JSON Response and Status Code
return new HttpResponseObject(HTTP_STATUS_SUCCESS, jsonResponse);
}
}
  1. In the above example, if you want to rename or change the package and class name, you are free to do so.
  2. Above example uses App42 Logging Service to do the logging inside your custom code. These logs would be available to you inside AppHQ console.
  3. Custom Code deployed in our cloud runs inside a sand-boxed environment for each service. There are restrictions on using certain JAVA APIs like System, FileIO, Reflection, ByteCode injection etc. If you have any requirement where you are looking to use these restricted APIs or any other third party Jar file, please write to us at support@shephertz.com for further help.

Deploying Custom Code

  1. Once you have written your custom code, you can deploy it either through ant command line or using our APIs.
  2. To deploy your custom code through Ant script, run < ant deploy > from a command line inside your root folder of the sample project that you downloaded from above. Before running this command, modify build.properties to enter your APIKey and SecretKey for the same.
  3. Once you run the above command, you will be asked to enter the name for the custom code to be deployed on App42 Cloud. It’s default name is MyCustomCode. If you get BUILD SUCCESSFUL message after entering the name, your App is deployed on App42 Cloud. Otherwise you will get an appropriate exception message.
  4. To deploy your custom code through API, you have to call deployJarFile method on CustomCodeService API by passing the name and the jar file’s location. In this case you have to first create the Jar file of your custom code using the JDK Jar utility and then call the API by passing the jar file’s location and name of the service to be deployed.

Running Custom Code

  1. Once your custom code service is deployed, it can be accessed either through REST calls using url https://customcode.shephertz.com/1.0/run/java/ or from native App42 SDKs available in different languages.
  2. Example for Calling Custom code from Android/JAVA SDK is shown below
ServiceAPI sp = new ServiceAPI("YOUR_APIKEY", "YOUR_SECRETKEY");
CustomCodeService customCodeService = sp.buildCustomCodeService();
String name  = "YOUR_DEPLOYED_CUSTOMCODE_SERVICE_NAME";
JSONObject requestBody = new JSONObject();
requestBody.put("name", "App42CustomCode");
//.....More Request Parameters required to your custom code can be set here in JSON format....//
 
//Run Custom Code 
JSONObject responseObject = customCodeService.runJavaCode(name, requestBody);
 
//Print JSON Response From Custom Code
System.out.println("Response From Custom Code : " + responseObject);

8046709a 9a4d 4fc3 910a a3e6abc0436f Writing Custom Code    Your Code, Our Cloud    8931e3f4 f630 47a3 9675 fa5b55cf7e24 Writing Custom Code    Your Code, Our Cloud

twitter Writing Custom Code    Your Code, Our Cloudfacebook Writing Custom Code    Your Code, Our Cloudgoogle Writing Custom Code    Your Code, Our Cloudlinkedin Writing Custom Code    Your Code, Our Cloudpinterest Writing Custom Code    Your Code, Our Cloudreddit Writing Custom Code    Your Code, Our Cloudstumbleupon Writing Custom Code    Your Code, Our Cloudtumblr Writing Custom Code    Your Code, Our Cloud

Leave a Reply

Your email address will not be published. Required fields are marked *


four − = 0

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>