private static async Task <HttpResponseMessage> Subscribe(HttpRequestMessage req, CloudTable table, string eventName) { var jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string callbackUrl = data?.CallbackUrl; // create subscription record var subscription = new WebHookSubscription() { Id = Guid.NewGuid().ToString(), PartitionKey = eventName, CallbackUrl = callbackUrl }; // save in table var insert = TableOperation.Insert(subscription); await table.ExecuteAsync(insert); // return url in "Location" header var response = req.CreateResponse(HttpStatusCode.Created, "Web hook subscription successfully created"); response.Headers.Add("Location", Environment.GetEnvironmentVariable("FUNCTION_APP_DOMAIN", EnvironmentVariableTarget.Process) + "/api/Subscription/DELETE?code=" + Environment.GetEnvironmentVariable("SUBSCRIPTION_ROUTE_CODE", EnvironmentVariableTarget.Process) + "&id=" + subscription.Id + "&event=" + eventName); return(response); }
private static async Task <HttpResponseMessage> Unsubscribe(HttpRequestMessage req, CloudTable table) { var id = req.GetQueryNameValuePairs() .FirstOrDefault(q => string.Compare(q.Key, "id", StringComparison.OrdinalIgnoreCase) == 0) .Value; var eventName = req.GetQueryNameValuePairs() .FirstOrDefault(q => string.Compare(q.Key, "event", StringComparison.OrdinalIgnoreCase) == 0) .Value; var toDelete = new WebHookSubscription() { Id = id, PartitionKey = eventName, ETag = "*" }; var delete = TableOperation.Delete(toDelete); await table.ExecuteAsync(delete); return(req.CreateResponse(HttpStatusCode.OK, "Web hook subscription successfully deleted")); }