diff --git a/Project/Services/HttpClientFactory.cs b/Project/Services/HttpClientFactory.cs index 323148d..8df834d 100644 --- a/Project/Services/HttpClientFactory.cs +++ b/Project/Services/HttpClientFactory.cs @@ -36,6 +36,7 @@ protected override async Task SendAsync(HttpRequestMessage logger.LogDebug($"Sending request to {request.RequestUri}"); var response = await base.SendAsync(request, cancellationToken); logger.LogDebug($"Received response from {request.RequestUri}"); + logger.LogDebug($"Response status code: {response.StatusCode}"); var traceparentHeader = response.Headers.TryGetValues(HttpHeaderUtilities.ICS_TRACE_HEADER, out var values) ? values.First() : "HEADER MISSING FROM RESPONSE"; logger.LogDebug($" ICS {HttpHeaderUtilities.ICS_TRACE_HEADER} header: {traceparentHeader}"); return response; diff --git a/Project/Services/WalmartCommerceService.cs b/Project/Services/WalmartCommerceService.cs index 0cea7cf..28bef66 100644 --- a/Project/Services/WalmartCommerceService.cs +++ b/Project/Services/WalmartCommerceService.cs @@ -127,22 +127,13 @@ public async Task GetLoginUrlAsync(IExecutionContext ctx, IGam fullUrl += $"?platform={platform}"; } - using var response = await _httpClient.GetAsync(fullUrl); - _logger.LogDebug("Calling Walmart ICS to retrieve login URL."); - _logger.LogDebug($"Request url: {fullUrl}"); - _logger.LogDebug($"Request headers: {_httpClient.DefaultRequestHeaders}"); - _logger.LogDebug($"Response status code: {response.StatusCode}"); - _logger.LogDebug($"Response body: {await response.Content.ReadAsStringAsync()}"); + + using var response = await _httpClient.GetAsync(fullUrl); if (!response.IsSuccessStatusCode) { _logger.LogError("Error in retrieving login URL from Walmart ICS."); - _logger.LogError($"Request url: {fullUrl}"); - _logger.LogError($"Request headers: {_httpClient.DefaultRequestHeaders}"); - _logger.LogError($"Response status code: {response.StatusCode}"); - _logger.LogError($"Response body: {await response.Content.ReadAsStringAsync()}"); - return new LoginUrlResponse([new GetLoginError()], string.Empty, []); } @@ -151,10 +142,6 @@ public async Task GetLoginUrlAsync(IExecutionContext ctx, IGam if (content == null || string.IsNullOrEmpty(content.LoginUrl)) { _logger.LogError("Error in retrieving login URL from Walmart ICS. Received null content."); - _logger.LogError($"Request url: {fullUrl}"); - _logger.LogError($"Request headers: {_httpClient.DefaultRequestHeaders}"); - _logger.LogError($"Response status code: {response.StatusCode}"); - return new LoginUrlResponse([new GetLoginError()], string.Empty, []); } @@ -190,29 +177,18 @@ public async Task GetAccountDetailsAsync(IExecutionConte var fullUrl = string.Format(ICS_GET_ACCOUNT_DETAILS_URL, icsHostname, isSandbox ? ICS_SANDBOX_URL_FRAGMENT : string.Empty, titleId); _httpClient.DefaultRequestHeaders.Add(WalmartAuthService.LCID_HEADER, lcid); - using var response = await _httpClient.GetAsync(fullUrl); - _logger.LogDebug("Calling Walmart ICS to get account details."); - _logger.LogDebug($"Request url: {fullUrl}"); - _logger.LogDebug($"Request headers: {_httpClient.DefaultRequestHeaders}"); - _logger.LogDebug($"Response status code: {response.StatusCode}"); - _logger.LogDebug($"Response body: {await response.Content.ReadAsStringAsync()}"); + + using var response = await _httpClient.GetAsync(fullUrl); if (!response.IsSuccessStatusCode) { _logger.LogError("Error in retrieving account details."); - _logger.LogError($"Request url: {fullUrl}"); - _logger.LogError($"Request headers: {_httpClient.DefaultRequestHeaders}"); - _logger.LogError($"Response status code: {response.StatusCode}"); - _logger.LogError($"Response body: {await response.Content.ReadAsStringAsync()}"); - var error = await MapErrors(response); return new AccountDetailsResponse([error ?? new AccountDetailsRequestError()], new AccountDetailsPayloadResponse(), []); } - _logger.LogInformation($"Response body from get-account-details: {await response.Content.ReadAsStringAsync()}"); - var content = await response.Content.ReadFromJsonAsync(); if (content == null || (content.Errors == null && content.Payload == null)) @@ -273,16 +249,13 @@ public async Task LinkAccountAsync(IExecutionContext ctx, I var requestContent = new StringContent(JsonSerializer.Serialize(linkRequest, _jsonOptions), Encoding.UTF8, "application/json"); + _logger.LogDebug("Calling Walmart ICS to link account."); + using var response = await _httpClient.PostAsync(fullUrl, requestContent); + if (!response.IsSuccessStatusCode) { - var message = $"Error in linking account with Walmart ICS."; - _logger.LogError(message); - _logger.LogError($"Request url: {fullUrl}"); - _logger.LogError($"Request headers: {_httpClient.DefaultRequestHeaders}"); - _logger.LogError($"Request body: {await requestContent.ReadAsStringAsync()}"); - _logger.LogError($"Response status code: {response.StatusCode}"); - _logger.LogError($"Response body: {await response.Content.ReadAsStringAsync()}"); + _logger.LogError("Error in linking account with Walmart ICS."); return new LinkAccountResponse([new LinkAccountError()], false, []); } @@ -338,15 +311,13 @@ public async Task UnlinkAccountAsync(IExecutionContext ct var fullUrl = string.Format(ICS_UNLINK_ACCOUNT_URL, icsHostname, isSandbox ? ICS_SANDBOX_URL_FRAGMENT : string.Empty, titleId); _httpClient.DefaultRequestHeaders.Add(WalmartAuthService.LCID_HEADER, lcid); + _logger.LogDebug("Calling Walmart ICS to unlink account."); + using var response = await _httpClient.DeleteAsync(fullUrl); + if (!response.IsSuccessStatusCode) { - var message = "Error in unlinking account with Walmart ICS."; - _logger.LogError(message); - _logger.LogError($"Request url: {fullUrl}"); - _logger.LogError($"Request headers: {_httpClient.DefaultRequestHeaders}"); - _logger.LogError($"Response status code: {response.StatusCode}"); - _logger.LogError($"Response body: {await response.Content.ReadAsStringAsync()}"); + _logger.LogError("Error in unlinking account with Walmart ICS."); return new UnlinkAccountResponse([new UnlinkAccountError()], false, []); } @@ -403,24 +374,14 @@ public async Task PlaceOrderAsync(IExecutionContext ctx, IGa var requestContent = new StringContent(JsonSerializer.Serialize(postPlaceOrderRequest, _jsonOptions), Encoding.UTF8, "application/json"); - using var response = await _httpClient.PostAsync(fullUrl, requestContent); - + _logger.LogDebug("Calling Walmart ICS to place order."); - _logger.LogDebug($"Request url: {fullUrl}"); - _logger.LogDebug($"Request headers: {_httpClient.DefaultRequestHeaders}"); - _logger.LogDebug($"Request body: {await requestContent.ReadAsStringAsync()}"); - _logger.LogDebug($"Response status code: {response.StatusCode}"); - _logger.LogDebug($"Response body: {await response.Content.ReadAsStringAsync()}"); + + using var response = await _httpClient.PostAsync(fullUrl, requestContent); if (!response.IsSuccessStatusCode) { _logger.LogError("Error while placing order with Walmart ICS."); - _logger.LogError($"Request url: {fullUrl}"); - _logger.LogError($"Request headers: {_httpClient.DefaultRequestHeaders}"); - _logger.LogError($"Request body: {await requestContent.ReadAsStringAsync()}"); - _logger.LogError($"Response status code: {response.StatusCode}"); - _logger.LogError($"Response body: {await response.Content.ReadAsStringAsync()}"); - var error = await MapErrors(response); return new PlaceOrderResponse([error ?? new PlaceOrderRequestError()], new PlaceOrderPayloadResponse()); @@ -491,24 +452,14 @@ public async Task PrepareOrderAsync(IExecutionContext ctx, }; var requestContent = new StringContent(JsonSerializer.Serialize(request, _jsonOptions), Encoding.UTF8, "application/json"); - using var response = await _httpClient.PostAsync(fullUrl, requestContent); - + _logger.LogDebug("Calling Walmart ICS to prepare order."); - _logger.LogDebug($"Request url: {fullUrl}"); - _logger.LogDebug($"Request headers: {_httpClient.DefaultRequestHeaders}"); - _logger.LogDebug($"Request body: {await requestContent.ReadAsStringAsync()}"); - _logger.LogDebug($"Response status code: {response.StatusCode}"); - _logger.LogDebug($"Response body: {await response.Content.ReadAsStringAsync()}"); + + using var response = await _httpClient.PostAsync(fullUrl, requestContent); if (!response.IsSuccessStatusCode) { _logger.LogError("Error while preparing order with Walmart ICS."); - _logger.LogError($"Request url: {fullUrl}"); - _logger.LogError($"Request headers: {_httpClient.DefaultRequestHeaders}"); - _logger.LogError($"Request body: {await requestContent.ReadAsStringAsync()}"); - _logger.LogError($"Response status code: {response.StatusCode}"); - _logger.LogError($"Response body: {await response.Content.ReadAsStringAsync()}"); - var error = await MapErrors(response); return new PrepareOrderResponse([error ?? new PrepareOrderRequestError()], new PrepareOrderPayloadResponse(), []); @@ -563,24 +514,14 @@ public async Task SetShippingAddressAsync(IExecutionContex }; var requestContent = new StringContent(JsonSerializer.Serialize(request, _jsonOptions), Encoding.UTF8, "application/json"); - using var response = await _httpClient.PostAsync(fullUrl, requestContent); - + _logger.LogDebug("Calling Walmart ICS to set shipping address."); - _logger.LogDebug($"Request url: {fullUrl}"); - _logger.LogDebug($"Request headers: {_httpClient.DefaultRequestHeaders}"); - _logger.LogDebug($"Request body: {await requestContent.ReadAsStringAsync()}"); - _logger.LogDebug($"Response status code: {response.StatusCode}"); - _logger.LogDebug($"Response body: {await response.Content.ReadAsStringAsync()}"); + + using var response = await _httpClient.PostAsync(fullUrl, requestContent); if (!response.IsSuccessStatusCode) { _logger.LogError("Error while setting shipping address with Walmart ICS."); - _logger.LogError($"Request url: {fullUrl}"); - _logger.LogError($"Request headers: {_httpClient.DefaultRequestHeaders}"); - _logger.LogError($"Request body: {await requestContent.ReadAsStringAsync()}"); - _logger.LogError($"Response status code: {response.StatusCode}"); - _logger.LogError($"Response body: {await response.Content.ReadAsStringAsync()}"); - var error = await MapErrors(response); return new PrepareOrderResponse([error ?? new SetShippingAddressRequestError()], new PrepareOrderPayloadResponse(), []);