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

co.easimart.NetworkUserController Maven / Gradle / Ivy

package co.easimart;

import org.json.JSONObject;

import java.util.Map;

import bolts.Continuation;
import bolts.Task;

/** package */ class NetworkUserController implements EasimartUserController {

  private static final int STATUS_CODE_CREATED = 201;

  private final EasimartHttpClient client;
  private final EasimartObjectCoder coder;
  private final boolean revocableSession;

  public NetworkUserController(EasimartHttpClient client) {
    this(client, false);
  }

  public NetworkUserController(EasimartHttpClient client, boolean revocableSession) {
    this.client = client;
    this.coder = EasimartObjectCoder.get(); // TODO(grantland): Inject
    this.revocableSession = revocableSession;
  }

  @Override
  public Task signUpAsync(
      final EasimartObject.State state,
      EasimartOperationSet operations,
      String sessionToken) {
    JSONObject objectJSON = coder.encode(state, operations, PointerEncoder.get());
    EasimartRESTCommand command = EasimartRESTUserCommand.signUpUserCommand(
            objectJSON, sessionToken, revocableSession);

    return command.executeAsync(client).onSuccess(new Continuation() {
      @Override
      public EasimartUser.State then(Task task) throws Exception {
        JSONObject result = task.getResult();
        return coder.decode(new EasimartUser.State.Builder(), result, EasimartDecoder.get())
            .isComplete(false)
            .isNew(true)
            .build();
      }
    });
  }

  //region logInAsync

  @Override
  public Task logInAsync(
      String username, String password) {
    EasimartRESTCommand command = EasimartRESTUserCommand.logInUserCommand(
            username, password, revocableSession);
    return command.executeAsync(client).onSuccess(new Continuation() {
      @Override
      public EasimartUser.State then(Task task) throws Exception {
        JSONObject result = task.getResult();

        return coder.decode(new EasimartUser.State.Builder(), result, EasimartDecoder.get())
            .isComplete(true)
            .build();
      }
    });
  }

  @Override
  public Task logInAsync(
      EasimartUser.State state, EasimartOperationSet operations) {
    JSONObject objectJSON = coder.encode(state, operations, PointerEncoder.get());
    final EasimartRESTUserCommand command = EasimartRESTUserCommand.serviceLogInUserCommand(
            objectJSON, state.sessionToken(), revocableSession);

    return command.executeAsync(client).onSuccess(new Continuation() {
      @Override
      public EasimartUser.State then(Task task) throws Exception {
        JSONObject result = task.getResult();

        // TODO(grantland): Does the server really respond back with complete object data if the
        // object isn't new?
        boolean isNew = command.getStatusCode() == STATUS_CODE_CREATED;
        boolean isComplete = !isNew;

        return coder.decode(new EasimartUser.State.Builder(), result, EasimartDecoder.get())
            .isComplete(isComplete)
            .isNew(isNew)
            .build();
      }
    });
  }

  @Override
  public Task logInAsync(
      final String authType, final Map authData) {
    final EasimartRESTUserCommand command = EasimartRESTUserCommand.serviceLogInUserCommand(
            authType, authData, revocableSession);
    return command.executeAsync(client).onSuccess(new Continuation() {
      @Override
      public EasimartUser.State then(Task task) throws Exception {
        JSONObject result = task.getResult();

        return coder.decode(new EasimartUser.State.Builder(), result, EasimartDecoder.get())
            .isComplete(true)
            .isNew(command.getStatusCode() == STATUS_CODE_CREATED)
            .putAuthData(authType, authData)
            .build();
      }
    });
  }

  //endregion

  @Override
  public Task getUserAsync(String sessionToken) {
    EasimartRESTCommand command = EasimartRESTUserCommand.getCurrentUserCommand(sessionToken);
    return command.executeAsync(client).onSuccess(new Continuation() {
      @Override
      public EasimartUser.State then(Task task) throws Exception {
        JSONObject result = task.getResult();

        return coder.decode(new EasimartUser.State.Builder(), result, EasimartDecoder.get())
            .isComplete(true)
            .build();
      }
    });
  }

  @Override
  public Task requestPasswordResetAsync(String email) {
    EasimartRESTCommand command = EasimartRESTUserCommand.resetPasswordResetCommand(email);
    return command.executeAsync(client).makeVoid();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy