Skip to content

Commit

Permalink
#541 Changed injection of clients to factory pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
pingu2k4 committed Nov 23, 2024
1 parent 10eeebe commit c52f938
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 34 deletions.
5 changes: 2 additions & 3 deletions src/PinguApps.Appwrite.Client/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Client.Clients;
using PinguApps.Appwrite.Client.Internals;
using PinguApps.Appwrite.Client.Utils;
Expand All @@ -17,9 +16,9 @@ public class AccountClient : SessionAwareClientBase, IAccountClient
private readonly IAccountApi _accountApi;
private readonly Config _config;

public AccountClient(IServiceProvider services, Config config)
internal AccountClient(IAccountApi accountApi, Config config)
{
_accountApi = services.GetRequiredService<IAccountApi>();
_accountApi = accountApi;
_config = config;
}

Expand Down
5 changes: 2 additions & 3 deletions src/PinguApps.Appwrite.Client/Clients/DatabasesClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Client.Internals;
using PinguApps.Appwrite.Shared;
using PinguApps.Appwrite.Shared.Requests.Databases;
Expand All @@ -14,9 +13,9 @@ public class DatabasesClient : SessionAwareClientBase, IDatabasesClient
{
private readonly IDatabasesApi _databasesApi;

public DatabasesClient(IServiceProvider services)
internal DatabasesClient(IDatabasesApi databasesApi)
{
_databasesApi = services.GetRequiredService<IDatabasesApi>();
_databasesApi = databasesApi;
}

[ExcludeFromCodeCoverage]
Expand Down
5 changes: 2 additions & 3 deletions src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Client.Internals;
using PinguApps.Appwrite.Client.Utils;
using PinguApps.Appwrite.Shared;
Expand All @@ -17,9 +16,9 @@ public class TeamsClient : SessionAwareClientBase, ITeamsClient
private readonly ITeamsApi _teamsApi;
private readonly Config _config;

public TeamsClient(IServiceProvider services, Config config)
internal TeamsClient(ITeamsApi teamsApi, Config config)
{
_teamsApi = services.GetRequiredService<ITeamsApi>();
_teamsApi = teamsApi;
_config = config;
}

Expand Down
40 changes: 34 additions & 6 deletions src/PinguApps.Appwrite.Client/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,23 @@ public static IServiceCollection AddAppwriteClient(this IServiceCollection servi
.AddHttpMessageHandler<HeaderHandler>()
.AddHttpMessageHandler<ClientCookieSessionHandler>();

services.AddSingleton<IAccountClient, AccountClient>();
services.AddSingleton<ITeamsClient, TeamsClient>();
services.AddSingleton<IDatabasesClient, DatabasesClient>();
services.AddSingleton<IAccountClient>(sp =>
{
var api = sp.GetRequiredService<IAccountApi>();
var config = sp.GetRequiredService<Config>();
return new AccountClient(api, config);
});
services.AddSingleton<ITeamsClient>(sp =>
{
var api = sp.GetRequiredService<ITeamsApi>();
var config = sp.GetRequiredService<Config>();
return new TeamsClient(api, config);
});
services.AddSingleton<IDatabasesClient>(sp =>
{
var api = sp.GetRequiredService<IDatabasesApi>();
return new DatabasesClient(api);
});
services.AddSingleton<IAppwriteClient, AppwriteClient>();
services.AddSingleton(x => new Lazy<IAppwriteClient>(() => x.GetRequiredService<IAppwriteClient>()));

Expand Down Expand Up @@ -88,9 +102,23 @@ public static IServiceCollection AddAppwriteClientForServer(this IServiceCollect
.AddHttpMessageHandler<HeaderHandler>()
.ConfigurePrimaryHttpMessageHandler(ConfigurePrimaryHttpMessageHandler);

services.AddSingleton<IAccountClient, AccountClient>();
services.AddSingleton<ITeamsClient, TeamsClient>();
services.AddSingleton<IDatabasesClient, DatabasesClient>();
services.AddSingleton<IAccountClient>(sp =>
{
var api = sp.GetRequiredService<IAccountApi>();
var config = sp.GetRequiredService<Config>();
return new AccountClient(api, config);
});
services.AddSingleton<ITeamsClient>(sp =>
{
var api = sp.GetRequiredService<ITeamsApi>();
var config = sp.GetRequiredService<Config>();
return new TeamsClient(api, config);
});
services.AddSingleton<IDatabasesClient>(sp =>
{
var api = sp.GetRequiredService<IDatabasesApi>();
return new DatabasesClient(api);
});
services.AddSingleton<IAppwriteClient, AppwriteClient>();

return services;
Expand Down
5 changes: 2 additions & 3 deletions src/PinguApps.Appwrite.Server/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Server.Internals;
using PinguApps.Appwrite.Server.Utils;
using PinguApps.Appwrite.Shared;
Expand All @@ -16,9 +15,9 @@ public class AccountClient : IAccountClient

private readonly Config _config;

public AccountClient(IServiceProvider services, Config config)
internal AccountClient(IAccountApi accountApi, Config config)
{
_accountApi = services.GetRequiredService<IAccountApi>();
_accountApi = accountApi;
_config = config;
}

Expand Down
5 changes: 2 additions & 3 deletions src/PinguApps.Appwrite.Server/Clients/DatabasesClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Server.Internals;
using PinguApps.Appwrite.Shared;
using PinguApps.Appwrite.Shared.Requests.Databases;
Expand All @@ -16,9 +15,9 @@ public class DatabasesClient : IDatabasesClient
{
private readonly IDatabasesApi _databasesApi;

public DatabasesClient(IServiceProvider services)
internal DatabasesClient(IDatabasesApi databasesApi)
{
_databasesApi = services.GetRequiredService<IDatabasesApi>();
_databasesApi = databasesApi;
}

[ExcludeFromCodeCoverage]
Expand Down
5 changes: 2 additions & 3 deletions src/PinguApps.Appwrite.Server/Clients/TeamsClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Server.Internals;
using PinguApps.Appwrite.Server.Utils;
using PinguApps.Appwrite.Shared;
Expand All @@ -17,9 +16,9 @@ public class TeamsClient : ITeamsClient
private readonly ITeamsApi _teamsApi;
private readonly Config _config;

public TeamsClient(IServiceProvider services, Config config)
internal TeamsClient(ITeamsApi teamsApi, Config config)
{
_teamsApi = services.GetRequiredService<ITeamsApi>();
_teamsApi = teamsApi;
_config = config;
}

Expand Down
5 changes: 2 additions & 3 deletions src/PinguApps.Appwrite.Server/Clients/UsersClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Server.Internals;
using PinguApps.Appwrite.Server.Utils;
using PinguApps.Appwrite.Shared;
Expand All @@ -16,9 +15,9 @@ public class UsersClient : IUsersClient
private readonly IUsersApi _usersApi;
private readonly Config _config;

public UsersClient(IServiceProvider services, Config config)
internal UsersClient(IUsersApi usersApi, Config config)
{
_usersApi = services.GetRequiredService<IUsersApi>();
_usersApi = usersApi;
_config = config;
}

Expand Down
27 changes: 23 additions & 4 deletions src/PinguApps.Appwrite.Server/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,29 @@ public static IServiceCollection AddAppwriteServer(this IServiceCollection servi
.AddHttpMessageHandler<HeaderHandler>()
.ConfigurePrimaryHttpMessageHandler(ConfigurePrimaryHttpMessageHandler);

services.AddSingleton<IAccountClient, AccountClient>();
services.AddSingleton<IUsersClient, UsersClient>();
services.AddSingleton<ITeamsClient, TeamsClient>();
services.AddSingleton<IDatabasesClient, DatabasesClient>();
services.AddSingleton<IAccountClient>(sp =>
{
var api = sp.GetRequiredService<IAccountApi>();
var config = sp.GetRequiredService<Config>();
return new AccountClient(api, config);
});
services.AddSingleton<IUsersClient>(sp =>
{
var api = sp.GetRequiredService<IUsersApi>();
var config = sp.GetRequiredService<Config>();
return new UsersClient(api, config);
});
services.AddSingleton<ITeamsClient>(sp =>
{
var api = sp.GetRequiredService<ITeamsApi>();
var config = sp.GetRequiredService<Config>();
return new TeamsClient(api, config);
});
services.AddSingleton<IDatabasesClient>(sp =>
{
var api = sp.GetRequiredService<IDatabasesApi>();
return new DatabasesClient(api);
});
services.AddSingleton<IAppwriteClient, AppwriteClient>();

return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public void SetSession_UpdatesSession()
// Arrange
var sc = new ServiceCollection();
var mockAccountApi = new Mock<IAccountApi>();
sc.AddSingleton(mockAccountApi.Object);
var sp = sc.BuildServiceProvider();
var accountClient = new AccountClient(sp, new Config(TestConstants.Endpoint, TestConstants.ProjectId));
var accountClient = new AccountClient(mockAccountApi.Object, new Config(TestConstants.Endpoint, TestConstants.ProjectId));
var sessionAware = accountClient as ISessionAware;

// Act
Expand Down

0 comments on commit c52f938

Please sign in to comment.