Updating Hooks Samples
This commit is contained in:
Родитель
4b705b41ce
Коммит
07c6557c66
|
@ -21,10 +21,10 @@ namespace Microsoft.TeamServices.Samples.Client.ServiceHooks
|
|||
{
|
||||
|
||||
/// <summary>
|
||||
/// Create a new web hook subscription that triggers a notification on all new work items created in the specified project.
|
||||
/// Create and delete a new web hook subscription that triggers a notification on all new work items created in the specified project.
|
||||
/// </summary>
|
||||
[ClientSampleMethod]
|
||||
public Subscription CreateWebHooksSubscription()
|
||||
public Subscription CreateDeleteWebHooksSubscription()
|
||||
{
|
||||
// Get the project to create the subscription in
|
||||
TeamProjectReference project = ClientSampleHelpers.FindAnyProject(this.Context);
|
||||
|
@ -50,20 +50,114 @@ namespace Microsoft.TeamServices.Samples.Client.ServiceHooks
|
|||
};
|
||||
|
||||
Subscription newSubscription = serviceHooksClient.CreateSubscriptionAsync(subscriptionParameters).Result;
|
||||
Guid subscriptionId = newSubscription.Id;
|
||||
|
||||
LogSubscription(newSubscription);
|
||||
|
||||
// Delete the subscription
|
||||
serviceHooksClient.DeleteSubscriptionAsync(subscriptionId).SyncResult();
|
||||
|
||||
// Try to get the subscription (should result in an exception)
|
||||
try
|
||||
{
|
||||
newSubscription = serviceHooksClient.GetSubscriptionAsync(subscriptionId).Result;
|
||||
} catch (Exception e)
|
||||
{
|
||||
Context.Log("Unable to get the deleted subscription:" + e.Message);
|
||||
}
|
||||
|
||||
return newSubscription;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create a new web hook subscription that triggers a notification on all work item updates across the entire account (project collection).
|
||||
/// Returns all custom subscriptions for the caller.
|
||||
/// </summary>
|
||||
[ClientSampleMethod]
|
||||
public IEnumerable<Subscription> ListSubscriptions()
|
||||
{
|
||||
VssConnection connection = Context.Connection;
|
||||
ServiceHooksPublisherHttpClient serviceHooksClient = connection.GetClient<ServiceHooksPublisherHttpClient>();
|
||||
|
||||
IList<Subscription> subscriptions = serviceHooksClient.QuerySubscriptionsAsync().Result;
|
||||
|
||||
foreach(var subscription in subscriptions)
|
||||
{
|
||||
LogSubscription(subscription);
|
||||
}
|
||||
|
||||
return subscriptions;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create and deletes a new Release Management web hook subscription
|
||||
/// </summary>
|
||||
[ClientSampleMethod]
|
||||
public Subscription CreateDeleteRMWebHooksSubscription()
|
||||
{
|
||||
// Get the project to create the subscription in
|
||||
TeamProjectReference project = ClientSampleHelpers.FindAnyProject(this.Context);
|
||||
|
||||
// Get the client
|
||||
VssConnection connection = Context.Connection;
|
||||
ServiceHooksPublisherHttpClient serviceHooksClient = connection.GetClient<ServiceHooksPublisherHttpClient>();
|
||||
|
||||
// Get the list of publishers
|
||||
IList<VisualStudio.Services.ServiceHooks.WebApi.Publisher> publishers = serviceHooksClient.GetPublishersAsync().Result;
|
||||
|
||||
// Find the Release Management publisher and get its ServiceInstanceType
|
||||
VisualStudio.Services.ServiceHooks.WebApi.Publisher rmPublisher = publishers.First(p => p.Id == "rm");
|
||||
Guid rmServiceInstance = Guid.Parse(rmPublisher.ServiceInstanceType);
|
||||
|
||||
// Get a new client using the RM ServiceInstanceType
|
||||
ServiceHooksPublisherHttpClient rmServiceHooksClient = connection.GetClient<ServiceHooksPublisherHttpClient>(rmServiceInstance);
|
||||
|
||||
Subscription subscriptionParameters = new Subscription()
|
||||
{
|
||||
ConsumerId = "webHooks",
|
||||
ConsumerActionId = "httpRequest",
|
||||
ConsumerInputs = new Dictionary<string, string>
|
||||
{
|
||||
{ "url", "https://requestb.in/12h6lw21" }
|
||||
},
|
||||
EventType = "ms.vss-release.release-created-event",
|
||||
PublisherId = "rm",
|
||||
PublisherInputs = new Dictionary<string, string>
|
||||
{
|
||||
{ "projectId", project.Id.ToString() }
|
||||
},
|
||||
};
|
||||
Subscription newSubscription = rmServiceHooksClient.CreateSubscriptionAsync(subscriptionParameters).Result;
|
||||
Guid subscriptionId = newSubscription.Id;
|
||||
|
||||
LogSubscription(newSubscription);
|
||||
|
||||
// Delete the subscription
|
||||
rmServiceHooksClient.DeleteSubscriptionAsync(subscriptionId).SyncResult();
|
||||
|
||||
// Try to get the subscription (should result in an exception)
|
||||
try
|
||||
{
|
||||
newSubscription = rmServiceHooksClient.GetSubscriptionAsync(subscriptionId).Result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Context.Log("Unable to get the deleted subscription:" + e.Message);
|
||||
}
|
||||
|
||||
return newSubscription;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create and delete a new web hook subscription that triggers a notification on all work item updates across the entire account (project collection).
|
||||
///
|
||||
/// This requires account/collection level administrator permissions.
|
||||
///
|
||||
/// </summary>
|
||||
[ClientSampleMethod]
|
||||
public Subscription CreateAccountWideWebHooksSubscription()
|
||||
public Subscription CreateDeleteAccountWideWebHooksSubscription()
|
||||
{
|
||||
// Get the client
|
||||
VssConnection connection = Context.Connection;
|
||||
|
@ -82,9 +176,23 @@ namespace Microsoft.TeamServices.Samples.Client.ServiceHooks
|
|||
};
|
||||
|
||||
Subscription newSubscription = serviceHooksClient.CreateSubscriptionAsync(subscriptionParameters).Result;
|
||||
Guid subscriptionId = newSubscription.Id;
|
||||
|
||||
LogSubscription(newSubscription);
|
||||
|
||||
// Delete the subscription
|
||||
serviceHooksClient.DeleteSubscriptionAsync(subscriptionId).SyncResult();
|
||||
|
||||
// Try to get the subscription (should result in an exception)
|
||||
try
|
||||
{
|
||||
newSubscription = serviceHooksClient.GetSubscriptionAsync(subscriptionId).Result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Context.Log("Unable to get the deleted subscription:" + e.Message);
|
||||
}
|
||||
|
||||
return newSubscription;
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче