Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new Microsoft Graph Subscription
        /// </summary>
        /// <param name="changeType">The event(s) the subscription should trigger on</param>
        /// <param name="notificationUrl">The URL that should be called when an event matching this subscription occurs</param>
        /// <param name="resource">The resource to monitor for changes. See https://docs.microsoft.com/graph/api/subscription-post-subscriptions#permissions for the list with supported options.</param>
        /// <param name="expirationDateTime">The datetime defining how long this subscription should stay alive before which it needs to get extended to stay alive. See https://docs.microsoft.com/graph/api/resources/subscription#maximum-length-of-subscription-per-resource-type for the supported maximum lifetime of the subscriber endpoints.</param>
        /// <param name="clientState">Specifies the value of the clientState property sent by the service in each notification. The maximum length is 128 characters. The client can check that the notification came from the service by comparing the value of the clientState property sent with the subscription with the value of the clientState property received with each notification.</param>
        /// <param name="latestSupportedTlsVersion">Specifies the latest version of Transport Layer Security (TLS) that the notification endpoint, specified by <paramref name="notificationUrl"/>, supports. If not provided, TLS 1.2 will be assumed.</param>
        /// <param name="accessToken">The OAuth 2.0 Access Token to use for invoking the Microsoft Graph</param>
        /// <param name="retryCount">Number of times to retry the request in case of throttling</param>
        /// <param name="delay">Milliseconds to wait before retrying the request. The delay will be increased (doubled) every retry</param>
        /// <returns>The just created Microsoft Graph subscription</returns>
        public static Model.Subscription CreateSubscription(Enums.GraphSubscriptionChangeType changeType, string notificationUrl, string resource, DateTimeOffset expirationDateTime, string clientState,
                                                            string accessToken, Enums.GraphSubscriptionTlsVersion latestSupportedTlsVersion = Enums.GraphSubscriptionTlsVersion.v1_2, int retryCount = 10, int delay = 500)
        {
            if (String.IsNullOrEmpty(notificationUrl))
            {
                throw new ArgumentNullException(nameof(notificationUrl));
            }

            if (String.IsNullOrEmpty(resource))
            {
                throw new ArgumentNullException(nameof(resource));
            }

            Model.Subscription result = null;

            try
            {
                // Use a synchronous model to invoke the asynchronous process
                result = Task.Run(async() =>
                {
                    var graphClient = GraphUtility.CreateGraphClient(accessToken, retryCount, delay);

                    // Prepare the subscription resource object
                    var newSubscription = new Subscription
                    {
                        ChangeType         = changeType.ToString().Replace(" ", ""),
                        NotificationUrl    = notificationUrl,
                        Resource           = resource,
                        ExpirationDateTime = expirationDateTime,
                        ClientState        = clientState
                    };

                    var subscription = await graphClient.Subscriptions
                                       .Request()
                                       .AddAsync(newSubscription);

                    if (subscription == null)
                    {
                        return(null);
                    }

                    var subscriptionModel = MapGraphEntityToModel(subscription);
                    return(subscriptionModel);
                }).GetAwaiter().GetResult();
            }
            catch (ServiceException ex)
            {
                Log.Error(Constants.LOGGING_SOURCE, CoreResources.GraphExtensions_ErrorOccured, ex.Error.Message);
                throw;
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Maps an entity returned by Microsoft Graph to its equivallent Model maintained within this library
        /// </summary>
        /// <param name="subscription">Microsoft Graph Subscription entity</param>
        /// <returns>Subscription Model</returns>
        private static Model.Subscription MapGraphEntityToModel(Subscription subscription)
        {
            var subscriptionModel = new Model.Subscription
            {
                Id                 = subscription.Id,
                ChangeType         = subscription.ChangeType.Split(',').Select(ct => (Enums.GraphSubscriptionChangeType)Enum.Parse(typeof(Enums.GraphSubscriptionChangeType), ct, true)).Aggregate((prev, next) => prev | next),
                NotificationUrl    = subscription.NotificationUrl,
                Resource           = subscription.Resource,
                ExpirationDateTime = subscription.ExpirationDateTime,
                ClientState        = subscription.ClientState
            };

            return(subscriptionModel);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Updates an existing Microsoft Graph Subscription
        /// </summary>
        /// <param name="subscriptionId">Unique identifier of the Microsoft Graph subscription</param>
        /// <param name="expirationDateTime">The datetime defining how long this subscription should stay alive before which it needs to get extended to stay alive. See https://docs.microsoft.com/graph/api/resources/subscription#maximum-length-of-subscription-per-resource-type for the supported maximum lifetime of the subscriber endpoints.</param>
        /// <param name="accessToken">The OAuth 2.0 Access Token to use for invoking the Microsoft Graph</param>
        /// <param name="retryCount">Number of times to retry the request in case of throttling</param>
        /// <param name="delay">Milliseconds to wait before retrying the request. The delay will be increased (doubled) every retry</param>
        /// <returns>The just updated Microsoft Graph subscription</returns>
        public static Model.Subscription UpdateSubscription(string subscriptionId, DateTimeOffset expirationDateTime,
                                                            string accessToken, int retryCount = 10, int delay = 500)
        {
            if (String.IsNullOrEmpty(subscriptionId))
            {
                throw new ArgumentNullException(nameof(subscriptionId));
            }

            Model.Subscription result = null;

            try
            {
                // Use a synchronous model to invoke the asynchronous process
                result = Task.Run(async() =>
                {
                    var graphClient = GraphUtility.CreateGraphClient(accessToken, retryCount, delay);

                    // Prepare the subscription resource object
                    var updatedSubscription = new Subscription
                    {
                        ExpirationDateTime = expirationDateTime
                    };

                    var subscription = await graphClient.Subscriptions[subscriptionId]
                                       .Request()
                                       .UpdateAsync(updatedSubscription);

                    if (subscription == null)
                    {
                        return(null);
                    }

                    var subscriptionModel = MapGraphEntityToModel(subscription);
                    return(subscriptionModel);
                }).GetAwaiter().GetResult();
            }
            catch (ServiceException ex)
            {
                Log.Error(Constants.LOGGING_SOURCE, CoreResources.GraphExtensions_ErrorOccured, ex.Error.Message);
                throw;
            }
            return(result);
        }