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

co.easimart.EasimartRESTUserCommand Maven / Gradle / Ivy

package co.easimart;

import co.easimart.http.EasimartHttpRequest;
import co.easimart.http.EasimartHttpResponse;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import bolts.Task;

/** package */ class EasimartRESTUserCommand extends EasimartRESTCommand {

  private static final String HEADER_REVOCABLE_SESSION = "X-Easimart-Revocable-Session";
  private static final String HEADER_TRUE = "1";

  public static EasimartRESTUserCommand getCurrentUserCommand(String sessionToken) {
    return new EasimartRESTUserCommand("users/me", EasimartHttpRequest.Method.GET, null, sessionToken);
  }

  //region Authentication

  public static EasimartRESTUserCommand signUpUserCommand(JSONObject parameters, String sessionToken,
      boolean revocableSession) {
    return new EasimartRESTUserCommand(
        "classes/_User", EasimartHttpRequest.Method.POST, parameters, sessionToken, revocableSession);
  }

  public static EasimartRESTUserCommand logInUserCommand(String username, String password,
      boolean revocableSession) {
    Map parameters = new HashMap<>();
    parameters.put("username", username);
    parameters.put("password", password);
    return new EasimartRESTUserCommand(
        "login", EasimartHttpRequest.Method.GET, parameters, null, revocableSession);
  }

  public static EasimartRESTUserCommand serviceLogInUserCommand(
      String authType, Map authData, boolean revocableSession) {

    // Mimic EasimartSetOperation
    JSONObject parameters;
    try {
      JSONObject authenticationData = new JSONObject();
      authenticationData.put(authType, PointerEncoder.get().encode(authData));

      parameters = new JSONObject();
      parameters.put("authData", authenticationData);
    } catch (JSONException e) {
      throw new RuntimeException("could not serialize object to JSON");
    }

    return serviceLogInUserCommand(parameters, null, revocableSession);
  }

  public static EasimartRESTUserCommand serviceLogInUserCommand(JSONObject parameters,
      String sessionToken, boolean revocableSession) {
    return new EasimartRESTUserCommand(
        "users", EasimartHttpRequest.Method.POST, parameters, sessionToken, revocableSession);
  }

  //endregion

  public static EasimartRESTUserCommand resetPasswordResetCommand(String email) {
    Map parameters = new HashMap<>();
    parameters.put("email", email);
    return new EasimartRESTUserCommand(
        "requestPasswordReset", EasimartHttpRequest.Method.POST, parameters, null);
  }

  private boolean isRevocableSessionEnabled;
  private int statusCode;

  private EasimartRESTUserCommand(
          String httpPath,
          EasimartHttpRequest.Method httpMethod,
          Map parameters,
          String sessionToken) {
    this(httpPath, httpMethod, parameters, sessionToken, false);
  }

  private EasimartRESTUserCommand(
          String httpPath,
          EasimartHttpRequest.Method httpMethod,
          Map parameters,
          String sessionToken, boolean isRevocableSessionEnabled) {
    super(httpPath, httpMethod, parameters, sessionToken);
    this.isRevocableSessionEnabled = isRevocableSessionEnabled;
  }

  private EasimartRESTUserCommand(
          String httpPath,
          EasimartHttpRequest.Method httpMethod,
          JSONObject parameters,
          String sessionToken, boolean isRevocableSessionEnabled) {
    super(httpPath, httpMethod, parameters, sessionToken);
    this.isRevocableSessionEnabled = isRevocableSessionEnabled;
  }

  public int getStatusCode() {
    return statusCode;
  }

  @Override
  protected void addAdditionalHeaders(EasimartHttpRequest.Builder requestBuilder) {
    super.addAdditionalHeaders(requestBuilder);
    if (isRevocableSessionEnabled) {
      requestBuilder.addHeader(HEADER_REVOCABLE_SESSION, HEADER_TRUE);
    }
  }

  @Override
  protected Task onResponseAsync(EasimartHttpResponse response,
      ProgressCallback progressCallback) {
    statusCode = response.getStatusCode();
    return super.onResponseAsync(response, progressCallback);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy