-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiClientBase.cs
120 lines (101 loc) · 2.66 KB
/
ApiClientBase.cs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Microsoft.Extensions.Logging;
using System.Net.Http.Json;
using System.Runtime.CompilerServices;
namespace ApiClientBaseLibrary;
/// <summary>
/// since Refit doesn't seem to work with authentication in Blazor WebAssembly, this is kind of the next best thing
/// </summary>
public abstract class ApiClientBase(HttpClient httpClient, ILogger<ApiClientBase> logger)
{
protected readonly ILogger<ApiClientBase> Logger = logger;
protected HttpClient Client { get; } = httpClient;
protected abstract Task<bool> HandleException(HttpResponseMessage? response, Exception exception, [CallerMemberName] string? methodName = null);
protected async Task<T?> GetAsync<T>(string uri)
{
Logger.LogDebug("GET {Uri}", uri);
var response = await Client.GetAsync(uri);
try
{
response.EnsureSuccessStatusCode();
Logger.LogDebug("GET {Uri} successful", uri);
return await response.Content.ReadFromJsonAsync<T>();
}
catch (Exception exc)
{
Logger.LogError(exc, "Error in {Method}", nameof(GetAsync));
if (!await HandleException(response, exc))
{
throw;
}
return default;
}
}
protected async Task<TResult?> PostWithResultAsync<TResult>(string uri)
{
var response = await Client.PostAsync(uri, null);
try
{
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<TResult>();
}
catch (Exception exc)
{
Logger.LogError(exc, "Error in {Method}", nameof(PostWithResultAsync));
if (!await HandleException(response, exc))
{
throw;
}
}
return default;
}
protected async Task PostWithInputAsync<T>(string uri, T value)
{
var response = await Client.PostAsJsonAsync(uri, value);
try
{
response.EnsureSuccessStatusCode();
}
catch (Exception exc)
{
Logger.LogError(exc, "Error in {Method}", nameof(PostWithInputAsync));
if (!await HandleException(response, exc))
{
throw;
}
}
}
protected async Task<TResult?> PostWithInputAndResultAsync<TResult>(string uri, TResult input)
{
var response = await Client.PostAsJsonAsync(uri, input);
try
{
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<TResult>();
}
catch (Exception exc)
{
Logger.LogError(exc, "Error in {Method}", nameof(PostWithInputAndResultAsync));
if (!await HandleException(response, exc))
{
throw;
}
}
return default;
}
protected async Task DeleteAsync(string uri)
{
var response = await Client.DeleteAsync(uri);
try
{
response.EnsureSuccessStatusCode();
}
catch (Exception exc)
{
Logger.LogError(exc, "Error in {Method}", nameof(DeleteAsync));
if (!await HandleException(response, exc))
{
throw;
}
}
}
}