Skip to content

Commit

Permalink
Improve DI code, so we always inject a secret manager instead of a se…
Browse files Browse the repository at this point in the history
…cret
  • Loading branch information
Viincenttt committed Aug 16, 2024
1 parent fb462ca commit a6be422
Show file tree
Hide file tree
Showing 28 changed files with 89 additions and 94 deletions.
3 changes: 2 additions & 1 deletion src/Mollie.Api/Client/BalanceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Mollie.Api.Client.Abstract;
using Mollie.Api.Extensions;
using Mollie.Api.Framework.Authentication.Abstract;
Expand All @@ -17,7 +18,7 @@ public class BalanceClient : BaseMollieClient, IBalanceClient {
public BalanceClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) {
}

public BalanceClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public BalanceClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<BalanceResponse> GetBalanceAsync(string balanceId) {
Expand Down
14 changes: 7 additions & 7 deletions src/Mollie.Api/Client/BaseMollieClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Mollie.Api.Client {
public abstract class BaseMollieClient : IDisposable {
public const string ApiEndPoint = "https://api.mollie.com/v2/";
private readonly string _apiEndpoint = ApiEndPoint;
private readonly IBearerTokenRetriever _bearerTokenRetriever;
private readonly IMollieSecretManager _mollieSecretManager;
private readonly HttpClient _httpClient;
private readonly JsonConverterService _jsonConverterService;

Expand All @@ -35,22 +35,22 @@ protected BaseMollieClient(string apiKey, HttpClient? httpClient = null) {
_jsonConverterService = new JsonConverterService();
_createdHttpClient = httpClient == null;
_httpClient = httpClient ?? new HttpClient();
_bearerTokenRetriever = new DefaultBearerTokenRetriever(apiKey);
_mollieSecretManager = new DefaultMollieSecretManager(apiKey);
}

protected BaseMollieClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) {
protected BaseMollieClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) {
_jsonConverterService = new JsonConverterService();
_createdHttpClient = httpClient == null;
_httpClient = httpClient ?? new HttpClient();
_bearerTokenRetriever = bearerTokenRetriever;
_mollieSecretManager = mollieSecretManager;
}

protected BaseMollieClient(HttpClient? httpClient = null, string apiEndpoint = ApiEndPoint) {
_apiEndpoint = apiEndpoint;
_jsonConverterService = new JsonConverterService();
_createdHttpClient = httpClient == null;
_httpClient = httpClient ?? new HttpClient();
_bearerTokenRetriever = new DefaultBearerTokenRetriever(string.Empty);
_mollieSecretManager = new DefaultMollieSecretManager(string.Empty);
}

public IDisposable WithIdempotencyKey(string value) {
Expand Down Expand Up @@ -108,7 +108,7 @@ private async Task<T> ProcessHttpResponseMessage<T>(HttpResponseMessage response
}

protected void ValidateApiKeyIsOauthAccesstoken(bool isConstructor = false) {
string apiKey = _bearerTokenRetriever.GetBearerToken();
string apiKey = _mollieSecretManager.GetBearerToken();
if (!apiKey.StartsWith("access_")) {
if (isConstructor) {
throw new InvalidOperationException(
Expand All @@ -134,7 +134,7 @@ private void ValidateUrlLink(UrlLink urlObject) {
protected virtual HttpRequestMessage CreateHttpRequest(HttpMethod method, string relativeUri, HttpContent? content = null) {
HttpRequestMessage httpRequest = new HttpRequestMessage(method, new Uri(new Uri(_apiEndpoint), relativeUri));
httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _bearerTokenRetriever.GetBearerToken());
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _mollieSecretManager.GetBearerToken());
httpRequest.Headers.Add("User-Agent", GetUserAgent());
var idemPotencyKey = _idempotencyKey.Value ?? Guid.NewGuid().ToString();
httpRequest.Headers.Add("Idempotency-Key", idemPotencyKey);
Expand Down
3 changes: 2 additions & 1 deletion src/Mollie.Api/Client/CaptureClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Mollie.Api.Client.Abstract;
using Mollie.Api.Extensions;
using Mollie.Api.Framework.Authentication.Abstract;
Expand All @@ -15,7 +16,7 @@ public class CaptureClient : BaseMollieClient, ICaptureClient {
public CaptureClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) {
}

public CaptureClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public CaptureClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<CaptureResponse> GetCaptureAsync(string paymentId, string captureId, bool testmode = false) {
Expand Down
3 changes: 2 additions & 1 deletion src/Mollie.Api/Client/ChargebackClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Mollie.Api.Client.Abstract;
using Mollie.Api.Extensions;
using Mollie.Api.Framework.Authentication.Abstract;
Expand All @@ -13,7 +14,7 @@ public class ChargebackClient : BaseMollieClient, IChargebackClient {
public ChargebackClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) {
}

public ChargebackClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public ChargebackClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<ChargebackResponse> GetChargebackAsync(string paymentId, string chargebackId, bool testmode = false) {
Expand Down
5 changes: 3 additions & 2 deletions src/Mollie.Api/Client/ClientLinkClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Mollie.Api.Client.Abstract;
using Mollie.Api.Extensions;
using Mollie.Api.Framework.Authentication.Abstract;
Expand All @@ -18,8 +19,8 @@ public ClientLinkClient(string clientId, string oauthAccessToken, HttpClient? ht
_clientId = clientId;
}

public ClientLinkClient(string clientId, IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null)
: base(bearerTokenRetriever, httpClient) {
public ClientLinkClient(string clientId, IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null)
: base(mollieSecretManager, httpClient) {
_clientId = clientId;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/CustomerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class CustomerClient : BaseMollieClient, ICustomerClient {
public CustomerClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) {
}

public CustomerClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public CustomerClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<CustomerResponse> CreateCustomerAsync(CustomerRequest request) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/InvoiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class InvoiceClient : OauthBaseMollieClient, IInvoiceClient {
public InvoiceClient(string oauthAccessToken, HttpClient? httpClient = null) : base(oauthAccessToken, httpClient) {
}

public InvoiceClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public InvoiceClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<InvoiceResponse> GetInvoiceAsync(string invoiceId) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/MandateClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MandateClient : BaseMollieClient, IMandateClient {
public MandateClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) {
}

public MandateClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public MandateClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<MandateResponse> GetMandateAsync(string customerId, string mandateId, bool testmode = false) {
Expand Down
6 changes: 3 additions & 3 deletions src/Mollie.Api/Client/OauthBaseMollieClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ protected OauthBaseMollieClient(string oauthAccessToken, HttpClient? httpClient
ValidateApiKeyIsOauthAccesstoken(oauthAccessToken);
}

protected OauthBaseMollieClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null)
: base(bearerTokenRetriever, httpClient) {
ValidateApiKeyIsOauthAccesstoken(bearerTokenRetriever.GetBearerToken());
protected OauthBaseMollieClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null)
: base(mollieSecretManager, httpClient) {
ValidateApiKeyIsOauthAccesstoken(mollieSecretManager.GetBearerToken());
}

private void ValidateApiKeyIsOauthAccesstoken(string oauthAccessToken) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/OnboardingClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class OnboardingClient : BaseMollieClient, IOnboardingClient {
public OnboardingClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) {
}

public OnboardingClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public OnboardingClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<OnboardingStatusResponse> GetOnboardingStatusAsync() {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/OrderClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class OrderClient : BaseMollieClient, IOrderClient {
public OrderClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) {
}

public OrderClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public OrderClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<OrderResponse> CreateOrderAsync(OrderRequest orderRequest) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/OrganizationClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class OrganizationClient : OauthBaseMollieClient, IOrganizationClient {
public OrganizationClient(string oauthAccessToken, HttpClient? httpClient = null) : base(oauthAccessToken, httpClient) {
}

public OrganizationClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public OrganizationClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<OrganizationResponse> GetCurrentOrganizationAsync() {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/PaymentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PaymentClient : BaseMollieClient, IPaymentClient {

public PaymentClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) { }

public PaymentClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public PaymentClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<PaymentResponse> CreatePaymentAsync(PaymentRequest paymentRequest, bool includeQrCode = false) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/PaymentLinkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PaymentLinkClient : BaseMollieClient, IPaymentLinkClient
{
public PaymentLinkClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) { }

public PaymentLinkClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public PaymentLinkClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<PaymentLinkResponse> CreatePaymentLinkAsync(PaymentLinkRequest paymentLinkRequest)
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/PaymentMethodClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PaymentMethodClient : BaseMollieClient, IPaymentMethodClient
public PaymentMethodClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) {
}

public PaymentMethodClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public PaymentMethodClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<PaymentMethodResponse> GetPaymentMethodAsync(
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/PermissionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PermissionClient : OauthBaseMollieClient, IPermissionClient {
public PermissionClient(string oauthAccessToken, HttpClient? httpClient = null) : base(oauthAccessToken, httpClient) {
}

public PermissionClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public PermissionClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<PermissionResponse> GetPermissionAsync(string permissionId) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/ProfileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ProfileClient : BaseMollieClient, IProfileClient {
public ProfileClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) {
}

public ProfileClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public ProfileClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<ProfileResponse> CreateProfileAsync(ProfileRequest request) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/RefundClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class RefundClient : BaseMollieClient, IRefundClient {
public RefundClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) {
}

public RefundClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public RefundClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<RefundResponse> CreatePaymentRefundAsync(string paymentId, RefundRequest refundRequest) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/SettlementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class SettlementClient : BaseMollieClient, ISettlementClient {
public SettlementClient(string oauthAccessToken, HttpClient? httpClient = null) : base(oauthAccessToken, httpClient) {
}

public SettlementClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public SettlementClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<SettlementResponse> GetSettlementAsync(string settlementId) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/ShipmentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ShipmentClient : BaseMollieClient, IShipmentClient {
public ShipmentClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) {
}

public ShipmentClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public ShipmentClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<ShipmentResponse> CreateShipmentAsync(string orderId, ShipmentRequest shipmentRequest) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/SubscriptionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class SubscriptionClient : BaseMollieClient, ISubscriptionClient {
public SubscriptionClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) {
}

public SubscriptionClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public SubscriptionClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<ListResponse<SubscriptionResponse>> GetSubscriptionListAsync(string customerId, string? from = null, int? limit = null, string? profileId = null, bool testmode = false) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/TerminalClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TerminalClient : BaseMollieClient, ITerminalClient
{
public TerminalClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) { }

public TerminalClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public TerminalClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<TerminalResponse> GetTerminalAsync(string terminalId) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mollie.Api/Client/WalletClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class WalletClient : BaseMollieClient, IWalletClient {
public WalletClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) {
}

public WalletClient(IBearerTokenRetriever bearerTokenRetriever, HttpClient? httpClient = null) : base(bearerTokenRetriever, httpClient) {
public WalletClient(IMollieSecretManager mollieSecretManager, HttpClient? httpClient = null) : base(mollieSecretManager, httpClient) {
}

public async Task<ApplePayPaymentSessionResponse> RequestApplePayPaymentSessionAsync(ApplePayPaymentSessionRequest request) {
Expand Down
Loading

0 comments on commit a6be422

Please sign in to comment.