Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project/Services/HttpClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected override async Task<HttpResponseMessage> 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;
Expand Down
101 changes: 21 additions & 80 deletions Project/Services/WalmartCommerceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,13 @@ public async Task<LoginUrlResponse> 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, []);
}

Expand All @@ -151,10 +142,6 @@ public async Task<LoginUrlResponse> 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, []);
}

Expand Down Expand Up @@ -190,29 +177,18 @@ public async Task<AccountDetailsResponse> 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<GetAccountDetailsResponse>();
if (content == null ||
(content.Errors == null && content.Payload == null))
Expand Down Expand Up @@ -273,16 +249,13 @@ public async Task<LinkAccountResponse> 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, []);
}
Expand Down Expand Up @@ -338,15 +311,13 @@ public async Task<UnlinkAccountResponse> 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, []);
}
Expand Down Expand Up @@ -403,24 +374,14 @@ public async Task<PlaceOrderResponse> 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());
Expand Down Expand Up @@ -491,24 +452,14 @@ public async Task<PrepareOrderResponse> 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(), []);
Expand Down Expand Up @@ -563,24 +514,14 @@ public async Task<PrepareOrderResponse> 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(), []);
Expand Down