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));
}
});
}
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());
}
}
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();
}
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;
}
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);
}
}
Aggregations