DDPClientWraper.java 3.11 KB
Newer Older
Yusuke Iwaki's avatar
Yusuke Iwaki committed
1
package chat.rocket.android.api;
2

3
import android.support.annotation.Nullable;
4 5
import bolts.Task;
import chat.rocket.android.helper.OkHttpHelper;
6
import chat.rocket.android.helper.TextUtils;
7 8
import chat.rocket.android_ddp.DDPClient;
import chat.rocket.android_ddp.DDPClientCallback;
9
import chat.rocket.android_ddp.DDPSubscription;
10 11
import java.util.UUID;
import org.json.JSONArray;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
12
import org.json.JSONException;
13
import rx.Observable;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
14
import timber.log.Timber;
15

16
/**
17
 * DDP client wrapper.
18
 */
19
public class DDPClientWraper {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
20 21
  private final DDPClient ddpClient;
  private final String hostname;
22

23
  private DDPClientWraper(String hostname) {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
24 25
    ddpClient = new DDPClient(OkHttpHelper.getClientForWebSocket());
    this.hostname = hostname;
26
  }
27

28 29 30
  /**
   * create new API client instance.
   */
31 32
  public static DDPClientWraper create(String hostname) {
    return new DDPClientWraper(hostname);
33
  }
34

35 36 37
  /**
   * Connect to WebSocket server with DDP client.
   */
38 39
  public Task<DDPClientCallback.Connect> connect(@Nullable String session) {
    return ddpClient.connect("wss://" + hostname + "/websocket", session);
40
  }
41

42 43 44 45
  /**
   * Returns whether DDP client is connected to WebSocket server.
   */
  public boolean isConnected() {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
46
    return ddpClient.isConnected();
47
  }
48

49 50 51 52
  /**
   * close connection.
   */
  public void close() {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
53
    ddpClient.close();
54
  }
55

56 57 58 59
  /**
   * Subscribe with DDP client.
   */
  public Task<DDPSubscription.Ready> subscribe(final String name, JSONArray param) {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
60
    return ddpClient.sub(UUID.randomUUID().toString(), name, param);
61
  }
62

63 64 65 66
  /**
   * Unsubscribe with DDP client.
   */
  public Task<DDPSubscription.NoSub> unsubscribe(final String subscriptionId) {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
67
    return ddpClient.unsub(subscriptionId);
68
  }
69

70 71 72 73
  /**
   * Returns Observable for handling DDP subscription.
   */
  public Observable<DDPSubscription.Event> getSubscriptionCallback() {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
74
    return ddpClient.getSubscriptionCallback();
75
  }
Yusuke Iwaki's avatar
Yusuke Iwaki committed
76 77 78 79 80

  private String generateId(String method) {
    return method + "-" + UUID.randomUUID().toString().replace("-", "");
  }

81 82 83
  /**
   * Execute raw RPC.
   */
84 85
  public Task<DDPClientCallback.RPC> rpc(String methodCallId, String methodName, String params,
      long timeoutMs) {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
86
    Timber.d("rpc:[%s]> %s(%s) timeout=%d", methodCallId, methodName, params, timeoutMs);
87
    if (TextUtils.isEmpty(params)) {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
88 89 90 91 92 93 94 95
      return ddpClient.rpc(methodName, null, methodCallId, timeoutMs).continueWithTask(task -> {
        if (task.isFaulted()) {
          Timber.d("rpc:[%s]< error = %s", methodCallId, task.getError());
        } else {
          Timber.d("rpc:[%s]< result = %s", methodCallId, task.getResult().result);
        }
        return task;
      });
96 97 98
    }

    try {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
99 100 101 102 103 104 105 106 107
      return ddpClient.rpc(methodName, new JSONArray(params), methodCallId, timeoutMs)
          .continueWithTask(task -> {
            if (task.isFaulted()) {
              Timber.d("rpc:[%s]< error = %s", methodCallId, task.getError());
            } else {
              Timber.d("rpc:[%s]< result = %s", methodCallId, task.getResult().result);
            }
            return task;
          });
108 109 110 111
    } catch (JSONException exception) {
      return Task.forError(exception);
    }
  }
112
}