Skip to content

Commit

Permalink
Merge pull request #82 from dragonfruitnetwork/reduce-serializer-ctors
Browse files Browse the repository at this point in the history
Update ApiClient to expose less ctors
  • Loading branch information
aspriddell authored Sep 10, 2021
2 parents 591412b + 86e1ba0 commit 3be2cda
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
22 changes: 6 additions & 16 deletions DragonFruit.Common.Data/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading;
using DragonFruit.Common.Data.Exceptions;
using DragonFruit.Common.Data.Headers;
using DragonFruit.Common.Data.Serializers;
using DragonFruit.Common.Data.Utils;

#pragma warning disable 618

Expand All @@ -25,19 +25,12 @@ public class ApiClient
#region Constructors

/// <summary>
/// Initialises a new <see cref="ApiClient"/> using an <see cref="ApiJsonSerializer"/> using the <see cref="CultureInfo.InvariantCulture"/>
/// Initialises a new <see cref="ApiClient"/> using an <see cref="ApiJsonSerializer"/> with an optional <see cref="CultureInfo"/>
/// </summary>
public ApiClient()
: this(new ApiJsonSerializer(CultureInfo.InvariantCulture))
{
}

/// <summary>
/// Initialises a new <see cref="ApiClient"/> using an <see cref="ApiJsonSerializer"/> using a custom <see cref="CultureInfo"/>
/// </summary>
public ApiClient(CultureInfo culture)
: this(new ApiJsonSerializer(culture))
public ApiClient(CultureInfo culture = null)
: this(new ApiJsonSerializer())
{
Serializer.Configure<ApiJsonSerializer>(o => o.Serializer.Culture = culture ?? CultureUtils.DefaultCulture);
}

/// <summary>
Expand Down Expand Up @@ -347,7 +340,7 @@ protected T InternalPerform<T>(HttpRequestMessage request, Func<HttpResponseMess
{
// exit the read lock as soon as the request has been sent and processed
// this is because the callback could involve re-processing the request.
RequestFinished();
_lock.ExitReadLock();
}

try
Expand Down Expand Up @@ -411,8 +404,5 @@ public void RequestClientReset(bool fullReset)
Interlocked.CompareExchange(ref _clientAdjustmentRequestSignal, 1, 0);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void RequestFinished() => _lock.ExitReadLock();
}
}
17 changes: 2 additions & 15 deletions DragonFruit.Common.Data/Serializers/SerializerResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class SerializerResolver
private static readonly Dictionary<Type, Type> SerializerMap = new Dictionary<Type, Type>();
private static readonly Dictionary<Type, Type> DeserializerMap = new Dictionary<Type, Type>();

private ISerializer _default;
private readonly ConcurrentDictionary<Type, ApiSerializer> _serializerCache = new ConcurrentDictionary<Type, ApiSerializer>();

/// <summary>
Expand Down Expand Up @@ -67,21 +66,9 @@ public static void Unregister<T>(DataDirection direction = DataDirection.All)
}

/// <summary>
/// The default <see cref="ISerializer"/> to use
/// The default <see cref="ISerializer"/> in use.
/// </summary>
public ISerializer Default
{
get => _default;
set
{
if (value is ApiSerializer { IsGeneric: false })
{
throw new ArgumentException("The provided serializer is non-generic.");
}

_default = value;
}
}
public ISerializer Default { get; }

/// <summary>
/// Resolves the <see cref="ApiSerializer"/> for the type provided
Expand Down
24 changes: 24 additions & 0 deletions DragonFruit.Common.Data/TargetTypedApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// DragonFruit.Common Copyright 2021 DragonFruit Network
// Licensed under the MIT License. Please refer to the LICENSE file at the root of this project for details

using System;
using DragonFruit.Common.Data.Serializers;

namespace DragonFruit.Common.Data
{
/// <summary>
/// A <see cref="ApiClient"/> superclass designed to allow better serializer configuration
/// </summary>
/// <typeparam name="T">The <see cref="ApiSerializer"/> to use</typeparam>
public class ApiClient<T> : ApiClient where T : ApiSerializer, new()
{
public ApiClient(Action<T> configurationOptions = null)
: base(Activator.CreateInstance<T>())
{
if (configurationOptions != null)
{
Serializer.Configure(configurationOptions);
}
}
}
}

0 comments on commit 3be2cda

Please sign in to comment.