diff --git a/Directory.Packages.props b/Directory.Packages.props
index bc547b8..2f8c59c 100644
--- a/Directory.Packages.props
+++ b/Directory.Packages.props
@@ -3,7 +3,7 @@
true
-
+
diff --git a/ShopifyNet.Tests/Interceptor/RequestAccessTokenInterceptorTests.cs b/ShopifyNet.Tests/Interceptor/RequestAccessTokenInterceptorTests.cs
new file mode 100644
index 0000000..1e141c8
--- /dev/null
+++ b/ShopifyNet.Tests/Interceptor/RequestAccessTokenInterceptorTests.cs
@@ -0,0 +1,76 @@
+using System.Net;
+using System.Net.Http.Json;
+using GraphQLSharp;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace ShopifyNet.Tests;
+
+[TestClass]
+public class RequestAccessTokenInterceptorTests
+{
+ private const string ACCESS_TOKEN_HEADER = "X-Shopify-Access-Token";
+
+ ///
+ /// Replaces the X-Shopify-Access-Token header set by ShopifyClientOptions with ShopifyGraphQLRequest.AccessToken, when set.
+ ///
+ private class RequestAccessTokenInterceptor : IInterceptor
+ {
+ public Task> InterceptRequestAsync(ShopifyGraphQLRequest request, ShopifyClientOptions options, CancellationToken cancellationToken, ExecuteRequestAsync executeAsync)
+ {
+ if (request.AccessToken == null)
+ return executeAsync(request, null, cancellationToken);
+
+ return executeAsync(request, headers =>
+ {
+ headers.Remove(ACCESS_TOKEN_HEADER);
+ headers.Add(ACCESS_TOKEN_HEADER, request.AccessToken);
+ }, cancellationToken);
+ }
+ }
+
+ private static (ShopifyClient client, List sentTokens) CreateClient()
+ {
+ var sentTokens = new List();
+ var httpClient = new TestHttpClient(msg =>
+ {
+ sentTokens.Add(msg.Headers.TryGetValues(ACCESS_TOKEN_HEADER, out var values) ? values.ToArray() : []);
+ return new HttpResponseMessage
+ {
+ StatusCode = HttpStatusCode.OK,
+ Content = JsonContent.Create(new GraphQLResponse())
+ };
+ });
+ var options = new ShopifyClientOptions("test.myshopify.com", "options-token")
+ {
+ Interceptor = new RequestAccessTokenInterceptor(),
+ HttpClient = httpClient,
+ };
+ return (new ShopifyClient(options), sentTokens);
+ }
+
+ [TestMethod]
+ public async Task ShouldReplaceOptionsAccessTokenWithRequestAccessToken()
+ {
+ var (client, sentTokens) = CreateClient();
+
+ await client.QueryAsync(new ShopifyGraphQLRequest
+ {
+ query = "query { shop { id } }",
+ AccessToken = "request-token",
+ });
+
+ Assert.AreEqual(1, sentTokens.Count);
+ CollectionAssert.AreEqual(new[] { "request-token" }, sentTokens[0]);
+ }
+
+ [TestMethod]
+ public async Task ShouldKeepOptionsAccessTokenWhenRequestAccessTokenIsNull()
+ {
+ var (client, sentTokens) = CreateClient();
+
+ await client.QueryAsync("query { shop { id } }");
+
+ Assert.AreEqual(1, sentTokens.Count);
+ CollectionAssert.AreEqual(new[] { "options-token" }, sentTokens[0]);
+ }
+}
diff --git a/ShopifyNet.Tests/TokenBucket/TestInterceptor.cs b/ShopifyNet.Tests/TokenBucket/TestInterceptor.cs
index 4bfb34b..0868ac6 100644
--- a/ShopifyNet.Tests/TokenBucket/TestInterceptor.cs
+++ b/ShopifyNet.Tests/TokenBucket/TestInterceptor.cs
@@ -12,12 +12,12 @@ public TestInterceptor(IInterceptor
this._interceptor = interceptor;
}
- public Task> InterceptRequestAsync(ShopifyGraphQLRequest request, ShopifyClientOptions options, CancellationToken cancellationToken, Func>> executeAsync)
+ public Task> InterceptRequestAsync(ShopifyGraphQLRequest request, ShopifyClientOptions options, CancellationToken cancellationToken, ExecuteRequestAsync executeAsync)
{
- return _interceptor.InterceptRequestAsync(request, options, cancellationToken, async (r, ct) =>
+ return _interceptor.InterceptRequestAsync(request, options, cancellationToken, async (r, h, ct) =>
{
CallCount++;
- return await executeAsync(r, ct);
+ return await executeAsync(r, h, ct);
});
}
}
diff --git a/ShopifyNet/Interceptor/TokenBucket/TokenBucketInterceptor.cs b/ShopifyNet/Interceptor/TokenBucket/TokenBucketInterceptor.cs
index bd25530..91fb741 100644
--- a/ShopifyNet/Interceptor/TokenBucket/TokenBucketInterceptor.cs
+++ b/ShopifyNet/Interceptor/TokenBucket/TokenBucketInterceptor.cs
@@ -49,12 +49,12 @@ private void RemoveIdleBucketsAsync()
}
}
- public async Task> InterceptRequestAsync(ShopifyGraphQLRequest r, ShopifyClientOptions options, CancellationToken cancellationToken, Func>> executeAsync)
+ public async Task> InterceptRequestAsync(ShopifyGraphQLRequest r, ShopifyClientOptions options, CancellationToken cancellationToken, ExecuteRequestAsync executeAsync)
{
TokenBucket bucket = null;
lock (_tokenToBucket)
{
- bucket = _tokenToBucket.GetOrAdd(options.AccessToken, t => new TokenBucket(DEFAULT_GRAPHQL_MAX_AVAILABLE, DEFAULT_GRAPHQL_RESTORE_RATE));
+ bucket = _tokenToBucket.GetOrAdd(r.AccessToken ?? options.AccessToken, t => new TokenBucket(DEFAULT_GRAPHQL_MAX_AVAILABLE, DEFAULT_GRAPHQL_RESTORE_RATE));
}
this.RemoveIdleBucketsAsync();
@@ -70,7 +70,7 @@ public async Task> InterceptRequestAsync(ShopifyGr
try
{
//may throw GraphQLErrorsException if options.ThrowOnGraphQLErrors is true
- res = await executeAsync(r, cancellationToken);
+ res = await executeAsync(r, null, cancellationToken);
}
catch (GraphQLErrorsException ex) when (attempt < MAX_ATTEMPTS && ex.response.IsThrottled())
{
diff --git a/ShopifyNet/ShopifyGraphQLRequest.cs b/ShopifyNet/ShopifyGraphQLRequest.cs
index 05f1270..77e4bee 100644
--- a/ShopifyNet/ShopifyGraphQLRequest.cs
+++ b/ShopifyNet/ShopifyGraphQLRequest.cs
@@ -8,6 +8,16 @@ public class ShopifyGraphQLRequest : GraphQLRequest
[JsonIgnore]
public int? Cost { get; set; }
+ ///
+ /// Optional per-request access token. Not applied by ShopifyNet itself: an interceptor is expected to read it
+ /// and set the X-Shopify-Access-Token header through the ExecuteRequestAsync header callback.
+ /// Because already adds its own token to that header, the callback must call
+ /// headers.Remove("X-Shopify-Access-Token") before adding this one, otherwise both values are sent.
+ /// When set, uses it to select the rate-limit bucket.
+ ///
+ [JsonIgnore]
+ public string AccessToken { get; set; }
+
public static implicit operator ShopifyGraphQLRequest(string query)
{
return new ShopifyGraphQLRequest
diff --git a/ShopifyNet/ShopifyNet.csproj b/ShopifyNet/ShopifyNet.csproj
index bda5dec..2a34528 100644
--- a/ShopifyNet/ShopifyNet.csproj
+++ b/ShopifyNet/ShopifyNet.csproj
@@ -5,7 +5,7 @@
https://github.com/Wish-Org/ShopifyNet
https://github.com/Wish-Org/ShopifyNet
.NET utilities for Shopify GraphQL
- 2.22.0
+ 3.0.0
shopify graphql types classes schema generator strongly typed