Search in sources :

Example 1 with UserStreamEvent

use of com.fitpay.android.api.models.UserStreamEvent in project fitpay-android-sdk by fitpay.

the class UserEventStream method getListener.

private ServerSentEvent.Listener getListener() {
    return new ServerSentEvent.Listener() {

        private int counter = 0;

        @Override
        public void onOpen(ServerSentEvent sse, Response response) {
            FPLog.d(TAG, "connected to event stream for user " + user.getId());
            connected = true;
        }

        @Override
        public void onMessage(ServerSentEvent sse, String id, String event, String message) {
            lastEventTs = System.currentTimeMillis();
            String payload = StringUtils.getDecryptedString(KeysManager.KEY_API, message);
            Gson gson = Constants.getGson();
            UserStreamEvent fitpayEvent = gson.fromJson(payload, UserStreamEvent.class);
            FPLog.d(TAG, "sse onMessage " + user.getId() + " received: " + fitpayEvent);
            RxBus.getInstance().post(fitpayEvent);
        }

        @Override
        public void onComment(ServerSentEvent sse, String comment) {
            FPLog.d(TAG, "sse onComment: " + comment);
        }

        @Override
        public boolean onRetryTime(ServerSentEvent sse, long milliseconds) {
            FPLog.d(TAG, "sse onRetryTime: " + milliseconds);
            return true;
        }

        @Override
        public boolean onRetryError(ServerSentEvent sse, Throwable throwable, Response response) {
            FPLog.e(TAG, "sse onRetryTime: " + response);
            FPLog.e(TAG, throwable);
            if (++counter <= 5) {
                FPLog.d(TAG, "still within retry parameters: " + counter + ", retrying sse connection");
                return true;
            } else {
                FPLog.d(TAG, "outside the retry parameters: " + counter + ", retrying sse connection");
                return false;
            }
        }

        @Override
        public void onClosed(ServerSentEvent sse) {
            FPLog.d(TAG, "event stream for user " + user.getId() + " closed");
        }

        @Override
        public Request onPreRetry(ServerSentEvent sse, Request originalRequest) {
            return null;
        }
    };
}
Also used : Response(okhttp3.Response) ServerSentEvent(com.here.oksse.ServerSentEvent) Request(okhttp3.Request) Gson(com.google.gson.Gson) UserStreamEvent(com.fitpay.android.api.models.UserStreamEvent)

Example 2 with UserStreamEvent

use of com.fitpay.android.api.models.UserStreamEvent in project fitpay-android-sdk by fitpay.

the class UserEventStreamSyncTest method testUserEventStreamSubscriptionProducesEvents.

@Test
public void testUserEventStreamSubscriptionProducesEvents() throws Exception {
    this.user = getUser();
    assertNotNull(user);
    createDevice(user, getTestDevice());
    // just to ensure events are going out, we'll just wait for the CREDITCARD_CREATED event
    final CountDownLatch eventLatch = new CountDownLatch(1);
    final List<UserStreamEvent> events = new ArrayList<>();
    NotificationManager.getInstance().addListener(new UserEventStreamListener() {

        @Override
        public void onUserEvent(UserStreamEvent event) {
            events.add(event);
            if ("CREDITCARD_CREATED".equals(event.getType())) {
                eventLatch.countDown();
            }
        }
    });
    UserEventStreamManager.subscribe(user.getId());
    CreditCard createdCard = createCreditCard(user, getTestCreditCard("9999504454545450"));
    final CountDownLatch latch = new CountDownLatch(1);
    createdCard.acceptTerms(new ApiCallback<CreditCard>() {

        @Override
        public void onSuccess(CreditCard result) {
            latch.countDown();
        }

        @Override
        public void onFailure(int errorCode, String errorMessage) {
            latch.countDown();
        }
    });
    eventLatch.await(30000, TimeUnit.MILLISECONDS);
    assertTrue(events.size() > 0);
}
Also used : ArrayList(java.util.ArrayList) UserStreamEvent(com.fitpay.android.api.models.UserStreamEvent) UserEventStreamListener(com.fitpay.android.api.sse.UserEventStreamListener) CountDownLatch(java.util.concurrent.CountDownLatch) CreditCard(com.fitpay.android.api.models.card.CreditCard) Test(org.junit.Test)

Example 3 with UserStreamEvent

use of com.fitpay.android.api.models.UserStreamEvent in project fitpay-android-sdk by fitpay.

the class WebViewCommunicatorImpl method getUserAndDevice.

