-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #970 from clement911/master
Extracted logic to parse Bucket state information for REST and GraphQL
- Loading branch information
Showing
6 changed files
with
99 additions
and
70 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
ShopifySharp/Infrastructure/BucketStates/GraphQLBucketState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace ShopifySharp | ||
{ | ||
public class GraphQLBucketState | ||
{ | ||
public int MaxAvailable { get; private set; } | ||
|
||
public int RestoreRate { get; private set; } | ||
|
||
public int CurrentlyAvailable { get; private set; } | ||
|
||
public int RequestedQueryCost { get; private set; } | ||
|
||
public int? ActualQueryCost { get; private set; } | ||
|
||
public static GraphQLBucketState Get(JToken response) | ||
{ | ||
var cost = response.SelectToken("extensions.cost"); | ||
if (cost == null) | ||
return null; | ||
|
||
var throttleStatus = cost["throttleStatus"]; | ||
int maximumAvailable = (int)throttleStatus["maximumAvailable"]; | ||
int restoreRate = (int)throttleStatus["restoreRate"]; | ||
int currentlyAvailable = (int)throttleStatus["currentlyAvailable"]; | ||
int requestedQueryCost = (int)cost["requestedQueryCost"]; | ||
int? actualQueryCost = (int?)cost["actualQueryCost"];//actual query cost is null if THROTTLED | ||
|
||
return new GraphQLBucketState | ||
{ | ||
MaxAvailable = maximumAvailable, | ||
RestoreRate = restoreRate, | ||
CurrentlyAvailable = currentlyAvailable, | ||
RequestedQueryCost = requestedQueryCost, | ||
ActualQueryCost = actualQueryCost, | ||
}; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
ShopifySharp/Infrastructure/BucketStates/RestBucketState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Linq; | ||
using System.Net.Http; | ||
|
||
namespace ShopifySharp | ||
{ | ||
public class RestBucketState | ||
{ | ||
public int CurrentlyUsed { get; private set; } | ||
|
||
public int MaxAvailable { get; private set; } | ||
|
||
public const string RESPONSE_HEADER_API_CALL_LIMIT = "X-Shopify-Shop-Api-Call-Limit"; | ||
|
||
public static RestBucketState Get(HttpResponseMessage response) | ||
{ | ||
string headerValue = response.Headers.FirstOrDefault(kvp => kvp.Key == RESPONSE_HEADER_API_CALL_LIMIT) | ||
.Value | ||
?.FirstOrDefault(); | ||
|
||
if (headerValue == null) | ||
return null; | ||
|
||
|
||
var split = headerValue.Split('/'); | ||
if (split.Length == 2 && int.TryParse(split[0], out int currentlyUsed) && | ||
int.TryParse(split[1], out int maxAvailable)) | ||
{ | ||
return new RestBucketState | ||
{ | ||
CurrentlyUsed = currentlyUsed, | ||
MaxAvailable = maxAvailable | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters