Untitled
unknown
plain_text
21 days ago
3.7 kB
2
Indexable
Never
protected async Task<HttpResponseMessage> NotifyMDMAsync(string type, int manufacturerId, int siteId, string status, int partnerId, int retryCount = 0) { var loggerProperties = new Dictionary<string, string>(); try { // Define the payload structure based on type var payload = new { ManufacturerId = manufacturerId, SiteId = siteId, Status = status, PartnerId = partnerId }; // Log the initiation of the process this.logger.LogMessage(EventLevel.Informational, string.Empty, $"MDM Notification {type} started for ManufacturerId: {manufacturerId}, SiteId: {siteId}"); // Load API configuration from a config service or repository var mdmConfig = await this.configurationHandler.GetConfigurationOrDefaultAsync("MDMConfig", new MDMConfig()).ConfigureAwait(false); if (mdmConfig.Disabled) { return new HttpResponseMessage(HttpStatusCode.Accepted); } // Determine which API URI to use based on the type (telematics or privacy) Uri apiUri; if (type == "telematics") { // Telematics data collection API apiUri = new Uri(mdmConfig.MdmBaseAddress + mdmConfig.TelematicsEndpoint); } else if (type == "privacy") { // Privacy mode API apiUri = new Uri(mdmConfig.MdmBaseAddress + mdmConfig.PrivacyEndpoint); } else { throw new ArgumentException("Invalid type specified. Type must be either 'telematics' or 'privacy'."); } // Authorization URI var authorizationUri = new Uri(mdmConfig.AuthorizationAddress); // Track the data being sent to MDM var trackingData = new { ManufacturerId = manufacturerId, SiteId = siteId, Type = type, Status = status, InvokeDateTime = DateTime.UtcNow, Payload = JsonConvert.SerializeObject(payload) }; await this.mdmTrackingRepository.AddOrUpdateTrackingTable(trackingData).ConfigureAwait(false); // Prepare the HTTP content using (var content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8)) { // Send the request using an HTTP client, with retries if needed var response = await SendToMDMAsync(content, apiUri, authorizationUri, retryCount).ConfigureAwait(false); // Handle response logging and retry logic string errorMessage = await GetResponseErrorMessage(response).ConfigureAwait(false); if (!response.IsSuccessStatusCode && retryCount < 2) { // Log retry attempt this.logger.LogMessage(EventLevel.Warning, string.Empty, $"MDM {type} Notification failed. Retrying..."); // Retry after some time (retry logic can be adjusted) await Task.Delay(TimeSpan.FromMinutes(retryCount == 0 ? 1 : 10)); return await NotifyMDMAsync(type, manufacturerId, siteId, status, partnerId, retryCount + 1); } // Log the final response this.logger.LogMessage(EventLevel.Informational, string.Empty, $"MDM {type} Notification completed with status code {response.StatusCode}"); return response; } } catch (Exception ex) { // Log any exception that occurs this.logger.LogException("MDMError", $"MDM {type} Notification failed for ManufacturerId: {manufacturerId}, SiteId: {siteId}", ex, loggerProperties); return new HttpResponseMessage(HttpStatusCode.InternalServerError); } }
Leave a Comment