Skip to content

Commit

Permalink
misc: refactor to use file-scoped namespaces; rename internal classes…
Browse files Browse the repository at this point in the history
…/properties for consistent style
  • Loading branch information
nozzlegear committed May 10, 2024
1 parent c45da45 commit a5b7154
Show file tree
Hide file tree
Showing 12 changed files with 537 additions and 538 deletions.
1 change: 1 addition & 0 deletions ShopifySharp.Tests/LeakyBucket_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using ShopifySharp.Infrastructure.Policies.LeakyBucketPolicy;
using Xunit;

namespace ShopifySharp.Tests;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
using System.Threading.Tasks;
using ShopifySharp.Infrastructure;

namespace ShopifySharp
// ReSharper disable once CheckNamespace
namespace ShopifySharp;

public class DefaultRequestExecutionPolicy : IRequestExecutionPolicy
{
public class DefaultRequestExecutionPolicy : IRequestExecutionPolicy
public async Task<RequestResult<T>> Run<T>(CloneableRequestMessage request, ExecuteRequestAsync<T> executeRequestAsync, CancellationToken cancellationToken, int? graphqlQueryCost = null)
{
public async Task<RequestResult<T>> Run<T>(CloneableRequestMessage request, ExecuteRequestAsync<T> executeRequestAsync, CancellationToken cancellationToken, int? graphqlQueryCost = null)
{
var fullResult = await executeRequestAsync(request);
var fullResult = await executeRequestAsync(request);

return fullResult;
}
return fullResult;
}
}
30 changes: 15 additions & 15 deletions ShopifySharp/Infrastructure/Policies/IRequestExecutionPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
using System.Threading.Tasks;
using ShopifySharp.Infrastructure;

