public async Task <HttpResponseMessage> SendCurrency(IList <CurrencyResponse> currencyList) { var uri = _dopplerSapServiceSettings.CurrencyEndpoint; _logger.LogInformation("Building http request with url {uri}.", uri); var httpRequest = new HttpRequestMessage { RequestUri = new Uri(uri), Method = new HttpMethod("POST") }; var requestContent = SafeJsonConvert.SerializeObject(currencyList, _serializationSettings); httpRequest.Content = new StringContent(requestContent, Encoding.UTF8); httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); var httpResponse = new HttpResponseMessage(); try { var jwtToken = _jwtTokenGenerator.CreateJwtToken(); _logger.LogInformation("Sending request to Doppler SAP Api."); var client = _httpClientFactory.CreateClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken); httpResponse = await client.SendAsync(httpRequest).ConfigureAwait(false); } catch (Exception e) { _logger.LogError(e, "Error occurred trying to send information to SAP return http {code}.", httpResponse.StatusCode); throw; } return(httpResponse); }
public async Task <IList <CurrencyResponse> > GetCurrencyByCode() { var cstZone = TimeZoneInfo.FindSystemTimeZoneById(_jobConfig.TimeZoneJobs); var returnList = new List <CurrencyResponse>(); foreach (var currencyCode in _dopplerCurrencySettings.CurrencyCodeList) { try { var cstTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, cstZone); var jwtToken = _jwtTokenGenerator.CreateJwtToken(); var httpResponse = await GetCurrencyValue(currencyCode, cstTime, jwtToken); var jsonResult = await httpResponse.Content.ReadAsStringAsync(); if (httpResponse.IsSuccessStatusCode) { var result = JsonConvert.DeserializeObject <CurrencyResponse>(jsonResult); if (result.CotizationAvailable) { returnList.Add(result); } else { _logger.LogError("{ReasonPhrase}. Error getting currency for {currencyCode}.", httpResponse.ReasonPhrase, currencyCode); // Get the most recent previous price result = await GetPreviousPrices(cstTime, currencyCode); if (result == null) { _logger.LogError("{ReasonPhrase}. Error getting currency for {currencyCode} after trying for the last 5 business days", httpResponse.ReasonPhrase, currencyCode); } else { returnList.Add(result); } } } else { _logger.LogError("{ReasonPhrase}. Error getting currency for {currencyCode}.", httpResponse.ReasonPhrase, currencyCode); } } catch (Exception e) { _logger.LogError(e, "Unexpected error getting currency for {currencyCode}.", currencyCode); throw; } } return(returnList); }