Untitled
unknown
plain_text
20 days ago
9.5 kB
2
Indexable
Never
protected async Task<HttpResponseMessage> SendRequestToSapAsync(SapInputModel requestModel, int apiRetryCount = 0) { var loggerProperties = new Dictionary<string, string>(); try { var type = (apiRetryCount == -1) ? "Retried" : ((apiRetryCount == -2) ? "Cleared" : null); var sapData = new { GtsGuid = requestModel.Id, OperationType = requestModel.OperationType, requestModel.VehicleId, AwaitingCallback = false, requestModel.SiteId, TypeButton = type }; if (apiRetryCount == -1) { await this.sapRequestTrackingRepository.AddOrUpdateTrackingTable(sapData).ConfigureAwait(false); requestModel.Id = Guid.Empty; apiRetryCount = 0; } if (apiRetryCount == -2) { await this.sapRequestTrackingRepository.AddOrUpdateTrackingTable(sapData).ConfigureAwait(false); return new HttpResponseMessage(HttpStatusCode.Accepted); } CultureInfo cultureInfo = new CultureInfo("en-US"); loggerProperties[Core.Entities.Constants.ProjectPropertyName] = Core.Entities.Constants.GTSName; loggerProperties[Core.Entities.Constants.OperationTypePropertyName] = requestModel != null && requestModel.PairingStatus != null ? Convert.ToString(requestModel.PairingStatus, cultureInfo) : string.Empty; ArgumentValidators.ThrowIfNull(requestModel, nameof(requestModel)); this.logger.LogMessage( EventLevel.Informational, string.Empty, $"GTS Sap Integration-{requestModel.PairingStatus} has started {requestModel.Id} - {requestModel.VehicleId}"); var demoDummyUrl = Core.Entities.Constants.DemoDummyUrl; var provisioningDefaultSapConfig = new ProvisioningSapConfig { AuthenticationType = string.Empty, CallbackPath = demoDummyUrl, DeviceInstallRelativePath = demoDummyUrl, DeviceSwapRelativePath = demoDummyUrl, DeviceUninstallRelativePath = demoDummyUrl, SapBaseAddress = demoDummyUrl, ServiceBusAddress = demoDummyUrl, ServiceLevelUpdateToSapPath = demoDummyUrl, SiteSwapRelativePath = string.Empty, Disabled = true }; var provisioningSapConfig = await this.configurationHandler.GetConfigurationOrDefaultAsync(ConfigurationConstants.ProvisioningSapConfig, provisioningDefaultSapConfig).ConfigureAwait(false); this.logger.LogMessage( EventLevel.Informational, string.Empty, $"provisioningSapConfig Disabled -{provisioningSapConfig.Disabled}"); if (provisioningSapConfig.Disabled) { return new HttpResponseMessage(HttpStatusCode.Accepted); } requestModel.Id = requestModel.Id == Guid.Empty ? Guid.NewGuid() : requestModel.Id; loggerProperties[Core.Entities.Constants.GTSGuidPropertyName] = Convert.ToString(requestModel.Id, cultureInfo); requestModel.CallbackUrl = new Uri(provisioningSapConfig.CallbackPath); var authorizationUri = new Uri(provisioningSapConfig.SapNewBaseAddress + provisioningSapConfig.AuthorizationAddress); var apiUri = new Uri(provisioningSapConfig.SapNewBaseAddress + provisioningSapConfig.SapApiBaseAddress + provisioningSapConfig.ExtensionAddress); this.logger.LogMessage( EventLevel.Informational, string.Empty, $" GTS SAP Integration apiUri -{apiUri}"); var trackingData = new { GtsGuid = requestModel.Id, OperationType = requestModel.OperationType, InvokeDateTime = DateTime.UtcNow, Payload = JsonConvert.SerializeObject(requestModel), AwaitingCallback = false, requestModel.VehicleId, requestModel.SiteId }; loggerProperties[Core.Entities.Constants.OperationTypePropertyName] = Convert.ToString(trackingData.OperationType, cultureInfo); await this.sapRequestTrackingRepository.AddOrUpdateTrackingTable(trackingData).ConfigureAwait(false); using (var content = new StringContent(JsonConvert.SerializeObject(requestModel), Encoding.UTF8)) { var response = await this.sapClientFactory.SendToSapAsync(content, apiUri, authorizationUri).ConfigureAwait(false); string errorMessage = await GetResponseErrorMessage(response).ConfigureAwait(false); string retry; var awaitcallback = 0; if (!response.IsSuccessStatusCode && (int)response.StatusCode != 400 && apiRetryCount == 0) { retry = DateTime.UtcNow.AddMinutes(1).ToString("s", CultureInfo.InvariantCulture); } else if (!response.IsSuccessStatusCode && (int)response.StatusCode != 400 && apiRetryCount == 1) { retry = DateTime.UtcNow.AddMinutes(10).ToString("s", CultureInfo.InvariantCulture); } else { retry = null; } if (!response.IsSuccessStatusCode && (int)response.StatusCode != 400) { awaitcallback = 1; } else { awaitcallback = 0; } var responseData = new { GtsGuid = requestModel.Id, ApiResult = ((int)response.StatusCode).ToString(CultureInfo.InvariantCulture), AwaitingCallback = awaitcallback, ApiRetryDateTime = retry, ResponseErrorMessage = errorMessage }; await this.sapRequestTrackingRepository.AddOrUpdateTrackingTable(responseData).ConfigureAwait(false); if ((int)response.StatusCode == 400 || (apiRetryCount == 2 && !response.IsSuccessStatusCode)) { SapApiFailureFile sapApiFailureFile = new SapApiFailureFile(); sapApiFailureFile.OperationTypes = requestModel.OperationType; sapApiFailureFile.Status = errorMessage; sapApiFailureFile.VehicleId = requestModel.VehicleId; sapApiFailureFile.InvokeDateTime = DateTime.UtcNow; sapApiFailureFile.ApiResult = ((int)response.StatusCode).ToString(CultureInfo.InvariantCulture); sapApiFailureFile.Payload = JsonConvert.SerializeObject(requestModel); var emails = await this.configurationHandler.GetConfigurationAsync<SapApiFailureEmails>(ConfigurationConstants.SapApiFailureEmails).ConfigureAwait(false); var notificationCategory = NotificationCategory.SapApiUser; var notificationContext = this.notificationContextBuilder.HavingEntityId(NotificationConstants.ManufacturerId) .WithEntityType(EntityType.Manufacturer) .HavingLanguageId(NotificationConstants.LanguageId) .Build(); var emailContent = await this.emailBuilder.BuildSapApiFailuremailAsync(notificationContext, notificationCategory, sapApiFailureFile).ConfigureAwait(false); emailContent.ToAddresses = emails.ToAddresses; await this.notificationService.SendEmailNotificationAsync(EmailProviderType.SendGrid, emailContent, null, true).ConfigureAwait(false); } this.logger.LogMessage( EventLevel.Informational, string.Empty, $" GTS SAP Integration - {requestModel.OperationType} response received for {requestModel.Id} - Status code {response.StatusCode}"); return response; } } #pragma warning disable S2221 // "Exception" should not be caught when not required by called methods catch (Exception ex) #pragma warning restore S2221 // "Exception" should not be caught when not required by called methods { this.logger.LogMessage(EventLevel.Informational, string.Empty, $" GTS SAP integration failed - {ex.Message}"); this.logger.LogException(Core.Entities.Constants.SAPErrorTag, Core.Entities.Constants.SAPErrorTag + $" - GTS SAP integration failed, vehicle id - {requestModel.VehicleId}, {JsonConvert.SerializeObject(requestModel)}", ex, loggerProperties); } return new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError); }
Leave a Comment