All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.easimart.EasimartCloudCodeController Maven / Gradle / Ivy

package co.easimart;

import org.json.JSONObject;

import java.util.Map;

import bolts.Continuation;
import bolts.Task;

/** package */ class EasimartCloudCodeController {

  /* package for test */ final EasimartHttpClient restClient;

  public EasimartCloudCodeController(EasimartHttpClient restClient) {
    this.restClient = restClient;
  }

  public  Task callFunctionInBackground(final String name,
      final Map params, String sessionToken) {
    EasimartRESTCommand command = EasimartRESTCloudCommand.callFunctionCommand(
            name,
            params,
            sessionToken);
    return command.executeAsync(restClient).onSuccess(new Continuation() {
      @Override
      public T then(Task task) throws Exception {
        @SuppressWarnings("unchecked")
        T result = (T) convertCloudResponse(task.getResult());
        return result;
      }
    });
  }

  /*
   * Decodes any Easimart data types in the result of the cloud function call.
   */
  /* package for test */ Object convertCloudResponse(Object result) {
    if (result instanceof JSONObject) {
      JSONObject jsonResult = (JSONObject)result;
      result = jsonResult.opt("result");
    }

    EasimartDecoder decoder = EasimartDecoder.get();
    Object finalResult = decoder.decode(result);
    if (finalResult != null) {
      return finalResult;
    }

    return result;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy