diff --git a/ShopifySharp.Tests/ShopifyException_Tests.cs b/ShopifySharp.Tests/ShopifyException_Tests.cs index 32e3e4ed2..7e4e8d885 100644 --- a/ShopifySharp.Tests/ShopifyException_Tests.cs +++ b/ShopifySharp.Tests/ShopifyException_Tests.cs @@ -30,7 +30,7 @@ private HttpRequestMessage PrepareRequest(HttpMethod method, string path, HttpCo Content = content }; - msg.Headers.Add("X-Shopify-Access-Token", Utils.AccessToken); + msg.Headers.Add(ShopifyService.REQUEST_HEADER_ACCESS_TOKEN, Utils.AccessToken); return msg; } @@ -85,7 +85,7 @@ public async Task Throws_On_Error_String() try { - ShopifyService.CheckResponseExceptions(msg.ToString(), response, rawBody); + ShopifyService.CheckResponseExceptions(string.Empty, response, rawBody); } catch (ShopifyRateLimitException) { @@ -127,7 +127,7 @@ public async Task Throws_On_Error_Object() try { - ShopifyService.CheckResponseExceptions(msg.ToString(), response, rawBody); + ShopifyService.CheckResponseExceptions(string.Empty, response, rawBody); } catch (ShopifyRateLimitException) { @@ -205,7 +205,7 @@ public async Task Throws_On_Error_Arrays() try { - ShopifyService.CheckResponseExceptions(msg.ToString(), response, rawBody); + ShopifyService.CheckResponseExceptions(string.Empty, response, rawBody); } catch (ShopifyRateLimitException) { diff --git a/ShopifySharp/Infrastructure/CloneableRequestMessage.cs b/ShopifySharp/Infrastructure/CloneableRequestMessage.cs index 462ba7810..fae1a9464 100644 --- a/ShopifySharp/Infrastructure/CloneableRequestMessage.cs +++ b/ShopifySharp/Infrastructure/CloneableRequestMessage.cs @@ -1,5 +1,5 @@ using System; -using System.IO; +using System.Linq; using System.Net.Http; using System.Threading.Tasks; @@ -66,7 +66,7 @@ private static async Task CloneToStreamOrReadOnlyMemoryContent(Http var rs = new ReadOnlyMemory(await originalStreamContent.ReadAsByteArrayAsync()); clonedContent = new ReadOnlyMemoryContent(rs); #else - var ms = new MemoryStream(); + var ms = new System.IO.MemoryStream(); await originalStreamContent.CopyToAsync(ms); ms.Position = 0; clonedContent = new StreamContent(ms); @@ -79,5 +79,20 @@ private static async Task CloneToStreamOrReadOnlyMemoryContent(Http return clonedContent; } + + public string GetRequestInfo() + { + var headers = this.Headers.Where(kv => kv.Value != null && kv.Key != ShopifyService.REQUEST_HEADER_ACCESS_TOKEN) + .Select(kv => $"\t{kv.Key}: {string.Join(", ", kv.Value)}"); + return $""" + Method: {this.Method} + RequestUri: {this.RequestUri} + Content: {this.Content?.GetType().Name} + Headers: + [ + {string.Join(Environment.NewLine, headers)} + ] + """; + } } } diff --git a/ShopifySharp/Infrastructure/Policies/LeakyBucketPolicy/LeakyBucketExecutionPolicy.cs b/ShopifySharp/Infrastructure/Policies/LeakyBucketPolicy/LeakyBucketExecutionPolicy.cs index 11bf8038c..72d826093 100644 --- a/ShopifySharp/Infrastructure/Policies/LeakyBucketPolicy/LeakyBucketExecutionPolicy.cs +++ b/ShopifySharp/Infrastructure/Policies/LeakyBucketPolicy/LeakyBucketExecutionPolicy.cs @@ -17,8 +17,6 @@ namespace ShopifySharp /// public class LeakyBucketExecutionPolicy : IRequestExecutionPolicy { - private const string REQUEST_HEADER_ACCESS_TOKEN = "X-Shopify-Access-Token"; - private static ConcurrentDictionary _shopAccessTokenToLeakyBucket = new ConcurrentDictionary(); private readonly Func _getRequestContext; @@ -269,7 +267,7 @@ CloneableRequestMessage baseRequest private string GetAccessToken(HttpRequestMessage client) { - return client.Headers.TryGetValues(REQUEST_HEADER_ACCESS_TOKEN, out var values) ? + return client.Headers.TryGetValues(ShopifyService.REQUEST_HEADER_ACCESS_TOKEN, out var values) ? values.FirstOrDefault() : null; } diff --git a/ShopifySharp/Services/ShopifyService.cs b/ShopifySharp/Services/ShopifyService.cs index 14a93e0ad..f0e8a87d7 100644 --- a/ShopifySharp/Services/ShopifyService.cs +++ b/ShopifySharp/Services/ShopifyService.cs @@ -18,7 +18,9 @@ namespace ShopifySharp { public abstract class ShopifyService : IShopifyService { - #nullable enable + public const string REQUEST_HEADER_ACCESS_TOKEN = "X-Shopify-Access-Token"; + +#nullable enable public virtual string APIVersion => "2024-01"; public virtual bool SupportsAPIVersioning => true; @@ -159,7 +161,7 @@ protected CloneableRequestMessage PrepareRequestMessage(RequestUri uri, HttpMeth if (!string.IsNullOrEmpty(_AccessToken)) { - msg.Headers.Add("X-Shopify-Access-Token", _AccessToken); + msg.Headers.Add(REQUEST_HEADER_ACCESS_TOKEN, _AccessToken); } msg.Headers.Add("Accept", "application/json"); @@ -213,11 +215,11 @@ protected async Task> ExecuteRequestCoreAsync( #endif //Check for and throw exception when necessary. - CheckResponseExceptions(baseRequestMessage.ToString(), response, rawResult); + CheckResponseExceptions(baseRequestMessage.GetRequestInfo(), response, rawResult); var result = method == HttpMethod.Delete ? default : Serializer.Deserialize(rawResult, rootElement, dateParseHandlingOverride); - return new RequestResult(baseRequestMessage.ToString(), response, response.Headers, result, rawResult, ReadLinkHeader(response.Headers)); + return new RequestResult(baseRequestMessage.GetRequestInfo(), response, response.Headers, result, rawResult, ReadLinkHeader(response.Headers)); }, cancellationToken, graphqlQueryCost); return policyResult; diff --git a/ShopifySharp/Utilities/ShopifyOauthUtility.cs b/ShopifySharp/Utilities/ShopifyOauthUtility.cs index c4fbd89eb..e486f9ac4 100644 --- a/ShopifySharp/Utilities/ShopifyOauthUtility.cs +++ b/ShopifySharp/Utilities/ShopifyOauthUtility.cs @@ -242,7 +242,7 @@ string clientSecret using var response = await client.SendAsync(request); var rawDataString = await response.Content.ReadAsStringAsync(); - ShopifyService.CheckResponseExceptions(request.ToString(), response, rawDataString); + ShopifyService.CheckResponseExceptions(request.GetRequestInfo(), response, rawDataString); var json = JToken.Parse(rawDataString); return new AuthorizationResult(json.Value("access_token"), json.Value("scope")?.Split(',')); @@ -285,7 +285,7 @@ string existingStoreAccessToken using var response = await client.SendAsync(request); var rawDataString = await response.Content.ReadAsStringAsync(); - ShopifyService.CheckResponseExceptions(request.ToString(), response, rawDataString); + ShopifyService.CheckResponseExceptions(request.GetRequestInfo(), response, rawDataString); var json = JToken.Parse(rawDataString); // TODO: throw a ShopifyJsonParseException if value is null. Exception should have a RawBody property.