Search in sources :

Example 1 with Webhook

use of com.arm.mbed.cloud.sdk.connect.model.Webhook in project mbed-cloud-sdk-java by ARMmbed.

the class Connect method updateWebhook.

/**
 * Registers new webhook for incoming subscriptions.
 * <p>
 * Example:
 *
 * <pre>
 * {@code
 * try {
 *     Webhook webhook = new Webhook();
 *     webhook.setUrl("https://goo.gl/testwh");
 *     webhook.addHeader("Auth","token");
 *     connectApi.updateWebhook(webhook);
 * } catch (MbedCloudException e) {
 *     e.printStackTrace();
 * }
 * }
 * </pre>
 *
 * @param webhook
 *            Webhook to set.
 * @throws MbedCloudException
 *             if a problem occurred during request processing.
 */
@API
public void updateWebhook(@NonNull Webhook webhook) throws MbedCloudException {
    checkNotNull(webhook, TAG_WEBHOOK);
    checkModelValidity(webhook, TAG_WEBHOOK);
    if (isForceClear()) {
        stopNotifications();
    }
    final Webhook finalWebhook = webhook;
    CloudCaller.call(this, "updateWebhook()", null, new CloudCall<Void>() {

        @Override
        public Call<Void> call() {
            return endpoint.getNotifications().v2NotificationCallbackPut(WebhookAdapter.reverseMap(finalWebhook));
        }
    });
}
Also used : Call(retrofit2.Call) CloudCall(com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall) Webhook(com.arm.mbed.cloud.sdk.connect.model.Webhook) API(com.arm.mbed.cloud.sdk.annotations.API)

Example 2 with Webhook

use of com.arm.mbed.cloud.sdk.connect.model.Webhook in project mbed-cloud-sdk-java by ARMmbed.

the class ConnectExamples method setUpAWebhook.

/**
 * Sets up a webhook for notifications
 */
@Example
public void setUpAWebhook() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    // Telling the API to stop notification channel if already in use
    api.setForceClear(true);
    try {
        // Defining resource to listen to
        String resourcePath = "/5002/0/1";
        log("Resource path of interest", resourcePath);
        // Creating webhook.
        Webhook webhook = new Webhook(new URL("http://mbedcloudjavawebhooktest.requestcatcher.com/test"));
        log("Webhook", webhook);
        // Setting up webhook.
        api.updateWebhook(webhook);
        Thread.sleep(2000);
        // Adding subscription to all connected devices.
        Paginator<Device> connectedDevices = api.listAllConnectedDevices(null);
        for (Device connectedDevice : connectedDevices) {
            try {
                Resource resource = api.getResource(connectedDevice, resourcePath);
                if (resource != null) {
                    api.addResourceSubscription(resource);
                }
            } catch (Exception e1) {
                e1.printStackTrace();
                logError("An error occurred when trying to fetch Resource [" + resourcePath + "] on device: " + connectedDevice, api.getLastApiMetadata());
            }
        }
        // Waiting for notifications to be sent to the webhook.
        Thread.sleep(60000);
        // Deleting the webhook.
        deleteWebhook(api);
    } catch (Exception e) {
        e.printStackTrace();
        logError("last API Metadata", api.getLastApiMetadata());
        deleteWebhook(api);
        fail(e.getMessage());
    }
}
Also used : Device(com.arm.mbed.cloud.sdk.devicedirectory.model.Device) Connect(com.arm.mbed.cloud.sdk.Connect) Resource(com.arm.mbed.cloud.sdk.connect.model.Resource) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) Webhook(com.arm.mbed.cloud.sdk.connect.model.Webhook) URL(java.net.URL) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 3 with Webhook

use of com.arm.mbed.cloud.sdk.connect.model.Webhook in project mbed-cloud-sdk-java by ARMmbed.

the class Connect method startNotifications.

/**
 * Starts notification pull.
 * <p>
 * If an external callback is not set up (using `update_webhook`) then calling this function is mandatory to get or
 * set resources. Unless {@link ConnectionOptions#setAutostartDaemon(boolean)} has been set to true or left as
 * default.
 * <p>
 * Example:
 *
 * <pre>
 * {@code
 * connectApi.startNotifications();
 * }
 * </pre>
 *
 * @throws MbedCloudException
 *             if a problem occurred during the process.
 */
