/// <summary> /// Runs the notification task. /// The task will continue to run indefinitely until the cancellationToken is canceled. /// </summary> /// <param name="connectorConfig"></param> /// <param name="logPrefix"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task RunAsync(ConnectorConfigModel connectorConfig, string logPrefix, CancellationToken cancellationToken) { // Set up properties that will be used consistently by multiple methods _connectorConfig = connectorConfig; _cancellationToken = cancellationToken; _logPrefix = logPrefix; _authenticationHelperSettings = AuthenticationHelperSettingsFactory(connectorConfig); while (!cancellationToken.IsCancellationRequested) { // Get all pending notifications var notifications = await NotificationPullManager.GetAllPendingConnectorNotifications( ApiClientFactorySettings, _authenticationHelperSettings, _connectorConfig.Id, _cancellationToken).ConfigureAwait(false); // Process all pending notifications foreach (var notification in notifications) { if (cancellationToken.IsCancellationRequested) { break; } await ProcessNotification(notification).ConfigureAwait(false); } // Wait the prescribed period of time before polling for more new Notifications await Task.Delay(TimeSpan.FromSeconds(NotificationRunnerSettings?.NotificationPollIntervalSeconds ?? NotificationRunnerSettings.DefaultNotificationPollIntervalSeconds)).ConfigureAwait(false); } }
/// <summary> /// Acknowledges a notification as having been processed. /// </summary> /// <param name="factorySettings"></param> /// <param name="authenticationSettings"></param> /// <param name="acknowledgement"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task AcknowledgeNotification(ApiClientFactorySettings factorySettings, AuthenticationHelperSettings authenticationSettings, ConnectorNotificationAcknowledgeModel acknowledgement, CancellationToken cancellationToken = default(CancellationToken)) { var client = _apiClientFactory.CreateApiClient(factorySettings); var policy = ApiClientRetryPolicy.GetPolicy(MaxRetryAttempts, cancellationToken); var acknowledgementResponse = await policy.ExecuteAsync( async() => { var authHelper = _apiClientFactory.CreateAuthenticationHelper(); var headers = await authHelper.GetHttpRequestHeaders(authenticationSettings).ConfigureAwait(false); var response = await client.ApiNotificationsPostWithHttpMessagesAsync(acknowledgement, customHeaders: headers, cancellationToken: cancellationToken).ConfigureAwait(false); return(response); } ).ConfigureAwait(false); if (acknowledgementResponse.Response.StatusCode == HttpStatusCode.NotFound) { throw new ResourceNotFoundException($"Notification with ID [{acknowledgement.NotificationId}] was not found. It may have already been acknowledged."); } // TODO: add more comprehensive handling of the response codes }
public async Task AcquireToken_DoesNotThrowNullException() { // These are auth credentials from the AAD app "ConnectorSDKTest" var authSettings = new AuthenticationHelperSettings() { AuthenticationResource = "api://f7aabf18-3ed3-41f2-af6a-bc0ba572cc22", TenantDomainName = "recordpoint.com", ClientId = "f7aabf18-3ed3-41f2-af6a-bc0ba572cc22", ClientSecret = MakeSecureString("2thu0/=15oE@dIfS/15W2Z4o[=C319-6") }; for (var i = 0; i < 3; i++) { var authHelper = new AuthenticationHelper(); var authHeader = await authHelper.GetHttpRequestHeaders(authSettings); Assert.NotNull(authHeader["Authorization"]); } }
/// <summary> /// Queries for all connector notifications for a given connector instance that are pending acknowledgement. /// </summary> /// <param name="factorySettings"></param> /// <param name="authenticationSettings"></param> /// <param name="connectorConfigId"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <IList <ConnectorNotificationModel> > GetAllPendingConnectorNotifications(ApiClientFactorySettings factorySettings, AuthenticationHelperSettings authenticationSettings, string connectorConfigId, CancellationToken cancellationToken = default(CancellationToken)) { // Get Notifications var client = _apiClientFactory.CreateApiClient(factorySettings); var policy = ApiClientRetryPolicy.GetPolicy(MaxRetryAttempts, cancellationToken); var notificationQueryResponse = await policy.ExecuteAsync( async (ct) => { var authHelper = _apiClientFactory.CreateAuthenticationHelper(); var headers = await authHelper.GetHttpRequestHeaders(authenticationSettings).ConfigureAwait(false); var response = await client.ApiNotificationsByConnectorIdGetWithHttpMessagesAsync( connectorConfigId, customHeaders: headers, cancellationToken: ct ).ConfigureAwait(false); return(response); }, cancellationToken ).ConfigureAwait(false); // TODO: add more comprehensive handling of the response codes var notifications = notificationQueryResponse.Body; return(notifications); }
/// <summary> /// Get Headers with Bearer Token /// </summary> /// <param name="authenticationHelper"></param> /// <param name="settings"></param> /// <returns></returns> public static async Task <Dictionary <string, List <string> > > GetHttpRequestHeaders(this IAuthenticationHelper authenticationHelper, AuthenticationHelperSettings settings) { var headers = new Dictionary <string, List <string> >(); var authenticationResult = await authenticationHelper .AcquireTokenAsync(settings) .ConfigureAwait(false); headers.AddAuthorizationHeader(authenticationResult.AccessTokenType, authenticationResult.AccessToken); return(headers); }
public Task <IList <ConnectorNotificationModel> > GetAllPendingConnectorNotifications(ApiClientFactorySettings factorySettings, AuthenticationHelperSettings authenticationSettings, string connectorConfigId, CancellationToken cancellationToken = default(CancellationToken)) { return(Task.FromResult(Dictionary.Values as IList <ConnectorNotificationModel>)); }
public Task AcknowledgeNotification(ApiClientFactorySettings factorySettings, AuthenticationHelperSettings authenticationSettings, ConnectorNotificationAcknowledgeModel acknowledgement, CancellationToken cancellationToken = default(CancellationToken)) { if (!Dictionary.TryRemove(acknowledgement.NotificationId, out var dummy)) { throw new ResourceNotFoundException(); } return(Task.FromResult(0)); }