1- using System . Net ;
2- using System . Net . Http ;
1+ using System . Net . Http ;
32using System . Text ;
43using System . Text . Json ;
54using System . Threading ;
6- using System . Threading . Tasks ;
75using ByteSync . Common . Controls . Json ;
8- using ByteSync . Common . Helpers ;
96using ByteSync . Exceptions ;
107using ByteSync . Interfaces . Controls . Communications . Http ;
118using ByteSync . Interfaces . Services . Communications ;
@@ -19,59 +16,63 @@ public class ApiInvoker : IApiInvoker
1916 private readonly IConnectionConstantsService _connectionConstantsService ;
2017 private readonly ILogger < ApiInvoker > _logger ;
2118
22- public ApiInvoker ( IHttpClientFactory httpClientFactory , IAuthenticationTokensRepository authenticationTokensRepository ,
19+ public ApiInvoker ( IHttpClientFactory httpClientFactory , IAuthenticationTokensRepository authenticationTokensRepository ,
2320 IConnectionConstantsService connectionConstantsService , ILogger < ApiInvoker > logger )
2421 {
2522 _httpClient = httpClientFactory . CreateClient ( "ApiClient" ) ;
2623 _authenticationTokensRepository = authenticationTokensRepository ;
2724 _connectionConstantsService = connectionConstantsService ;
2825 _logger = logger ;
2926 }
30-
27+
3128 public Task < T > GetAsync < T > ( string resource , CancellationToken cancellationToken = default )
3229 {
3330 return InvokeRestAsync < T > ( HttpMethod . Get , resource , null , null , cancellationToken ) ;
3431 }
35-
32+
3633 public Task < T > PostAsync < T > ( string resource , object ? postObject , CancellationToken cancellationToken = default )
3734 {
3835 return InvokeRestAsync < T > ( HttpMethod . Post , resource , null , postObject , cancellationToken ) ;
3936 }
40-
37+
4138 public Task < T > DeleteAsync < T > ( string resource , object objectToDelete , CancellationToken cancellationToken = default )
4239 {
4340 return InvokeRestAsync < T > ( HttpMethod . Delete , resource , null , objectToDelete , cancellationToken ) ;
4441 }
45-
42+
4643 public Task PostAsync ( string resource , object ? postObject , CancellationToken cancellationToken = default )
4744 {
4845 return DoInvokeRestAsync < object > ( HttpMethod . Post , resource , null , postObject , false , cancellationToken ) ;
4946 }
50-
51- private async Task < T > InvokeRestAsync < T > ( HttpMethod httpVerb , string resource , Dictionary < string , string > ? additionalHeaders , object ? requestObject ,
47+
48+ private async Task < T > InvokeRestAsync < T > ( HttpMethod httpVerb , string resource , Dictionary < string , string > ? additionalHeaders ,
49+ object ? requestObject ,
5250 CancellationToken cancellationToken )
5351 {
5452 return await DoInvokeRestAsync < T > ( httpVerb , resource , additionalHeaders , requestObject , true , cancellationToken ) ;
5553 }
56-
57- private async Task < T > DoInvokeRestAsync < T > ( HttpMethod httpMethod , string resource , Dictionary < string , string > ? additionalHeaders ,
54+
55+ private async Task < T > DoInvokeRestAsync < T > ( HttpMethod httpMethod , string resource , Dictionary < string , string > ? additionalHeaders ,
5856 object ? requestObject , bool handleResult , CancellationToken cancellationToken )
5957 {
6058 using var request = await BuildRequest ( httpMethod , resource , additionalHeaders , requestObject ) ;
61-
59+
6260 try
6361 {
6462 var response = await _httpClient . SendAsync ( request , cancellationToken ) ;
63+
6564 return await HandleResponse < T > ( handleResult , response ) ;
6665 }
6766 catch ( Exception ex )
6867 {
6968 _logger . LogError ( ex , "Exception occurred while invoking API: {Resource}" , resource ) ;
69+
7070 throw new ApiException ( $ "An error occurred while invoking the API on resource { resource } ", ex ) ;
7171 }
7272 }
73-
74- private async Task < HttpRequestMessage > BuildRequest ( HttpMethod httpMethod , string resource , Dictionary < string , string > ? additionalHeaders ,
73+
74+ private async Task < HttpRequestMessage > BuildRequest ( HttpMethod httpMethod , string resource ,
75+ Dictionary < string , string > ? additionalHeaders ,
7576 object ? requestObject )
7677 {
7778 var rootUrl = await _connectionConstantsService . GetApiUrl ( ) ;
@@ -80,10 +81,11 @@ private async Task<HttpRequestMessage> BuildRequest(HttpMethod httpMethod, strin
8081 var request = new HttpRequestMessage ( httpMethod , fullUrl ) ;
8182
8283 var jwtToken = await _authenticationTokensRepository . GetTokens ( ) ;
83- if ( jwtToken != null && ! jwtToken . JwtToken . IsNullOrEmpty ( ) )
84+ if ( jwtToken != null && ! jwtToken . JwtToken . IsNullOrEmpty ( ) )
8485 {
85- request . Headers . Add ( "authorization" , jwtToken . JwtToken ) ;
86+ request . Headers . Add ( "authorization" , jwtToken . JwtToken ) ;
8687 }
88+
8789 if ( additionalHeaders is { Count : > 0 } )
8890 {
8991 foreach ( var header in additionalHeaders )
@@ -97,14 +99,14 @@ private async Task<HttpRequestMessage> BuildRequest(HttpMethod httpMethod, strin
9799 var json = JsonHelper . Serialize ( requestObject ) ;
98100 request . Content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
99101 }
100-
102+
101103 return request ;
102104 }
103-
105+
104106 private async Task < T > HandleResponse < T > ( bool handleResult , HttpResponseMessage response )
105107 {
106108 var content = await response . Content . ReadAsStringAsync ( ) ;
107-
109+
108110 if ( response . IsSuccessStatusCode )
109111 {
110112 if ( handleResult )
@@ -128,12 +130,13 @@ private async Task<T> HandleResponse<T>(bool handleResult, HttpResponseMessage r
128130 {
129131 return DeserializeContent < T > ( content ) ;
130132 }
131-
133+
132134 _logger . LogError ( "API call failed with status code {StatusCode}: {ErrorMessage}" , response . StatusCode , errorMessage ) ;
135+
133136 throw new ApiException ( $ "API call failed with status code { response . StatusCode } : { errorMessage } ", response . StatusCode ) ;
134137 }
135138 }
136-
139+
137140 private T DeserializeContent < T > ( string content )
138141 {
139142 try
@@ -143,12 +146,13 @@ private T DeserializeContent<T>(string content)
143146 {
144147 throw new ApiException ( "Failed to deserialize the response content." ) ;
145148 }
146-
149+
147150 return result ;
148151 }
149152 catch ( JsonException ex )
150153 {
151154 _logger . LogError ( ex , "JSON deserialization error" ) ;
155+
152156 throw new ApiException ( "Failed to deserialize the response content." , ex ) ;
153157 }
154158 }
0 commit comments