Skip to content

Commit

Permalink
Add CancellationToken support to ShopDomainUtility.IsValidShopDomainA…
Browse files Browse the repository at this point in the history
…sync
  • Loading branch information
nozzlegear committed Dec 16, 2023
1 parent 15ffdd5 commit a9b3fa8
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions ShopifySharp/Utilities/ShopifyDomainUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public interface IShopifyDomainUtility
/// <p>**Warning**: this method of validation is not officially supported by Shopify and could break at any time.</p>
/// </summary>
/// <param name="shopDomain">The shop domain to check. This should be a *.myshopify.com domain.</param>
/// <returns>A boolean indicating whether the URL is valid.</returns>
Task<bool> IsValidShopDomainAsync(string shopDomain);
/// <param name="cancellationToken">A cancellation token which can cancel the request.</param>
/// <returns>Returns true if the shop domain is a Shopify store.</returns>
Task<bool> IsValidShopDomainAsync(string shopDomain, CancellationToken cancellationToken = default);
}

public class ShopifyDomainUtility : IShopifyDomainUtility
Expand All @@ -49,7 +50,7 @@ public Uri BuildShopDomainUri(string shopDomain)
}

/// <inheritdoc />
public async Task<bool> IsValidShopDomainAsync(string shopDomain)
public async Task<bool> IsValidShopDomainAsync(string shopDomain, CancellationToken cancellationToken = default)
{
var requestUri = BuildShopDomainUri(shopDomain);
var client = _httpClientFactory.CreateClient();
Expand All @@ -58,8 +59,10 @@ public async Task<bool> IsValidShopDomainAsync(string shopDomain)

try
{
var response = await client.SendAsync(msg);
return response.Headers.Any(h => h.Key.Equals("X-ShopId", StringComparison.OrdinalIgnoreCase));
var response = await client.SendAsync(msg, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
var hasShopIdHeader = response.Headers.Any(h => h.Key.Equals("X-ShopId", StringComparison.OrdinalIgnoreCase));

return hasShopIdHeader;
}
catch (HttpRequestException)
{
Expand Down

0 comments on commit a9b3fa8

Please sign in to comment.