public async Task WebhookTests_LiveSession()
        {
            const string TRIGGER1 = "FILE.PREVIEWED";
            const string TRIGGER2 = "FILE.DOWNLOADED";
            const string ADDRESS1 = "https://example1.com";
            const string ADDRESS2 = "https://example2.com";

            //first remove any dangling webhooks from previous failed tests
            var existingWebhooks = await _client.WebhooksManager.GetWebhooksAsync(autoPaginate : true);

            foreach (var wh in existingWebhooks.Entries)
            {
                await _client.WebhooksManager.DeleteWebhookAsync(wh.Id);
            }

            //create a new webhook on a file
            BoxRequestEntity target = new BoxRequestEntity()
            {
                Id = "16894937051", Type = BoxType.file
            };
            var triggers = new List <string>()
            {
                TRIGGER1
            };
            BoxWebhookRequest whr = new BoxWebhookRequest()
            {
                Target = target, Address = ADDRESS1, Triggers = triggers
            };
            var webhook = await _client.WebhooksManager.CreateWebhookAsync(whr);

            Assert.IsNotNull(webhook, "Failed to create webhook");
            Assert.AreEqual(TRIGGER1, webhook.Triggers.First(), "Webhook trigger does not match");
            Assert.AreEqual(ADDRESS1, webhook.Address, "Webhook address does not match");

            //get a webhook
            var fetchedWebhook = await _client.WebhooksManager.GetWebhookAsync(webhook.Id);

            Assert.AreEqual(fetchedWebhook.Id, webhook.Id, "Failed to get webhook");

            //update a webhook
            triggers = new List <string>()
            {
                TRIGGER1, TRIGGER2
            };
            whr = new BoxWebhookRequest()
            {
                Id = webhook.Id, Address = ADDRESS2, Triggers = triggers
            };
            var updatedWebhook = await _client.WebhooksManager.UpdateWebhookAsync(whr);

            Assert.IsTrue(updatedWebhook.Triggers.Contains(TRIGGER1), "Webhook trigger does not match");
            Assert.IsTrue(updatedWebhook.Triggers.Contains(TRIGGER2), "Webhook trigger does not match");
            Assert.AreEqual(ADDRESS2, updatedWebhook.Address, "Webhook address does not match");

            //delete a webhook
            var result = await _client.WebhooksManager.DeleteWebhookAsync(webhook.Id);

            Assert.IsTrue(result, "Failed to delete webhook");
        }
Beispiel #2
0
        /// <summary>
        /// Create a new webhook.
        /// </summary>
        /// <param name="webhookRequest">BoxWebhookRequest object.</param>
        /// <returns>Returns a webhook object if creation succeeds.</returns>
        public async Task <BoxWebhook> CreateWebhookAsync(BoxWebhookRequest webhookRequest)
        {
            BoxRequest request = new BoxRequest(_config.WebhooksUri)
                                 .Method(RequestMethod.Post)
                                 .Payload(_converter.Serialize(webhookRequest));

            IBoxResponse <BoxWebhook> response = await ToResponseAsync <BoxWebhook>(request).ConfigureAwait(false);

            return(response.ResponseObject);
        }
        private async Task RunCreate()
        {
            if (!string.IsNullOrEmpty(_path.Value()))
            {
                var json = false;
                if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting())
                {
                    json = true;
                }
                await base.CreateWebhooksFromFile(_path.Value(), this._save.HasValue(), json : json);

                return;
            }
            base.CheckForValue(this._id.Value, this._app, "A Box item ID is required for this call.");
            base.CheckForValue(this._type.Value, this._app, "A Box item type is required for this call.");
            base.CheckForValue(this._triggers.Value, this._app, "Triggers are required for this call.");
            base.CheckForValue(this._address.Value, this._app, "A webhook handler URL is required for this call.");

            var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value());

            var type           = base.ProcessType(this._type.Value);
            var webhookRequest = new BoxWebhookRequest();

            webhookRequest.Target = new BoxRequestEntity()
            {
                Id   = this._id.Value,
                Type = type
            };

            var triggerStrings = this._triggers.Value.Split(',');
            var triggers       = new List <string>();

            foreach (var trigger in triggerStrings)
            {
                triggers.Add(trigger.Trim().ToUpper());
            }

            webhookRequest.Triggers = triggers;
            webhookRequest.Address  = this._address.Value;
            var createdWebhook = await boxClient.WebhooksManager.CreateWebhookAsync(webhookRequest);

            if (this._idOnly.HasValue())
            {
                Reporter.WriteInformation(createdWebhook.Id);
                return;
            }
            if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting())
            {
                base.OutputJson(createdWebhook);
                return;
            }
            Reporter.WriteSuccess("Created new webhook...");
            base.PrintWebhook(createdWebhook);
        }
Beispiel #4
0
        /// <summary>
        /// Update a webhook.
        /// </summary>
        /// <param name="webhookRequest">BoxWebhookRequest object.</param>
        /// <returns>Returns the updated webhook object.</returns>
        public async Task <BoxWebhook> UpdateWebhookAsync(BoxWebhookRequest webhookRequest)
        {
            webhookRequest.ThrowIfNull("webhookRequest")
            .Id.ThrowIfNullOrWhiteSpace("webhookRequest.Id");

            BoxRequest request = new BoxRequest(_config.WebhooksUri, webhookRequest.Id)
                                 .Method(RequestMethod.Put)
                                 .Payload(_converter.Serialize(webhookRequest));

            IBoxResponse <BoxWebhook> response = await ToResponseAsync <BoxWebhook>(request).ConfigureAwait(false);

            return(response.ResponseObject);
        }
        private async Task RunUpdate()
        {
            if (!string.IsNullOrEmpty(_path.Value()))
            {
                var json = false;
                if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting())
                {
                    json = true;
                }
                await base.UpdateWebhooksFromFile(_path.Value(), this._save.HasValue(), json : json);

                return;
            }
            base.CheckForValue(this._id.Value, this._app, "A webhook ID is required for this call.");

            var boxClient      = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value());
            var webhookRequest = new BoxWebhookRequest();

            webhookRequest.Id = this._id.Value;
            if (this._triggers.HasValue())
            {
                var triggerStrings = this._triggers.Value().Split(',');
                var triggers       = new List <string>();
                foreach (var trigger in triggerStrings)
                {
                    triggers.Add(trigger.Trim().ToUpper());
                }
                webhookRequest.Triggers = triggers;
            }

            if (this._address.HasValue())
            {
                webhookRequest.Address = this._address.Value();
            }
            var webhook = await boxClient.WebhooksManager.UpdateWebhookAsync(webhookRequest);

            if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting())
            {
                base.OutputJson(webhook);
                return;
            }
            Reporter.WriteSuccess("Updated webhook...");
            base.PrintWebhook(webhook);
        }