Skip to content

Commit

Permalink
13th release (#11)
Browse files Browse the repository at this point in the history
* Fix bug in the Flyweight class's constructor

* Add the HasActiveScope property to the ScopedCollection class

* Remove redundant lazy initialization of property and header scopes

* Increase version number

---------

Co-authored-by: Kambiz Khojasteh <kambiz.khojasteh@gmail.com>
  • Loading branch information
kampute and Khojasteh authored May 8, 2024
1 parent f429ecc commit d6af688
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Title>Kampute.HttpClient.DataContract</Title>
<Description>This package is an extension package for Kampute.HttpClient, enhancing it to manage application/xml content types, using DataContractSerializer for serialization and deserialization of XML responses and payloads.</Description>
<Authors>Kambiz Khojasteh</Authors>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<Company>Kampute</Company>
<Copyright>Copyright (c) 2024 Kampute</Copyright>
<LangVersion>latest</LangVersion>
Expand Down
2 changes: 1 addition & 1 deletion src/Kampute.HttpClient.Json/Kampute.HttpClient.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Title>Kampute.HttpClient.Json</Title>
<Description>This package is an extension package for Kampute.HttpClient, enhancing it to manage application/json content types, using System.Text.Json library for serialization and deserialization of JSON responses and payloads.</Description>
<Authors>Kambiz Khojasteh</Authors>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<Company>Kampute</Company>
<Copyright>Copyright (c) 2024 Kampute</Copyright>
<LangVersion>latest</LangVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Title>Kampute.HttpClient.NewtonsoftJson</Title>
<Description>This package is an extension package for Kampute.HttpClient, enhancing it to manage application/json content types, using Newtonsoft.Json library for serialization and deserialization of JSON responses and payloads.</Description>
<Authors>Kambiz Khojasteh</Authors>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<Company>Kampute</Company>
<Copyright>Copyright (c) 2024 Kampute</Copyright>
<LangVersion>latest</LangVersion>
Expand Down
2 changes: 1 addition & 1 deletion src/Kampute.HttpClient.Xml/Kampute.HttpClient.Xml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Title>Kampute.HttpClient.Xml</Title>
<Description>This package is an extension package for Kampute.HttpClient, enhancing it to manage application/xml content types, using XmlSerializer for serialization and deserialization of XML responses and payloads.</Description>
<Authors>Kambiz Khojasteh</Authors>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<Company>Kampute</Company>
<Copyright>Copyright (c) 2024 Kampute</Copyright>
<LangVersion>latest</LangVersion>
Expand Down
16 changes: 8 additions & 8 deletions src/Kampute.HttpClient/HttpRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ private static HttpRequestHeaders CreateRequestHeaders()
private readonly HttpClient _httpClient;
private readonly IDisposable? _disposable;

private readonly Lazy<ScopedCollection<KeyValuePair<string, string?>>> _scopedHeaders = new(LazyThreadSafetyMode.ExecutionAndPublication);
private readonly Lazy<ScopedCollection<KeyValuePair<string, object?>>> _scopedProperties = new(LazyThreadSafetyMode.ExecutionAndPublication);
private readonly ScopedCollection<KeyValuePair<string, string?>> _scopedHeaders = new();
private readonly ScopedCollection<KeyValuePair<string, object?>> _scopedProperties = new();

private IHttpBackoffProvider _backoffStrategy = BackoffStrategies.None;
private Uri? _baseAddress;
Expand Down Expand Up @@ -261,7 +261,7 @@ public virtual IDisposable BeginPropertyScope(IEnumerable<KeyValuePair<string, o
if (properties is null)
throw new ArgumentNullException(nameof(properties));

return _scopedProperties.Value.BeginScope(properties);
return _scopedProperties.BeginScope(properties);
}

/// <summary>
Expand Down Expand Up @@ -290,7 +290,7 @@ public virtual IDisposable BeginHeaderScope(IEnumerable<KeyValuePair<string, str
if (headers is null)
throw new ArgumentNullException(nameof(headers));

return _scopedHeaders.Value.BeginScope(headers);
return _scopedHeaders.BeginScope(headers);
}

/// <summary>
Expand Down Expand Up @@ -661,9 +661,9 @@ void AddRequestHeaders()
foreach (var header in DefaultRequestHeaders)
request.Headers.TryAddWithoutValidation(header.Key, header.Value);

if (_scopedHeaders.IsValueCreated)
if (_scopedHeaders.HasActiveScope)
{
foreach (var header in _scopedHeaders.Value)
foreach (var header in _scopedHeaders)
{
request.Headers.Remove(header.Key);
if (header.Value is not null)
Expand All @@ -683,9 +683,9 @@ void AddRequestProperties()
request.Properties[HttpRequestMessagePropertyKeys.TransactionId] = Guid.NewGuid();
request.Properties[HttpRequestMessagePropertyKeys.ResponseObjectType] = responseObjectType;

if (_scopedProperties.IsValueCreated)
if (_scopedProperties.HasActiveScope)
{
foreach (var property in _scopedProperties.Value)
foreach (var property in _scopedProperties)
{
if (property.Value is not null)
request.Properties[property.Key] = property.Value;
Expand Down
2 changes: 1 addition & 1 deletion src/Kampute.HttpClient/Kampute.HttpClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Title>Kampute.HttpClient</Title>
<Description>Kampute.HttpClient is a versatile and lightweight .NET library that simplifies RESTful API communication. Its core HttpRestClient class provides a streamlined approach to HTTP interactions, offering advanced features such as flexible serialization/deserialization, robust error handling, configurable backoff strategies, and detailed request-response processing. Striking a balance between simplicity and extensibility, Kampute.HttpClient empowers developers with a powerful yet easy-to-use client for seamless API integration across a wide range of .NET applications.</Description>
<Authors>Kambiz Khojasteh</Authors>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<Company>Kampute</Company>
<Copyright>Copyright (c) 2024 Kampute</Copyright>
<LangVersion>latest</LangVersion>
Expand Down
9 changes: 5 additions & 4 deletions src/Kampute.HttpClient/Utilities/FlyweightCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@ public sealed class FlyweightCache<TKey, TValue> where TKey : notnull
/// <param name="valueFactory">A delegate that defines the method to create values if the key does not exist in the cache.</param>
/// <exception cref="ArgumentNullException">Thrown if the provided <paramref name="valueFactory"/> is <c>null</c>.</exception>
public FlyweightCache(Func<TKey, TValue> valueFactory)
: this(valueFactory, null)
{
_valueFactory = valueFactory ?? throw new ArgumentNullException(nameof(valueFactory));
_store = new ConcurrentDictionary<TKey, TValue>();
}

/// <summary>
/// Initializes a new instance of the <see cref="FlyweightCache{TKey, TValue}"/> class using a specified value factory and key comparer.
/// </summary>
/// <param name="valueFactory">A delegate that defines the method to create values if the key does not exist in the cache.</param>
/// <param name="keyComparer">The equality comparison implementation to use when comparing keys.</param>
/// <exception cref="ArgumentNullException">Thrown if the provided <paramref name="valueFactory"/> is <c>null</c>.</exception>
public FlyweightCache(Func<TKey, TValue> valueFactory, IEqualityComparer<TKey>? keyComparer)
/// <exception cref="ArgumentNullException">Thrown if the provided <paramref name="valueFactory"/> or <paramref name="keyComparer"/> is <c>null</c>.</exception>
public FlyweightCache(Func<TKey, TValue> valueFactory, IEqualityComparer<TKey> keyComparer)
{
_valueFactory = valueFactory ?? throw new ArgumentNullException(nameof(valueFactory));
_store = new ConcurrentDictionary<TKey, TValue>(keyComparer);
_store = new ConcurrentDictionary<TKey, TValue>(keyComparer ?? throw new ArgumentNullException(nameof(keyComparer)));
}

/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions src/Kampute.HttpClient/Utilities/ScopedCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public class ScopedCollection<T> : IEnumerable<T>
{
private readonly AsyncLocal<Scope?> _activeScope = new();

/// <summary>
/// Gets a value indicating whether the current context has an active scope.
/// </summary>
/// <value>
/// <c>true</c> if an active scope is present; otherwise, <c>false</c>.
/// </value>
public bool HasActiveScope => _activeScope.Value is not null;

/// <summary>
/// Initiates a new scope within the current context, incorporating the specified items.
/// </summary>
Expand Down

0 comments on commit d6af688

Please sign in to comment.