Skip to content

Commit b13ed85

Browse files
committed
refactor: cleanup
1 parent bedc647 commit b13ed85

28 files changed

+529
-499
lines changed
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Threading.Tasks;
41
using ByteSync.Common.Business.Announcements;
52
using ByteSync.Interfaces.Controls.Communications.Http;
63

@@ -10,13 +7,13 @@ public class AnnouncementApiClient : IAnnouncementApiClient
107
{
118
private readonly IApiInvoker _apiInvoker;
129
private readonly ILogger<AnnouncementApiClient> _logger;
13-
10+
1411
public AnnouncementApiClient(IApiInvoker apiInvoker, ILogger<AnnouncementApiClient> logger)
1512
{
1613
_apiInvoker = apiInvoker;
1714
_logger = logger;
1815
}
19-
16+
2017
public async Task<List<Announcement>> GetAnnouncements()
2118
{
2219
try
@@ -26,7 +23,8 @@ public async Task<List<Announcement>> GetAnnouncements()
2623
catch (Exception ex)
2724
{
2825
_logger.LogError(ex, "Error while retrieving announcements");
26+
2927
throw;
3028
}
3129
}
32-
}
30+
}

src/ByteSync.Client/Services/Communications/Api/ApiInvoker.cs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
using System.Net;
2-
using System.Net.Http;
1+
using System.Net.Http;
32
using System.Text;
43
using System.Text.Json;
54
using System.Threading;
6-
using System.Threading.Tasks;
75
using ByteSync.Common.Controls.Json;
8-
using ByteSync.Common.Helpers;
96
using ByteSync.Exceptions;
107
using ByteSync.Interfaces.Controls.Communications.Http;
118
using 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
}

src/ByteSync.Client/Services/Communications/Api/AuthApiClient.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Runtime.CompilerServices;
2-
using System.Threading.Tasks;
32
using ByteSync.Common.Business.Auth;
43
using ByteSync.Interfaces.Controls.Communications.Http;
54
using Serilog;
@@ -20,33 +19,33 @@ public AuthApiClient(IApiInvoker apiInvoker)
2019
try
2120
{
2221
var result = await _apiInvoker.PostAsync<InitialAuthenticationResponse>($"auth/login", loginData);
23-
22+
2423
return result;
2524
}
2625
catch (Exception ex)
2726
{
2827
LogError(ex);
29-
28+
3029
throw;
3130
}
3231
}
33-
32+
3433
public async Task<RefreshTokensResponse?> RefreshAuthenticationTokens(RefreshTokensData refreshTokensData)
3534
{
3635
try
3736
{
3837
var result = await _apiInvoker.PostAsync<RefreshTokensResponse>($"auth/refreshTokens", refreshTokensData);
39-
38+
4039
return result;
4140
}
4241
catch (Exception ex)
4342
{
4443
LogError(ex);
45-
44+
4645
throw;
4746
}
4847
}
49-
48+
5049
private void LogError(Exception exception, [CallerMemberName] string caller = "")
5150
{
5251
// ReSharper disable once TemplateIsNotCompileTimeConstantProblem

0 commit comments

Comments
 (0)