1+ namespace TransactionProcessor . Client
2+ {
3+ using System ;
4+ using System . Net . Http ;
5+ using System . Text ;
6+ using System . Threading ;
7+ using System . Threading . Tasks ;
8+ using ClientProxyBase ;
9+ using DataTransferObjects ;
10+ using Newtonsoft . Json ;
11+
12+ /// <summary>
13+ ///
14+ /// </summary>
15+ /// <seealso cref="ClientProxyBase.ClientProxyBase" />
16+ /// <seealso cref="TransactionProcessor.Client.ITransactionProcessorClient" />
17+ public class TransactionProcessorClient : ClientProxyBase , ITransactionProcessorClient
18+ {
19+ #region Fields
20+
21+ /// <summary>
22+ /// The base address
23+ /// </summary>
24+ private readonly String BaseAddress ;
25+
26+ #endregion
27+
28+ #region Constructors
29+
30+ /// <summary>
31+ /// Initializes a new instance of the <see cref="TransactionProcessorClient" /> class.
32+ /// </summary>
33+ /// <param name="baseAddressResolver">The base address resolver.</param>
34+ /// <param name="httpClient">The HTTP client.</param>
35+ public TransactionProcessorClient ( Func < String , String > baseAddressResolver ,
36+ HttpClient httpClient ) : base ( httpClient )
37+ {
38+ this . BaseAddress = baseAddressResolver ( "TransactionProcessorApi" ) ;
39+
40+ // Add the API version header
41+ this . HttpClient . DefaultRequestHeaders . Add ( "api-version" , "1.0" ) ;
42+ }
43+
44+ #endregion
45+
46+ #region Methods
47+
48+ /// <summary>
49+ /// Performs the transaction.
50+ /// </summary>
51+ /// <param name="transactionRequest">The transaction request.</param>
52+ /// <param name="cancellationToken">The cancellation token.</param>
53+ /// <returns></returns>
54+ public async Task < SerialisedMessage > PerformTransaction ( SerialisedMessage transactionRequest ,
55+ CancellationToken cancellationToken )
56+ {
57+ SerialisedMessage response = null ;
58+
59+ String requestUri = $ "{ this . BaseAddress } /api/transactions";
60+
61+ try
62+ {
63+ String requestSerialised = JsonConvert . SerializeObject ( transactionRequest ) ;
64+
65+ StringContent httpContent = new StringContent ( requestSerialised , Encoding . UTF8 , "application/json" ) ;
66+
67+ // Add the access token to the client headers
68+ //this.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
69+
70+ // Make the Http Call here
71+ HttpResponseMessage httpResponse = await this . HttpClient . PostAsync ( requestUri , httpContent , cancellationToken ) ;
72+
73+ // Process the response
74+ String content = await this . HandleResponse ( httpResponse , cancellationToken ) ;
75+
76+ // call was successful so now deserialise the body to the response object
77+ response = JsonConvert . DeserializeObject < SerialisedMessage > ( content ) ;
78+ }
79+ catch ( Exception ex )
80+ {
81+ // An exception has occurred, add some additional information to the message
82+ Exception exception = new Exception ( "Error posting transaction." , ex ) ;
83+
84+ throw exception ;
85+ }
86+
87+ return response ;
88+ }
89+
90+ #endregion
91+ }
92+ }
0 commit comments