|
| 1 | +package io.ably.pubsub.device; |
| 2 | + |
| 3 | +import io.ably.lib.realtime.AblyRealtime; |
| 4 | +import io.ably.lib.types.AblyException; |
| 5 | +import io.ably.lib.types.ClientOptions; |
| 6 | +import io.ably.pubsub.internal.Side; |
| 7 | + |
| 8 | +/** |
| 9 | + * The door into Ably Pub/Sub for devices: Android apps and other end-user runtimes. |
| 10 | + * <p> |
| 11 | + * Clients built here declare themselves device-side to Ably: every connection and request |
| 12 | + * they make carries the {@code ably-pubsub-device} agent entry, which is how the platform |
| 13 | + * classifies the traffic (on MAU-priced accounts, device traffic is what is counted). The |
| 14 | + * side is the package's to declare — a caller-supplied agent entry cannot override it. |
| 15 | + * <p> |
| 16 | + * There is one door: a device holds one live client. Connectionless operations (history, |
| 17 | + * presence reads, token requests) are all available on it. |
| 18 | + * <p> |
| 19 | + * This builder is the only recommended entry point of this artifact; the classes it |
| 20 | + * constructs come from {@code io.ably.pubsub:core-android}, which is an internal |
| 21 | + * implementation artifact not intended for direct use. |
| 22 | + */ |
| 23 | +public final class PubSubDevice { |
| 24 | + private PubSubDevice() {} |
| 25 | + |
| 26 | + /** |
| 27 | + * Returns a builder for the device's client. |
| 28 | + * |
| 29 | + * @param options a {@link ClientOptions} object to configure the client. |
| 30 | + * @return the builder. |
| 31 | + */ |
| 32 | + public static ClientBuilder clientBuilder(ClientOptions options) { |
| 33 | + return new ClientBuilder(options, null); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns a builder for the device's client. |
| 38 | + * |
| 39 | + * @param keyOrToken an Ably API key or token string. |
| 40 | + * @return the builder. |
| 41 | + */ |
| 42 | + public static ClientBuilder clientBuilder(String keyOrToken) { |
| 43 | + return new ClientBuilder(null, keyOrToken); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Builds the device client. Accepts everything the core constructor accepts. |
| 48 | + */ |
| 49 | + public static final class ClientBuilder { |
| 50 | + private final ClientOptions options; |
| 51 | + private final String keyOrToken; |
| 52 | + |
| 53 | + private ClientBuilder(ClientOptions options, String keyOrToken) { |
| 54 | + this.options = options; |
| 55 | + this.keyOrToken = keyOrToken; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Constructs the client, declaring the device side on it. |
| 60 | + * |
| 61 | + * @return the client. |
| 62 | + * @throws AblyException if the options, key or token are rejected. |
| 63 | + */ |
| 64 | + public AblyRealtime build() throws AblyException { |
| 65 | + final ClientOptions stamped; |
| 66 | + if (keyOrToken != null) { |
| 67 | + stamped = Side.optionsWithSideAgent(keyOrToken, Side.DEVICE_AGENT_IDENTIFIER, BuildConfig.VERSION); |
| 68 | + } else { |
| 69 | + stamped = Side.optionsWithSideAgent(options, Side.DEVICE_AGENT_IDENTIFIER, BuildConfig.VERSION); |
| 70 | + } |
| 71 | + return new AblyRealtime(stamped); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments