Skip to content

Commit

Permalink
Implement Push.Admin.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcard committed Feb 18, 2017
1 parent fa0a812 commit 7759ba3
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
142 changes: 142 additions & 0 deletions android/src/main/java/io/ably/lib/rest/Push.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
import io.ably.lib.types.Param;
import io.ably.lib.types.RegistrationToken;
import io.ably.lib.types.ErrorInfo;
import io.ably.lib.types.PaginatedResult;
import io.ably.lib.types.AsyncPaginatedResult;
import io.ably.lib.http.PaginatedQuery;
import io.ably.lib.http.AsyncPaginatedQuery;
import io.ably.lib.util.Log;
import io.ably.lib.util.Serialisation;
import io.ably.lib.util.IntentUtils;
import io.ably.lib.util.StringUtils;
import android.content.SharedPreferences;
import android.content.Context;
import android.content.Intent;
Expand All @@ -32,6 +37,7 @@
public class Push {
public Push(AblyRest rest) {
this.rest = rest;
this.admin = new Admin(rest);
}

public void publish(Param[] recipient, JsonObject payload) throws AblyException {
Expand Down Expand Up @@ -88,6 +94,141 @@ public void onNewRegistrationToken(Context context, RegistrationToken.Type type,
getStateMachine(context).handleEvent(new ActivationStateMachine.GotPushDeviceDetails());
}

public static class Admin {
public final DeviceRegistrations deviceRegistrations;
public final ChannelSubscriptions channelSubscriptions;

Admin(AblyRest rest) {
this.deviceRegistrations = new DeviceRegistrations(rest);
this.channelSubscriptions = new ChannelSubscriptions(rest);
}
}

public static class DeviceRegistrations {
public DeviceDetails save(DeviceDetails device) throws AblyException {
Http.RequestBody body = rest.http.requestBodyFromGson(device.toJsonObject());
return rest.http.put("/push/deviceRegistrations/" + device.id, HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), null, body, DeviceDetails.httpResponseHandler);
}

public void saveAsync(DeviceDetails device, final Callback<DeviceDetails> callback) {
Http.RequestBody body = rest.http.requestBodyFromGson(device.toJsonObject());
rest.asyncHttp.put("/push/deviceRegistrations/" + device.id, HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), null, body, DeviceDetails.httpResponseHandler, callback);
}

public PaginatedResult<DeviceDetails> get(Param[] params) throws AblyException {
return new PaginatedQuery<DeviceDetails>(rest.http, "/push/deviceRegistrations", HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), params, DeviceDetails.httpBodyHandler).get();
}

public void getAsync(Param[] params, Callback<AsyncPaginatedResult<DeviceDetails>> callback) throws AblyException {
new AsyncPaginatedQuery<DeviceDetails>(rest.asyncHttp, "/push/deviceRegistrations", HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), params, DeviceDetails.httpBodyHandler).get(callback);
}

public void remove(Param[] params) throws AblyException {
rest.http.del("/push/deviceRegistrations", HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), null, null);
}

public void removeAsync(Param[] params, CompletionListener listener) throws AblyException {
rest.asyncHttp.del("/push/deviceRegistrations", HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), null, null, new CompletionListener.ToCallback(listener));
}

DeviceRegistrations(AblyRest rest) {
this.rest = rest;
}

private final AblyRest rest;
}

public static class ChannelSubscriptions {
public void save(ChannelSubscription subscription) throws AblyException {
Http.RequestBody body = rest.http.requestBodyFromGson(subscription.toJsonObject());
rest.http.post("/push/channelSubscriptions", HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), null, body, ChannelSubscription.httpResponseHandler);
}

public void saveAsync(ChannelSubscription subscription, final Callback<ChannelSubscription> callback) {
Http.RequestBody body = rest.http.requestBodyFromGson(subscription.toJsonObject());
rest.asyncHttp.put("/push/channelSubscriptions", HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), null, body, ChannelSubscription.httpResponseHandler, callback);
}

public PaginatedResult<ChannelSubscription> get(Param[] params) throws AblyException {
return new PaginatedQuery<ChannelSubscription>(rest.http, "/push/channelSubscriptions", HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), params, ChannelSubscription.httpBodyHandler).get();
}

public void getAsync(Param[] params, Callback<AsyncPaginatedResult<ChannelSubscription>> callback) throws AblyException {
new AsyncPaginatedQuery<ChannelSubscription>(rest.asyncHttp, "/push/channelSubscriptions", HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), params, ChannelSubscription.httpBodyHandler).get(callback);
}

public void remove(Param[] params) throws AblyException {
rest.http.del("/push/channelSubscriptions", HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), null, null);
}

public void removeAsync(Param[] params, CompletionListener listener) throws AblyException {
rest.asyncHttp.del("/push/channelSubscriptions", HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), null, null, new CompletionListener.ToCallback(listener));
}

