-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPaymentServiceAdapter.cs
More file actions
43 lines (37 loc) · 1.93 KB
/
PaymentServiceAdapter.cs
File metadata and controls
43 lines (37 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Duber.Domain.ACL.Contracts;
using Duber.Domain.ACL.Translators;
using Duber.Domain.SharedKernel.Chaos;
using Duber.Domain.SharedKernel.Model;
using Duber.Infrastructure.Chaos;
using Duber.Infrastructure.Resilience.Http;
using Polly;
namespace Duber.Domain.ACL.Adapters
{
public class PaymentServiceAdapter : IPaymentServiceAdapter
{
private readonly ResilientHttpClient _httpClient;
private readonly string _paymentServiceBaseUrl;
private readonly Lazy<Task<GeneralChaosSetting>> _generalChaosSettingFactory;
public PaymentServiceAdapter(ResilientHttpClient httpClient, string paymentServiceBaseUrl, Lazy<Task<GeneralChaosSetting>> generalChaosSettingFactory)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_generalChaosSettingFactory = generalChaosSettingFactory ?? throw new ArgumentException(nameof(generalChaosSettingFactory));
_paymentServiceBaseUrl = !string.IsNullOrWhiteSpace(paymentServiceBaseUrl) ? paymentServiceBaseUrl : throw new ArgumentNullException(nameof(paymentServiceBaseUrl));
}
public async Task<PaymentInfo> ProcessPaymentAsync(int userId, string reference)
{
var uri = new Uri(
new Uri(_paymentServiceBaseUrl),
string.Format(ThirdPartyServices.Payment.PerformPayment(), userId, reference));
var request = new HttpRequestMessage(HttpMethod.Post, uri);
var chaosSettings = await _generalChaosSettingFactory.Value;
var context = new Context(OperationKeys.PaymentApi.ToString()).WithChaosSettings(chaosSettings);
var response = await _httpClient.SendAsync(request, context);
response.EnsureSuccessStatusCode();
return PaymentInfoTranslator.Translate(await response.Content.ReadAsStringAsync());
}
}
}