private void getUserAndDevice(final String deviceId, final String callbackId) {
    ApiManager.getInstance().getUser(new ApiCallback<User>() {

        @Override
        public void onSuccess(User result) {
            if (result == null) {
                onTaskError(EventCallback.USER_CREATED, callbackId, "getUser failed: result is null");
                return;
            }
            WebViewCommunicatorImpl.this.user = result;
            RxBus.getInstance().post(new UserReceived(user.getId(), user.getUsername()));
            EventCallback eventCallback = new EventCallback.Builder().setCommand(EventCallback.USER_CREATED).setStatus(EventCallback.STATUS_OK).build();
            eventCallback.send();
            result.getDevice(deviceId, new ApiCallback<Device>() {

                @Override
                public void onSuccess(Device result) {
                    WebViewCommunicatorImpl.this.device = result;
                    String token = ApiManager.getPushToken();
                    String deviceToken = device.getNotificationToken();
                    final Runnable onSuccess = () -> onTaskSuccess(EventCallback.GET_USER_AND_DEVICE, callbackId);
                    boolean automaticallySubscribeToUserEventStream = true;
                    if (ApiManager.getConfig().containsKey(ApiManager.PROPERTY_AUTOMATICALLY_SUBSCRIBE_TO_USER_EVENT_STREAM)) {
                        automaticallySubscribeToUserEventStream = "true".equals(ApiManager.getConfig().get(ApiManager.PROPERTY_AUTOMATICALLY_SUBSCRIBE_TO_USER_EVENT_STREAM));
                    }
                    if (automaticallySubscribeToUserEventStream) {
                        try {
                            UserEventStreamManager.subscribe(user.getId());
                        } catch (IOException e) {
                            FPLog.e(e);
                        }
                        boolean automaticSyncThroughUserEventStream = true;
                        if (ApiManager.getConfig().containsKey(ApiManager.PROPERTY_AUTOMATICALLY_SYNC_FROM_USER_EVENT_STREAM)) {
                            automaticSyncThroughUserEventStream = "true".equals(ApiManager.getConfig().get(ApiManager.PROPERTY_AUTOMATICALLY_SYNC_FROM_USER_EVENT_STREAM));
                        }
                        if (automaticSyncThroughUserEventStream) {
                            userEventStreamSyncListener = new UserEventStreamListener() {

                                @Override
                                public void onUserEvent(UserStreamEvent event) {
                                    if ("SYNC".equals(event.getType())) {
                                        SyncInfo syncInfo = gson.fromJson(event.getPayload(), SyncInfo.class);
                                        syncInfo.setInitiator(SyncInitiator.PLATFORM);
                                        SyncRequest syncRequest = new SyncRequest.Builder().setSyncId(syncInfo.getSyncId()).setSyncInfo(syncInfo).setConnector(deviceService.getPaymentDeviceConnector()).setDevice(device).setUser(user).build();
                                        RxBus.getInstance().post(syncRequest);
                                    }
                                }
                            };
                            NotificationManager.getInstance().addListener(userEventStreamSyncListener);
                        }
                    }
                    if (deviceToken == null || !deviceToken.equals(token)) {
                        Device updatedDevice = new Device.Builder().setNotificationToken(token).build();
                        device.updateToken(updatedDevice, deviceToken == null, new ApiCallback<Device>() {

                            @Override
                            public void onSuccess(Device result) {
                                WebViewCommunicatorImpl.this.device = result;
                                onSuccess.run();
                            }

                            @Override
                            public void onFailure(@ResultCode.Code int errorCode, String errorMessage) {
                                onTaskError(EventCallback.GET_USER_AND_DEVICE, callbackId, "update device failed:" + errorMessage);
                            }
                        });
                    } else {
                        onSuccess.run();
                    }
                }

                @Override
                public void onFailure(@ResultCode.Code int errorCode, String errorMessage) {
                    onTaskError(EventCallback.GET_USER_AND_DEVICE, callbackId, "getDevice failed " + errorMessage);
                }
            });
        }

        @Override
        public void onFailure(@ResultCode.Code int errorCode, String errorMessage) {
            onTaskError(EventCallback.USER_CREATED, callbackId, "getUser failed " + errorMessage);
        }
    });
}
Also used : User(com.fitpay.android.api.models.user.User) ApiCallback(com.fitpay.android.api.callbacks.ApiCallback) Device(com.fitpay.android.api.models.device.Device) UserStreamEvent(com.fitpay.android.api.models.UserStreamEvent) UserEventStreamListener(com.fitpay.android.api.sse.UserEventStreamListener) EventCallback(com.fitpay.android.utils.EventCallback) IOException(java.io.IOException) SyncRequest(com.fitpay.android.paymentdevice.models.SyncRequest) NotificationSyncRequest(com.fitpay.android.paymentdevice.events.NotificationSyncRequest) SyncInfo(com.fitpay.android.paymentdevice.models.SyncInfo) UserReceived(com.fitpay.android.webview.events.UserReceived)

Aggregations

UserStreamEvent (com.fitpay.android.api.models.UserStreamEvent)3 UserEventStreamListener (com.fitpay.android.api.sse.UserEventStreamListener)2 ApiCallback (com.fitpay.android.api.callbacks.ApiCallback)1 CreditCard (com.fitpay.android.api.models.card.CreditCard)1 Device (com.fitpay.android.api.models.device.Device)1 User (com.fitpay.android.api.models.user.User)1 NotificationSyncRequest (com.fitpay.android.paymentdevice.events.NotificationSyncRequest)1 SyncInfo (com.fitpay.android.paymentdevice.models.SyncInfo)1 SyncRequest (com.fitpay.android.paymentdevice.models.SyncRequest)1 EventCallback (com.fitpay.android.utils.EventCallback)1 UserReceived (com.fitpay.android.webview.events.UserReceived)1 Gson (com.google.gson.Gson)1 ServerSentEvent (com.here.oksse.ServerSentEvent)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 Test (org.junit.Test)1