/// <summary> /// set requests headers and removes them prior to adding if they're already present. /// </summary> /// <param name="request">request that is modified (pass by ref)</param> /// <param name="token">access token</param> /// <param name="apiKey">subscription key for azure</param> /// <param name="backendId">ID of the backend</param> /// <returns></returns> protected void AddHeaders(ref HttpRequestMessage request, string token, string apiKey, BOBackendId backendId) { _logger.LogDebug(nameof(AddHeaders)); if (request.Headers.Contains(HeaderNames.Auth.Authorization)) { _logger.LogDebug($"Removing header '{HeaderNames.Auth.Authorization}'"); request.Headers.Remove(HeaderNames.Auth.Authorization); } request.Headers.Add(HeaderNames.Auth.Authorization, "Bearer " + token); if (request.Headers.Contains(HeaderNames.Auth.HfAuthorization)) { _logger.LogDebug($"Removing header '{HeaderNames.Auth.HfAuthorization}'"); request.Headers.Remove(HeaderNames.Auth.HfAuthorization); } request.Headers.Add(HeaderNames.Auth.HfAuthorization, "Bearer " + token); if (request.Headers.Contains(HeaderNames.Azure.SUBSCRIPTION_KEY)) { _logger.LogDebug($"Removing header '{HeaderNames.Azure.SUBSCRIPTION_KEY}'"); request.Headers.Remove(HeaderNames.Azure.SUBSCRIPTION_KEY); } if (!string.IsNullOrEmpty(apiKey)) { _logger.LogDebug($"Adding header '{HeaderNames.Azure.SUBSCRIPTION_KEY}'"); request.Headers.Add(HeaderNames.Azure.SUBSCRIPTION_KEY, apiKey); } if (request.Headers.Contains(HeaderNames.BACKEND_ID)) { _logger.LogDebug($"Removing header '{HeaderNames.BACKEND_ID}'"); request.Headers.Remove(HeaderNames.BACKEND_ID); } _logger.LogDebug($"Adding header '{HeaderNames.BACKEND_ID}'"); request.Headers.Add(HeaderNames.BACKEND_ID, backendId.ToString()); _logger.LogDebug($"Adding header 'Accept-Encoding'"); request.Headers.Add("Accept-Encoding", "gzip, deflate"); }
/// <summary> /// set requests headers and removes them prior to adding if they're already present. /// </summary> /// <param name="request">request that is modified (pass by ref)</param> /// <param name="token">access token</param> /// <param name="apiKey">subscription key for azure</param> /// <param name="backendId">ID of the backend</param> /// <returns></returns> protected void AddHeaders(ref HttpRequestMessage request, string token, string apiKey, BOBackendId backendId, string correlationId) { _logger.LogDebug(nameof(AddHeaders)); if (request.Headers.Contains(HeaderNames.Auth.Authorization)) { _logger.LogDebug($"Removing header '{HeaderNames.Auth.Authorization}'"); request.Headers.Remove(HeaderNames.Auth.Authorization); } request.Headers.Add(HeaderNames.Auth.Authorization, "Bearer " + token); if (request.Headers.Contains(HeaderNames.Auth.HfAuthorization)) { _logger.LogDebug($"Removing header '{HeaderNames.Auth.HfAuthorization}'"); request.Headers.Remove(HeaderNames.Auth.HfAuthorization); } request.Headers.Add(HeaderNames.Auth.HfAuthorization, "Bearer " + token); if (request.Headers.Contains(HeaderNames.Azure.SUBSCRIPTION_KEY)) { _logger.LogDebug($"Removing header '{HeaderNames.Azure.SUBSCRIPTION_KEY}'"); request.Headers.Remove(HeaderNames.Azure.SUBSCRIPTION_KEY); } if (!string.IsNullOrEmpty(apiKey)) { _logger.LogDebug($"Adding header '{HeaderNames.Azure.SUBSCRIPTION_KEY}'"); request.Headers.Add(HeaderNames.Azure.SUBSCRIPTION_KEY, apiKey); } if (request.Headers.Contains(HeaderNames.BACKEND_ID)) { _logger.LogDebug($"Removing header '{HeaderNames.BACKEND_ID}'"); request.Headers.Remove(HeaderNames.BACKEND_ID); } _logger.LogDebug($"Adding header '{HeaderNames.BACKEND_ID}'"); request.Headers.Add(HeaderNames.BACKEND_ID, backendId.ToString()); if (request.Headers.Contains("x-correlation-id")) { _logger.LogDebug($"Removing header 'x-correlation-id'"); request.Headers.Remove("x-correlation-id"); } if (!string.IsNullOrEmpty(correlationId)) { _logger.LogDebug($"Adding x-correlation-id header"); request.Headers.Add("x-correlation-id", correlationId); } }
/// <summary> /// /// </summary> /// <param name="urls"></param> /// <param name="lookupURL"></param> /// <param name="clientCertString"></param> /// <param name="apiKey"></param> /// <param name="backendId"></param> /// <returns></returns> /// <exception cref="HfException" >if Could not perform lookup and silentFailure is false</exception> public async Task <GenericLookupResult> RetrieveURLs(IList <Bo4eUri> urls, Uri lookupURL, string clientCertString, string apiKey, BOBackendId backendId, string correlationId = null) { if (string.IsNullOrWhiteSpace(clientCertString)) { _logger.LogWarning($"{nameof(clientCertString)} is initial: '{clientCertString}'"); } if (lookupURL == null) { _logger.LogCritical($"{nameof(lookupURL)} is null!"); } GenericLookupQuery urlObject = new GenericLookupQuery() { Uris = urls }; string requestBody = JsonConvert.SerializeObject(urlObject, new StringEnumConverter()); var request = new HttpRequestMessage() { Content = new StringContent(requestBody, System.Text.UTF8Encoding.UTF8, MIME_TYPE_JSON), RequestUri = lookupURL, Method = HttpMethod.Post }; if (request.Headers.Contains(HeaderNames.Auth.XArrClientCert)) { _logger.LogDebug($"Removing header '{HeaderNames.Auth.XArrClientCert}'"); request.Headers.Remove(HeaderNames.Auth.XArrClientCert); } request.Headers.Add(HeaderNames.Auth.XArrClientCert, clientCertString); if (request.Headers.Contains(HeaderNames.Azure.SUBSCRIPTION_KEY)) { _logger.LogDebug($"Removing header '{HeaderNames.Azure.SUBSCRIPTION_KEY}'"); request.Headers.Remove(HeaderNames.Azure.SUBSCRIPTION_KEY); } if (!string.IsNullOrEmpty(apiKey)) { _logger.LogDebug($"Adding {HeaderNames.Azure.SUBSCRIPTION_KEY} header"); request.Headers.Add(HeaderNames.Azure.SUBSCRIPTION_KEY, apiKey); } else { _logger.LogWarning($"{nameof(apiKey)} is initial: '{apiKey}'"); } if (request.Headers.Contains(HeaderNames.BACKEND_ID)) { _logger.LogDebug($"Removing {HeaderNames.BACKEND_ID} header"); request.Headers.Remove(HeaderNames.BACKEND_ID); } request.Headers.Add(HeaderNames.BACKEND_ID, backendId.ToString()); if (request.Headers.Contains("x-correlation-id")) { _logger.LogDebug($"Removing header 'x-correlation-id'"); request.Headers.Remove("x-correlation-id"); } if (!string.IsNullOrEmpty(correlationId)) { _logger.LogDebug($"Adding x-correlation-id header"); request.Headers.Add("x-correlation-id", correlationId); } var responseMessage = await httpClient.SendAsync(request); if (!responseMessage.IsSuccessStatusCode) { string responseContentPost = await responseMessage.Content.ReadAsStringAsync(); _logger.LogCritical($"Could not perform lookup: {responseMessage.ReasonPhrase} / {responseContentPost}; The original request was: {requestBody} POSTed to {lookupURL}"); if (_silentFailure) { return(null); } else { throw new HfException(responseMessage); } } string responseContent = await responseMessage.Content.ReadAsStringAsync(); GenericLookupResult resultObject = DeserializeObjectAndLog <GenericLookupResult>(responseContent); return(resultObject); }