This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for subscription renewal
- Loading branch information
Showing
8 changed files
with
271 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
|
||
namespace GraphWebhooks.Helpers | ||
{ | ||
public class HttpHelper | ||
{ | ||
internal static async Task<HttpResponseMessage> SendAsync(string endpoint, HttpMethod httpMethod, object content = null) | ||
{ | ||
// Get an access token. | ||
string accessToken = await AuthHelper.GetAccessTokenAsync(); | ||
|
||
// Build the request. | ||
using (HttpClient client = new HttpClient()) | ||
{ | ||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); | ||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
|
||
// This sample subscribes to get notifications when the user receives an email. | ||
HttpRequestMessage request = new HttpRequestMessage(httpMethod, endpoint); | ||
|
||
if (content != null) | ||
{ | ||
string contentString = JsonConvert.SerializeObject(content, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); | ||
request.Content = new StringContent(contentString, System.Text.Encoding.UTF8, "application/json"); | ||
} | ||
|
||
return await client.SendAsync(request); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using GraphWebhooks.Models; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using System.Timers; | ||
using System.Web; | ||
|
||
namespace GraphWebhooks.Helpers | ||
{ | ||
public class SubscriptionCache | ||
{ | ||
static SubscriptionCache cache = null; | ||
|
||
Timer timer; | ||
private SubscriptionCache() | ||
{ | ||
// Renew subscriptions every 10 minute. | ||
Timer renewalTimer = new Timer(10 * 60 * 1000) | ||
{ | ||
AutoReset = false | ||
}; | ||
renewalTimer.Elapsed += OnRenewal; | ||
renewalTimer.Start(); | ||
this.timer = renewalTimer; | ||
} | ||
|
||
public static SubscriptionCache GetSubscriptionCache() | ||
{ | ||
if(cache != null) | ||
{ | ||
return cache; | ||
} | ||
|
||
cache = new SubscriptionCache(); | ||
return cache; | ||
} | ||
|
||
|
||
private async void OnRenewal(object sender, ElapsedEventArgs e) | ||
{ | ||
Dictionary<string, SubscriptionDetails> subscriptionstore = HttpRuntime.Cache.Get("subscription_store") as Dictionary<string, SubscriptionDetails>; | ||
|
||
foreach (var item in subscriptionstore) | ||
{ | ||
var response = await SubscriptionHelper.CheckSubscription(item.Key); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
await SubscriptionHelper.RenewSubscription(item.Key as string); | ||
} | ||
else | ||
{ | ||
await SubscriptionHelper.CreateSubscription(); | ||
} | ||
} | ||
|
||
timer.Start(); | ||
} | ||
|
||
|
||
// This sample temporarily stores the current subscription ID, client state, user object ID, and tenant ID. | ||
// This info is required so the NotificationController can retrieve an access token from the cache and validate the subscription. | ||
// Production apps typically use some method of persistent storage. | ||
public void SaveSubscriptionInfo(SubscriptionDetails subscriptionDetails) | ||
{ | ||
if (HttpRuntime.Cache["subscription_store"] == null) | ||
{ | ||
Dictionary<string, SubscriptionDetails> subscriptionstore = new Dictionary<string, SubscriptionDetails>(); | ||
subscriptionstore.Add(subscriptionDetails.SubscriptionId, subscriptionDetails); | ||
HttpRuntime.Cache.Add("subscription_store", | ||
subscriptionstore, | ||
null, DateTime.MaxValue, new TimeSpan(24, 0, 0), System.Web.Caching.CacheItemPriority.NotRemovable, null); | ||
} | ||
else | ||
{ | ||
Dictionary<string, SubscriptionDetails> subscriptionstore = HttpRuntime.Cache.Get("subscription_store") as Dictionary<string, SubscriptionDetails>; | ||
subscriptionstore.Add(subscriptionDetails.SubscriptionId, subscriptionDetails); | ||
} | ||
} | ||
|
||
public SubscriptionDetails GetSubscriptionInfo(string subscriptionId) | ||
{ | ||
Dictionary<string, SubscriptionDetails> subscriptionstore = HttpRuntime.Cache.Get("subscription_store") as Dictionary<string, SubscriptionDetails>; | ||
return subscriptionstore[subscriptionId]; | ||
} | ||
|
||
public Dictionary<string, SubscriptionDetails> DeleteAllSubscriptions() | ||
{ | ||
return HttpRuntime.Cache.Remove("subscription_store") as Dictionary<string, SubscriptionDetails>; | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. | ||
* See LICENSE in the source repository root for complete license information. | ||
*/ | ||
|
||
using System; | ||
using System.Web; | ||
|
||
namespace GraphWebhooks.Helpers | ||
{ | ||
public class SubscriptionDetails | ||
{ | ||
public string SubscriptionId { get; set; } | ||
public string ClientState { get; set; } | ||
public string UserId { get; set; } | ||
public string TenantId { get; set; } | ||
|
||
internal SubscriptionDetails(string subscriptionId, string clientState, string userId, string tenantId) | ||
{ | ||
SubscriptionId = subscriptionId; | ||
ClientState = clientState; | ||
UserId = userId; | ||
TenantId = tenantId; | ||
} | ||
} | ||
} |
Oops, something went wrong.