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

co.easimart.NetworkSessionController Maven / Gradle / Ivy

package co.easimart;

import org.json.JSONObject;

import bolts.Continuation;
import bolts.Task;

/** package */ class NetworkSessionController implements EasimartSessionController {

  private final EasimartHttpClient client;
  private final EasimartObjectCoder coder;

  public NetworkSessionController(EasimartHttpClient client) {
    this.client = client;
    this.coder = EasimartObjectCoder.get(); // TODO(grantland): Inject
  }

  @Override
  public Task getSessionAsync(String sessionToken) {
    EasimartRESTSessionCommand command =
        EasimartRESTSessionCommand.getCurrentSessionCommand(sessionToken);

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

  @Override
  public Task revokeAsync(String sessionToken) {
    return EasimartRESTSessionCommand.revoke(sessionToken)
        .executeAsync(client)
        .makeVoid();
  }

  @Override
  public Task upgradeToRevocable(String sessionToken) {
    EasimartRESTSessionCommand command =
        EasimartRESTSessionCommand.upgradeToRevocableSessionCommand(sessionToken);
    return command.executeAsync(client).onSuccess(new Continuation() {
      @Override
      public EasimartObject.State then(Task task) throws Exception {
        JSONObject result = task.getResult();
        return coder.decode(new EasimartObject.State.Builder("_Session"), result, EasimartDecoder.get())
            .isComplete(true)
            .build();
      }
    });
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy