Skip to content

Commit

Permalink
Merge pull request #587 from PinguApps/586-config-overwritten
Browse files Browse the repository at this point in the history
586 Ensured that config is only ever accessed as a keyed service to prevent it being overwritten
  • Loading branch information
pingu2k4 authored Dec 21, 2024
2 parents f30511c + 7718df1 commit 3c91798
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/PinguApps.Appwrite.Client/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -16,7 +17,7 @@ public class AccountClient : SessionAwareClientBase, IAccountClient
private readonly IAccountApi _accountApi;
private readonly Config _config;

internal AccountClient(IAccountApi accountApi, Config config)
internal AccountClient(IAccountApi accountApi, [FromKeyedServices("Client")] Config config)
{
_accountApi = accountApi;
_config = config;
Expand Down
3 changes: 2 additions & 1 deletion src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -16,7 +17,7 @@ public class TeamsClient : SessionAwareClientBase, ITeamsClient
private readonly ITeamsApi _teamsApi;
private readonly Config _config;

internal TeamsClient(ITeamsApi teamsApi, Config config)
internal TeamsClient(ITeamsApi teamsApi, [FromKeyedServices("Client")] Config config)
{
_teamsApi = teamsApi;
_config = config;
Expand Down
3 changes: 2 additions & 1 deletion src/PinguApps.Appwrite.Client/Handlers/HeaderHandler.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Shared;

namespace PinguApps.Appwrite.Client.Handlers;
internal class HeaderHandler : DelegatingHandler
{
private readonly string _projectId;

public HeaderHandler(Config config)
public HeaderHandler([FromKeyedServices("Client")] Config config)
{
_projectId = config.ProjectId;
}
Expand Down
12 changes: 6 additions & 6 deletions src/PinguApps.Appwrite.Client/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static IServiceCollection AddAppwriteClient(this IServiceCollection servi
{
var customRefitSettings = AddSerializationConfigToRefitSettings(refitSettings);

services.AddSingleton(new Config(endpoint, projectId));
services.AddKeyedSingleton("Client", new Config(endpoint, projectId));
services.AddTransient<HeaderHandler>();
services.AddTransient<ClientCookieSessionHandler>();

Expand All @@ -52,13 +52,13 @@ public static IServiceCollection AddAppwriteClient(this IServiceCollection servi
services.AddSingleton<IAccountClient>(sp =>
{
var api = sp.GetRequiredService<IAccountApi>();
var config = sp.GetRequiredService<Config>();
var config = sp.GetRequiredKeyedService<Config>("Client");
return new AccountClient(api, config);
});
services.AddSingleton<ITeamsClient>(sp =>
{
var api = sp.GetRequiredService<ITeamsApi>();
var config = sp.GetRequiredService<Config>();
var config = sp.GetRequiredKeyedService<Config>("Client");
return new TeamsClient(api, config);
});
services.AddSingleton<IDatabasesClient>(sp =>
Expand All @@ -84,7 +84,7 @@ public static IServiceCollection AddAppwriteClientForServer(this IServiceCollect
{
var customRefitSettings = AddSerializationConfigToRefitSettings(refitSettings);

services.AddSingleton(new Config(endpoint, projectId));
services.AddKeyedSingleton("Client", new Config(endpoint, projectId));
services.AddTransient<HeaderHandler>();

services.AddRefitClient<IAccountApi>(customRefitSettings)
Expand All @@ -105,13 +105,13 @@ public static IServiceCollection AddAppwriteClientForServer(this IServiceCollect
services.AddSingleton<IAccountClient>(sp =>
{
var api = sp.GetRequiredService<IAccountApi>();
var config = sp.GetRequiredService<Config>();
var config = sp.GetRequiredKeyedService<Config>("Client");
return new AccountClient(api, config);
});
services.AddSingleton<ITeamsClient>(sp =>
{
var api = sp.GetRequiredService<ITeamsApi>();
var config = sp.GetRequiredService<Config>();
var config = sp.GetRequiredKeyedService<Config>("Client");
return new TeamsClient(api, config);
});
services.AddSingleton<IDatabasesClient>(sp =>
Expand Down
3 changes: 2 additions & 1 deletion src/PinguApps.Appwrite.Server/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 @@ -15,7 +16,7 @@ public class AccountClient : IAccountClient

private readonly Config _config;

internal AccountClient(IAccountApi accountApi, Config config)
internal AccountClient(IAccountApi accountApi, [FromKeyedServices("Server")] Config config)
{
_accountApi = accountApi;
_config = config;
Expand Down
3 changes: 2 additions & 1 deletion src/PinguApps.Appwrite.Server/Clients/TeamsClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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,7 +17,7 @@ public class TeamsClient : ITeamsClient
private readonly ITeamsApi _teamsApi;
private readonly Config _config;

internal TeamsClient(ITeamsApi teamsApi, Config config)
internal TeamsClient(ITeamsApi teamsApi, [FromKeyedServices("Server")] Config config)
{
_teamsApi = teamsApi;
_config = config;
Expand Down
3 changes: 2 additions & 1 deletion src/PinguApps.Appwrite.Server/Clients/UsersClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -15,7 +16,7 @@ public class UsersClient : IUsersClient
private readonly IUsersApi _usersApi;
private readonly Config _config;

internal UsersClient(IUsersApi usersApi, Config config)
internal UsersClient(IUsersApi usersApi, [FromKeyedServices("Server")] Config config)
{
_usersApi = usersApi;
_config = config;
Expand Down
3 changes: 2 additions & 1 deletion src/PinguApps.Appwrite.Server/Handlers/HeaderHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Shared;

namespace PinguApps.Appwrite.Server.Handlers;
Expand All @@ -10,7 +11,7 @@ internal class HeaderHandler : DelegatingHandler
private readonly string _projectId;
private readonly string _apiKey;

public HeaderHandler(Config config)
public HeaderHandler([FromKeyedServices("Server")] Config config)
{
if (config.ApiKey is null)
throw new ArgumentNullException("config.ApiKey");
Expand Down
8 changes: 4 additions & 4 deletions src/PinguApps.Appwrite.Server/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static IServiceCollection AddAppwriteServer(this IServiceCollection servi
{
var customRefitSettings = AddSerializationConfigToRefitSettings(refitSettings);

services.AddSingleton(new Config(endpoint, projectId, apiKey));
services.AddKeyedSingleton("Server", new Config(endpoint, projectId, apiKey));
services.AddTransient<HeaderHandler>();

services.AddRefitClient<IAccountApi>(customRefitSettings)
Expand All @@ -57,19 +57,19 @@ public static IServiceCollection AddAppwriteServer(this IServiceCollection servi
services.AddSingleton<IAccountClient>(sp =>
{
var api = sp.GetRequiredService<IAccountApi>();
var config = sp.GetRequiredService<Config>();
var config = sp.GetRequiredKeyedService<Config>("Server");
return new AccountClient(api, config);
});
services.AddSingleton<IUsersClient>(sp =>
{
var api = sp.GetRequiredService<IUsersApi>();
var config = sp.GetRequiredService<Config>();
var config = sp.GetRequiredKeyedService<Config>("Server");
return new UsersClient(api, config);
});
services.AddSingleton<ITeamsClient>(sp =>
{
var api = sp.GetRequiredService<ITeamsApi>();
var config = sp.GetRequiredService<Config>();
var config = sp.GetRequiredKeyedService<Config>("Server");
return new TeamsClient(api, config);
});
services.AddSingleton<IDatabasesClient>(sp =>
Expand Down
2 changes: 1 addition & 1 deletion src/PinguApps.Appwrite.Shared/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace PinguApps.Appwrite.Shared;
public static class Constants
{
public const string Version = "0.4.5";
public const string Version = "0.4.6";
}

0 comments on commit 3c91798

Please sign in to comment.