@API
@Daemon(task = "Notification pull", start = true)
public void startNotifications() throws MbedCloudException {
    Webhook webhook = null;
    try {
        if (isForceClear()) {
            deleteWebhook();
        }
    } catch (MbedCloudException exception) {
    // Nothing to do
    }
    try {
        webhook = getWebhook();
    } catch (MbedCloudException exception) {
    // Nothing to do
    }
    if (webhook != null) {
        logger.throwSdkException("A webhook is currently set up [" + webhook + "]. Notification pull cannot be used at the same time. Please remove the webhook if you want to use this mechanism instead.");
    }
    handlersStore.startNotificationPull();
}
Also used : MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) Webhook(com.arm.mbed.cloud.sdk.connect.model.Webhook) Daemon(com.arm.mbed.cloud.sdk.annotations.Daemon) API(com.arm.mbed.cloud.sdk.annotations.API)

Example 4 with Webhook

use of com.arm.mbed.cloud.sdk.connect.model.Webhook in project mbed-cloud-sdk-java by ARMmbed.

the class WebhookAdapter method map.

/**
 * Maps a webhook.
 *
 * @param apiWebhook
 *            webhook to map
 * @return a webhook
 */
public static Webhook map(com.arm.mbed.cloud.sdk.internal.mds.model.Webhook apiWebhook) {
    if (apiWebhook == null) {
        return null;
    }
    final Webhook webhook = new Webhook();
    webhook.setUrl(TranslationUtils.toUrl(apiWebhook.getUrl()));
    webhook.setHeaders(apiWebhook.getHeaders());
    return webhook;
}
Also used : Webhook(com.arm.mbed.cloud.sdk.connect.model.Webhook)

Example 5 with Webhook

use of com.arm.mbed.cloud.sdk.connect.model.Webhook in project mbed-cloud-sdk-java by ARMmbed.

the class ConnectExamples method switchNotificationChannel.

/**
 * Switches between notification channels.
 */
@Example
public void switchNotificationChannel() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    try {
        // Using Notification pull channel.
        api.startNotifications();
        Thread.sleep(5000);
        // Stopping notification pull channel.
        api.stopNotifications();
        Thread.sleep(100);
        // Starting a webhook channel.
        // Creating a webhook.
        Webhook webhook = new Webhook(new URL("http://mbedcloudjavawebhooktest.requestcatcher.com/test"));
        log("Webhook", webhook);
        // Setting up the webhook.
        api.updateWebhook(webhook);
        Thread.sleep(2000);
        // Stopping webhook channel.
        deleteWebhook(api);
        // Shutting down connect service.
        api.shutdownConnectService();
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            api.stopNotifications();
            Thread.sleep(100);
            api.shutdownConnectService();
            Thread.sleep(100);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        deleteWebhook(api);
    }
}
Also used : Connect(com.arm.mbed.cloud.sdk.Connect) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) Webhook(com.arm.mbed.cloud.sdk.connect.model.Webhook) URL(java.net.URL) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Aggregations

Webhook (com.arm.mbed.cloud.sdk.connect.model.Webhook)6 MbedCloudException (com.arm.mbed.cloud.sdk.common.MbedCloudException)4 Connect (com.arm.mbed.cloud.sdk.Connect)3 ConnectionOptions (com.arm.mbed.cloud.sdk.common.ConnectionOptions)3 URL (java.net.URL)3 AbstractExample (utils.AbstractExample)3 Example (utils.Example)3 API (com.arm.mbed.cloud.sdk.annotations.API)2 Daemon (com.arm.mbed.cloud.sdk.annotations.Daemon)1 CloudCall (com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall)1 Resource (com.arm.mbed.cloud.sdk.connect.model.Resource)1 Device (com.arm.mbed.cloud.sdk.devicedirectory.model.Device)1 Call (retrofit2.Call)1