namespace ShopifySharp
{
public delegate Task<RequestResult<T>> ExecuteRequestAsync<T>(CloneableRequestMessage request);
// ReSharper disable once CheckNamespace
namespace ShopifySharp;

public delegate Task<RequestResult<T>> ExecuteRequestAsync<T>(CloneableRequestMessage request);

/// <summary>
/// Used to specify centralized logic that should run when executing shopify requests.
/// It is most useful to implement retry logic, but it can also be used for other concerns (i.e. tracing)
/// </summary>
public interface IRequestExecutionPolicy
{
/// <param name="baseRequest">The base request that was built by a service to execute.</param>
/// <param name="executeRequestAsync">A delegate that executes the request you pass to it.</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <param name="graphqlQueryCost">Optional expected QraphQL query cost (as seen in GraphQL response at extensions.cost.requestedQueryCost</param>
Task<RequestResult<T>> Run<T>(CloneableRequestMessage requestMessage, ExecuteRequestAsync<T> executeRequestAsync, CancellationToken cancellationToken, int? graphqlQueryCost = null);
}
/// <summary>
/// Used to specify centralized logic that should run when executing shopify requests.
/// It is most useful to implement retry logic, but it can also be used for other concerns (i.e. tracing)
/// </summary>
public interface IRequestExecutionPolicy
{
/// <param name="requestMessage">The base request that was built by a service to execute.</param>
/// <param name="executeRequestAsync">A delegate that executes the request you pass to it.</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <param name="graphqlQueryCost">Optional expected Graphql query cost (as seen in GraphQL response at extensions.cost.requestedQueryCost</param>
Task<RequestResult<T>> Run<T>(CloneableRequestMessage requestMessage, ExecuteRequestAsync<T> executeRequestAsync, CancellationToken cancellationToken, int? graphqlQueryCost = null);
}
11 changes: 0 additions & 11 deletions ShopifySharp/Infrastructure/Policies/LeakyBucketPolicy/APIType.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ShopifySharp.Infrastructure.Policies.LeakyBucketPolicy;

internal enum ApiType : byte
{
RestAdmin,
GraphQlAdmin,
GraphQlPartner,
}
Original file line number Diff line number Diff line change
@@ -1,62 +1,61 @@
using System;
using System.Collections.Generic;

namespace ShopifySharp
namespace ShopifySharp;

internal class ContextAwareQueue<T>
{
internal class ContextAwareQueue<T>
private Queue<T> _BackgroundQueue { get; set; }= new Queue<T>();

private Queue<T> _ForegroundQueue { get; set;} = new Queue<T>();

private readonly Func<RequestContext> _getContext;

public int Count => _BackgroundQueue.Count + _ForegroundQueue.Count;

public ContextAwareQueue(Func<RequestContext> getContext)
{
private Queue<T> _BackgroundQueue { get; set; }= new Queue<T>();
_getContext = getContext;
}

private Queue<T> _ForegroundQueue { get; set;} = new Queue<T>();
public void Enqueue(T i)
{
(_getContext() == RequestContext.Background ? _BackgroundQueue : _ForegroundQueue).Enqueue(i);
}

private readonly Func<RequestContext> _getContext;
public T Peek() => _ForegroundQueue.Count > 0 ? _ForegroundQueue.Peek() : _BackgroundQueue.Peek();

public int Count => _BackgroundQueue.Count + _ForegroundQueue.Count;
public T Dequeue() => _ForegroundQueue.Count > 0 ? _ForegroundQueue.Dequeue() : _BackgroundQueue.Dequeue();

public ContextAwareQueue(Func<RequestContext> getContext)
/// Removes the item and updates the queue.
public void RemoveAndUpdateQueue(T itemToRemove)
{
if (_ForegroundQueue.Contains(itemToRemove))
{
_getContext = getContext;
_ForegroundQueue = CopyQueueExcludingItem(_ForegroundQueue, itemToRemove);
return;
}

public void Enqueue(T i)
if (_BackgroundQueue.Contains(itemToRemove))
{
(_getContext() == RequestContext.Background ? _BackgroundQueue : _ForegroundQueue).Enqueue(i);
_BackgroundQueue = CopyQueueExcludingItem(_BackgroundQueue, itemToRemove);
}
}

public T Peek() => _ForegroundQueue.Count > 0 ? _ForegroundQueue.Peek() : _BackgroundQueue.Peek();

public T Dequeue() => _ForegroundQueue.Count > 0 ? _ForegroundQueue.Dequeue() : _BackgroundQueue.Dequeue();
/// Copies the items in <paramref name="existingQueue"/> to a new queue, excluding <paramref name="itemToExclude"/>.
private static Queue<T> CopyQueueExcludingItem(Queue<T> existingQueue, T itemToExclude)
{
var newQueue = new Queue<T>();

/// Removes the item and updates the queue.
public void RemoveAndUpdateQueue(T itemToRemove)
while (existingQueue.Count > 0)
{
if (_ForegroundQueue.Contains(itemToRemove))
var itemToAdd = existingQueue.Dequeue();
if (!itemToAdd.Equals(itemToExclude))
{
_ForegroundQueue = CopyQueueExcludingItem(_ForegroundQueue, itemToRemove);
return;
}

if (_BackgroundQueue.Contains(itemToRemove))
{
_BackgroundQueue = CopyQueueExcludingItem(_BackgroundQueue, itemToRemove);
newQueue.Enqueue(itemToAdd);
}
}

/// Copies the items in <paramref name="existingQueue"/> to a new queue, excluding <paramref name="itemToExclude"/>.
private static Queue<T> CopyQueueExcludingItem(Queue<T> existingQueue, T itemToExclude)
{
var newQueue = new Queue<T>();

while (existingQueue.Count > 0)
{
var itemToAdd = existingQueue.Dequeue();
if (!itemToAdd.Equals(itemToExclude))
{
newQueue.Enqueue(itemToAdd);
}
}

return newQueue;
}
return newQueue;
}
}
Loading

0 comments on commit a5b7154

Please sign in to comment.