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

com.parse.ParseCloud Maven / Gradle / Ivy

Go to download

A library that gives you access to the powerful Parse cloud platform from your Android app.

There is a newer version: 1.17.3
Show newest version
/*
 * Copyright (c) 2015-present, Parse, LLC.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */
package com.parse;

import java.util.List;
import java.util.Map;

import bolts.Continuation;
import bolts.Task;

/**
 * The ParseCloud class defines provides methods for interacting with Parse Cloud Functions. A Cloud
 * Function can be called with {@link #callFunctionInBackground(String, Map, FunctionCallback)}
 * using a {@link FunctionCallback}. For example, this sample code calls the "validateGame" Cloud
 * Function and calls processResponse if the call succeeded and handleError if it failed.
 *
 * 
 * ParseCloud.callFunctionInBackground("validateGame", parameters, new FunctionCallback() {
 *      public void done(Object object, ParseException e) {
 *        if (e == null) {
 *          processResponse(object);
 *        } else {
 *          handleError();
 *        }
 *      }
 * }
 * 
 *
 * Using the callback methods is usually preferred because the network operation will not block the
 * calling thread. However, in some cases it may be easier to use the
 * {@link #callFunction(String, Map)} call which do block the calling thread. For example, if your
 * application has already spawned a background task to perform work, that background task could use
 * the blocking calls and avoid the code complexity of callbacks.
 */
public final class ParseCloud {

  /* package for test */ static ParseCloudCodeController getCloudCodeController() {
    return ParseCorePlugins.getInstance().getCloudCodeController();
  }

  /**
   * Calls a cloud function in the background.
   *
   * @param name
   *          The cloud function to call.
   * @param params
   *          The parameters to send to the cloud function. This map can contain anything that could
   *          be placed in a ParseObject except for ParseObjects themselves.
   *
   * @return A Task that will be resolved when the cloud function has returned.
   */
  public static  Task callFunctionInBackground(final String name,
      final Map params) {
    return ParseUser.getCurrentSessionTokenAsync().onSuccessTask(new Continuation>() {
      @Override
      public Task then(Task task) throws Exception {
        String sessionToken = task.getResult();
        return getCloudCodeController().callFunctionInBackground(name, params, sessionToken);
      }
    });
  }

  /**
   * Calls a cloud function.
   *
   * @param name
   *          The cloud function to call.
   * @param params
   *          The parameters to send to the cloud function. This map can contain anything that could
   *          be placed in a ParseObject except for ParseObjects themselves.
   * @return The result of the cloud call. Result may be a @{link Map}< {@link String}, ?>,
   *         {@link ParseObject}, {@link List}<?>, or any type that can be set as a field in a
   *         ParseObject.
   * @throws ParseException
   */
  public static  T callFunction(String name, Map params) throws ParseException {
    return ParseTaskUtils.wait(ParseCloud.callFunctionInBackground(name, params));
  }

  /**
   * Calls a cloud function in the background.
   *
   * @param name
   *          The cloud function to call.
   * @param params
   *          The parameters to send to the cloud function. This map can contain anything that could
   *          be placed in a ParseObject except for ParseObjects themselves.
   * @param callback
   *          The callback that will be called when the cloud function has returned.
   */
  public static  void callFunctionInBackground(String name, Map params,
      FunctionCallback callback) {
    ParseTaskUtils.callbackOnMainThreadAsync(
        ParseCloud.callFunctionInBackground(name, params),
        callback);
  }

  private ParseCloud() {
    // do nothing
  }
}