Skip to content

Commit

Permalink
Merge pull request #4 from Archomeda/test-internal-client-interface
Browse files Browse the repository at this point in the history
Make client interface internal
  • Loading branch information
Archomeda authored Jun 21, 2019
2 parents 2924575 + 263a411 commit 0009933
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 43 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
- **Breaking:** Various endpoint clients with child endpoint clients and/or extra parameters have had their virtual property setters removed; you can still override the property for customization however
(reason: it's discouraged to call virtual methods or set virtual properties from a constructor in a non-sealed class)
- **Breaking:** `IGuildIdLogClient.ParamSince` and its implementation `GuildIdLogClient.ParamSince` are no longer virtual and no longer have a public setter to follow convention of e.g. `CreateSubtokenClient` (use `IGuildIdLogClient.Since(int? since)` to set this parameter with the fluent design pattern)
- **Breaking:** Since the settings in `IConnection` are only used interally, the `.Connection` property of `IGw2WebApiClient`, `IGw2WebApiV2Client` and all endpoint clients have been either removed or are now marked as internal
(if you still want to keep track of these settings, you should keep a reference to the instance yourself)
- Most `Connection` properties have a public setter to allow changes after object creation

## 0.3.1
### Fixes
Expand Down
5 changes: 2 additions & 3 deletions Gw2Sharp.Tests/WebApi/Gw2WebApiClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Gw2Sharp.WebApi;
using Gw2Sharp.WebApi;
using Gw2Sharp.WebApi.V2;
using NSubstitute;
using Xunit;
Expand All @@ -15,10 +15,9 @@ public void ConstructorTest()
var client1 = new Gw2WebApiClient();
var client2 = new Gw2WebApiClient(connection);

Assert.IsType<Connection>(client1.Connection);
Assert.IsType<Gw2WebApiV2Client>(client1.V2);

Assert.Same(connection, client2.Connection);
Assert.Same(connection, ((IWebApiClientInternal)client2.V2.Account.Achievements).Connection);
Assert.IsType<Gw2WebApiV2Client>(client2.V2);
}
}
Expand Down
18 changes: 9 additions & 9 deletions Gw2Sharp.Tests/WebApi/V2/Clients/BaseEndpointClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected virtual async Task AssertPaginatedDataAsync<TObject>(IPaginatedClient<
var (data, expected) = this.GetTestData(file);

var httpRequest = Substitute.For<IHttpRequest>();
this.Client.Connection.HttpClient.RequestAsync(Arg.Any<IHttpRequest>(), CancellationToken.None).Returns(callInfo =>
((IWebApiClientInternal)this.Client).Connection.HttpClient.RequestAsync(Arg.Any<IHttpRequest>(), CancellationToken.None).Returns(callInfo =>
{
this.AssertRequest(callInfo, client, "?page=2&page_size=100");
this.AssertAuthenticatedRequest(callInfo, client);
Expand All @@ -68,7 +68,7 @@ protected virtual async Task AssertBlobDataAsync<TObject>(IBlobClient<TObject> c
where TObject : IApiV2Object
{
var (data, expected) = this.GetTestData(file);
this.Client.Connection.HttpClient.RequestAsync(Arg.Any<IHttpRequest>(), CancellationToken.None).Returns(callInfo =>
((IWebApiClientInternal)this.Client).Connection.HttpClient.RequestAsync(Arg.Any<IHttpRequest>(), CancellationToken.None).Returns(callInfo =>
{
this.AssertRequest(callInfo, client, string.Empty);
this.AssertAuthenticatedRequest(callInfo, client);
Expand All @@ -88,7 +88,7 @@ protected virtual async Task AssertGetDataAsync<TObject, TId>(IBulkExpandableCli
var id = this.GetId<TId>(expected[idName]);

var httpRequest = Substitute.For<IHttpRequest>();
this.Client.Connection.HttpClient.RequestAsync(Arg.Any<IHttpRequest>(), CancellationToken.None).Returns(callInfo =>
((IWebApiClientInternal)this.Client).Connection.HttpClient.RequestAsync(Arg.Any<IHttpRequest>(), CancellationToken.None).Returns(callInfo =>
{
this.AssertRequest(callInfo, client, $"/{id.ToString()}");
this.AssertAuthenticatedRequest(callInfo, client);
Expand All @@ -105,7 +105,7 @@ protected virtual async Task AssertAllDataAsync<TObject>(IAllExpandableClient<TO
where TObject : IApiV2Object
{
var (data, expected) = this.GetTestData(file);
this.Client.Connection.HttpClient.RequestAsync(Arg.Any<IHttpRequest>(), CancellationToken.None).Returns(callInfo =>
((IWebApiClientInternal)this.Client).Connection.HttpClient.RequestAsync(Arg.Any<IHttpRequest>(), CancellationToken.None).Returns(callInfo =>
{
this.AssertRequest(callInfo, client, "?ids=all");
this.AssertAuthenticatedRequest(callInfo, client);
Expand All @@ -128,7 +128,7 @@ protected virtual async Task AssertBulkDataAsync<TObject, TId>(IBulkExpandableCl
}));

var httpRequest = Substitute.For<IHttpRequest>();
this.Client.Connection.HttpClient.RequestAsync(Arg.Any<IHttpRequest>(), CancellationToken.None).Returns(callInfo =>
((IWebApiClientInternal)this.Client).Connection.HttpClient.RequestAsync(Arg.Any<IHttpRequest>(), CancellationToken.None).Returns(callInfo =>
{
this.AssertRequest(callInfo, client, $"?ids={string.Join(",", ids.Select(i => i?.ToString()))}");
this.AssertAuthenticatedRequest(callInfo, client);
Expand All @@ -147,7 +147,7 @@ protected virtual async Task AssertIdsDataAsync<TObject, TId>(IBulkExpandableCli
var (data, expected) = this.GetTestData(file);

var httpRequest = Substitute.For<IHttpRequest>();
this.Client.Connection.HttpClient.RequestAsync(Arg.Any<IHttpRequest>(), CancellationToken.None).Returns(callInfo =>
((IWebApiClientInternal)this.Client).Connection.HttpClient.RequestAsync(Arg.Any<IHttpRequest>(), CancellationToken.None).Returns(callInfo =>
{
this.AssertRequest(callInfo, client, string.Empty);
this.AssertAuthenticatedRequest(callInfo, client);
Expand Down Expand Up @@ -188,22 +188,22 @@ protected virtual void AssertRequest(CallInfo callInfo, IEndpointClient client,
Assert.Equal(uri, callInfo.ArgAt<IHttpRequest>(0).Url);
var requestHeaders = callInfo.ArgAt<IHttpRequest>(0).RequestHeaders;
Assert.Contains(new KeyValuePair<string, string>("Accept", "application/json"), requestHeaders);
Assert.Contains(new KeyValuePair<string, string>("User-Agent", client.Connection.UserAgent), requestHeaders);
Assert.Contains(new KeyValuePair<string, string>("User-Agent", ((IWebApiClientInternal)client).Connection.UserAgent), requestHeaders);
}

protected virtual void AssertAuthenticatedRequest(CallInfo callInfo, IEndpointClient client)
{
var requestHeaders = callInfo.ArgAt<IHttpRequest>(0).RequestHeaders;
if (client.IsAuthenticated)
Assert.Contains(new KeyValuePair<string, string>("Authorization", $"Bearer {client.Connection.AccessToken}"), requestHeaders);
Assert.Contains(new KeyValuePair<string, string>("Authorization", $"Bearer {((IWebApiClientInternal)client).Connection.AccessToken}"), requestHeaders);
else
Assert.DoesNotContain(requestHeaders, h => h.Key == "Authorization");
}

protected virtual void AssertLocalizedRequest(CallInfo callInfo, IEndpointClient client)
{
var requestHeaders = callInfo.ArgAt<IHttpRequest>(0).RequestHeaders;
Assert.Contains(new KeyValuePair<string, string>("Accept-Language", client.Connection.LocaleString), requestHeaders);
Assert.Contains(new KeyValuePair<string, string>("Accept-Language", ((IWebApiClientInternal)client).Connection.LocaleString), requestHeaders);
}

protected virtual void AssertSchemaVersionRequest(CallInfo callInfo, IEndpointClient client)
Expand Down
8 changes: 4 additions & 4 deletions Gw2Sharp/WebApi/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public Connection(string accessToken, Locale locale, string userAgent, IHttpClie
}

/// <inheritdoc />
public string AccessToken { get; private set; }
public string AccessToken { get; set; }

/// <inheritdoc />
public Locale Locale { get; private set; }
public Locale Locale { get; set; }

/// <inheritdoc />
public string LocaleString
Expand All @@ -136,10 +136,10 @@ public string LocaleString
public string UserAgent { get; private set; }

/// <inheritdoc />
public IHttpClient HttpClient { get; private set; }
public IHttpClient HttpClient { get; set; }

/// <inheritdoc />
public ICacheMethod CacheMethod { get; private set; }
public ICacheMethod CacheMethod { get; set; }


/// <inheritdoc />
Expand Down
8 changes: 4 additions & 4 deletions Gw2Sharp/WebApi/Gw2WebApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public Gw2WebApiClient() : this(new Connection()) { }
/// Creates a new <see cref="Gw2WebApiClient"/>.
/// </summary>
/// <param name="connection">The connection used to make requests, see <see cref="IConnection"/>.</param>
/// <exception cref="NullReferenceException"><paramref name="connection"/> is <c>null</c>.</exception>
public Gw2WebApiClient(IConnection connection)
{
this.Connection = connection;
if (connection == null)
throw new ArgumentNullException(nameof(connection));

this.v2 = new Gw2WebApiV2Client(connection);
}

/// <inheritdoc />
public IConnection Connection { get; private set; }

/// <inheritdoc />
public virtual IGw2WebApiV2Client V2 => this.v2;
}
Expand Down
5 changes: 0 additions & 5 deletions Gw2Sharp/WebApi/IGw2WebApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ namespace Gw2Sharp.WebApi
/// </summary>
public interface IGw2WebApiClient
{
/// <summary>
/// Provides the client connection to make web requests.
/// </summary>
IConnection Connection { get; }

/// <summary>
/// Gets the version 2 of the API.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions Gw2Sharp/WebApi/IWebApiClientInternal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Gw2Sharp.WebApi
{
/// <summary>
/// Implements a web API client, consumed internally.
/// </summary>
internal interface IWebApiClientInternal
{
/// <summary>
/// Gets the client connection to make web requests.
/// </summary>
IConnection Connection { get; }
}
}
9 changes: 7 additions & 2 deletions Gw2Sharp/WebApi/V2/Clients/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Gw2Sharp.WebApi.V2.Clients
/// <summary>
/// An abstract base class for implementing clients.
/// </summary>
public abstract class BaseClient : IClient
public abstract class BaseClient : IClient, IWebApiClientInternal
{
/// <summary>
/// Creates a new base client.
Expand All @@ -18,6 +18,11 @@ protected BaseClient(IConnection connection)
}

/// <inheritdoc />
public IConnection Connection { get; private set; }
IConnection IWebApiClientInternal.Connection => this.Connection;

/// <summary>
/// Gets the client connection to make web requests.
/// </summary>
internal IConnection Connection { get; }
}
}
7 changes: 0 additions & 7 deletions Gw2Sharp/WebApi/V2/Clients/IClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,5 @@ namespace Gw2Sharp.WebApi.V2.Clients
/// </summary>
public interface IClient
{
/// <summary>
/// Provides the client connection to make web requests.
/// </summary>
/// <value>
/// The client connection.
/// </value>
IConnection Connection { get; }
}
}
7 changes: 3 additions & 4 deletions Gw2Sharp/WebApi/V2/Gw2WebApiV2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public Gw2WebApiV2Client() : this(new Connection()) { }
/// <exception cref="NullReferenceException"><paramref name="connection"/> is <c>null</c>.</exception>
public Gw2WebApiV2Client(IConnection connection)
{
this.Connection = connection ?? throw new ArgumentNullException(nameof(connection));
if (connection == null)
throw new ArgumentNullException(nameof(connection));

this.account = new AccountClient(connection);
this.achievements = new AchievementsClient(connection);
this.backstory = new BackstoryClient(connection);
Expand Down Expand Up @@ -89,9 +91,6 @@ public Gw2WebApiV2Client(IConnection connection)
this.worldBosses = new WorldBossesClient(connection);
}

/// <inheritdoc />
public IConnection Connection { get; private set; }

/// <inheritdoc />
public virtual IAccountClient Account => this.account;

Expand Down
5 changes: 0 additions & 5 deletions Gw2Sharp/WebApi/V2/IGw2WebApiV2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ namespace Gw2Sharp.WebApi.V2
/// </summary>
public interface IGw2WebApiV2Client
{
/// <summary>
/// Provides the client connection to make web requests.
/// </summary>
IConnection Connection { get; }

/// <summary>
/// Gets the account information.
/// Requires scopes: account.
Expand Down

0 comments on commit 0009933

Please sign in to comment.