public PaginatedResult<String> listChannels(Param[] params) throws AblyException {
return new PaginatedQuery<String>(rest.http, "/push/channelSubscriptions", HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), params, StringUtils.httpBodyHandler).get();
}

public void listChannelsAsync(Param[] params, Callback<AsyncPaginatedResult<String>> callback) throws AblyException {
new AsyncPaginatedQuery<String>(rest.asyncHttp, "/push/channelSubscriptions", HttpUtils.defaultAcceptHeaders(rest.options.useBinaryProtocol), params, StringUtils.httpBodyHandler).get(callback);
}

ChannelSubscriptions(AblyRest rest) {
this.rest = rest;
}

private final AblyRest rest;
}

public static class ChannelSubscription {
public final String channel;
public final String deviceId;
public final String clientId;

public static ChannelSubscription forDevice(String channel, String deviceId) {
return new ChannelSubscription(channel, deviceId, null);
}

public static ChannelSubscription forClientId(String channel, String clientId) {
return new ChannelSubscription(channel, null, clientId);
}

private ChannelSubscription(String channel, String deviceId, String clientId) {
this.channel = channel;
this.deviceId = deviceId;
this.clientId = clientId;
}

public JsonObject toJsonObject() {
JsonObject o = new JsonObject();

o.addProperty("channel", channel);
if (clientId != null) {
o.addProperty("clientId", clientId);
}
if (deviceId != null) {
o.addProperty("deviceId", deviceId);
}

return o;
}

public static ChannelSubscription fromJsonObject(JsonObject o) {
return Serialisation.gson.fromJson(o, ChannelSubscription.class);
}

private static Serialisation.FromJsonElement<ChannelSubscription> fromJsonElement = new Serialisation.FromJsonElement<ChannelSubscription>() {
@Override
public ChannelSubscription fromJsonElement(JsonElement e) {
return fromJsonObject((JsonObject) e);
}
};

protected static Http.ResponseHandler<ChannelSubscription> httpResponseHandler = new Serialisation.HttpResponseHandler<ChannelSubscription>(ChannelSubscription.class, fromJsonElement);

protected static Http.BodyHandler<ChannelSubscription> httpBodyHandler = new Serialisation.HttpBodyHandler<ChannelSubscription>(ChannelSubscription[].class, fromJsonElement);
}

/**
* ActivationStateMachine reacts to events from the user (calls to activate, deactivate, and
* their optional callbacks), from services (G/FCM registration tokens) and from its internal
Expand Down Expand Up @@ -562,6 +703,7 @@ private static class PersistKeys {
}

private final AblyRest rest;
private final Admin admin;

private synchronized ActivationStateMachine getStateMachine(Context context) {
if (ActivationStateMachine.INSTANCE == null) {
Expand Down
19 changes: 19 additions & 0 deletions lib/src/main/java/io/ably/lib/rest/DeviceDetails.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package io.ably.lib.rest;

import com.google.gson.JsonObject;
import com.google.gson.JsonElement;

import io.ably.lib.types.ErrorInfo;
import io.ably.lib.types.AblyException;
import io.ably.lib.http.Http;
import io.ably.lib.util.Serialisation;

/**
* Created by tcard on 3/2/17.
Expand Down Expand Up @@ -63,4 +67,19 @@ public JsonObject pushMetadataJsonObject() {
push.add("metadata", this.push.metadata);
return json;
}

public static DeviceDetails fromJsonObject(JsonObject o) {
return Serialisation.gson.fromJson(o, DeviceDetails.class);
}

private static Serialisation.FromJsonElement<DeviceDetails> fromJsonElement = new Serialisation.FromJsonElement<DeviceDetails>() {
@Override
public DeviceDetails fromJsonElement(JsonElement e) {
return fromJsonObject((JsonObject) e);
}
};

protected static Http.ResponseHandler<DeviceDetails> httpResponseHandler = new Serialisation.HttpResponseHandler<DeviceDetails>(DeviceDetails.class, fromJsonElement);

protected static Http.BodyHandler<DeviceDetails> httpBodyHandler = new Serialisation.HttpBodyHandler<DeviceDetails>(DeviceDetails[].class, fromJsonElement);
}
17 changes: 17 additions & 0 deletions lib/src/main/java/io/ably/lib/util/StringUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.ably.lib.util;

import com.google.gson.JsonElement;
import io.ably.lib.http.Http;

public class StringUtils {
public static Serialisation.FromJsonElement<String> fromJsonElement = new Serialisation.FromJsonElement<String>() {
@Override
public String fromJsonElement(JsonElement e) {
return e.getAsJsonPrimitive().getAsString();
}
};

public static Http.ResponseHandler<String> httpResponseHandler = new Serialisation.HttpResponseHandler<String>(String.class, fromJsonElement);

public static Http.BodyHandler<String> httpBodyHandler = new Serialisation.HttpBodyHandler<String>(String[].class, fromJsonElement);
}

0 comments on commit 7759ba3

Please sign in to comment.