@@ -10,7 +10,6 @@ namespace OneShelf.Common.Api.Client;
10
10
public class ApiClientBase < TClient >
11
11
where TClient : ApiClientBase < TClient >
12
12
{
13
- private readonly ApiClientOptions < TClient > _options ;
14
13
private readonly IHttpClientFactory _httpClientFactory ;
15
14
16
15
private readonly JsonSerializerOptions _jsonSerializerOptions = new ( )
@@ -20,27 +19,29 @@ public class ApiClientBase<TClient>
20
19
21
20
public ApiClientBase ( IOptions < ApiClientOptions < TClient > > options , IHttpClientFactory httpClientFactory )
22
21
{
23
- _options = options . Value ;
22
+ Options = options . Value ;
24
23
_httpClientFactory = httpClientFactory ;
25
24
}
26
25
27
26
protected ApiClientBase ( ApiClientOptions < TClient > options , IHttpClientFactory httpClientFactory )
28
27
{
29
- _options = options ;
28
+ Options = options ;
30
29
_httpClientFactory = httpClientFactory ;
31
30
}
31
+
32
+ protected ApiClientOptions < TClient > Options { get ; }
32
33
33
- public Identity GetServiceIdentity ( )
34
+ public Identity GetServiceIdentity ( string ? conditionalStreamId = null )
34
35
=> new ( )
35
36
{
36
- Hash = _options . ServiceCode ,
37
+ Hash = Options . GetServiceCode ( conditionalStreamId ) ?? throw new ( "The service identity code is not configured." ) ,
37
38
} ;
38
39
39
- protected async Task < TResponse > PostWithCode < TRequest , TResponse > ( string url , TRequest request , CancellationToken cancellationToken = default , ApiTraceBag ? apiTraceBag = null )
40
+ protected async Task < TResponse > PostWithCode < TRequest , TResponse > ( string url , TRequest request , CancellationToken cancellationToken = default , ApiTraceBag ? apiTraceBag = null , string ? conditionalStreamId = null )
40
41
{
41
42
var started = DateTime . Now ;
42
43
using var client = _httpClientFactory . CreateClient ( ) ;
43
- using var httpResponseMessage = await client . PostAsJsonAsync ( new Uri ( _options . Endpoint , WithCode ( url ) ) , request , _jsonSerializerOptions , cancellationToken ) ;
44
+ using var httpResponseMessage = await client . PostAsJsonAsync ( new Uri ( Options . GetEndpoint ( conditionalStreamId ) , WithCode ( url ) ) , request , _jsonSerializerOptions , cancellationToken ) ;
44
45
if ( httpResponseMessage . StatusCode == HttpStatusCode . Unauthorized )
45
46
{
46
47
throw new UnauthorizedException ( await httpResponseMessage . Content . ReadAsStringAsync ( cancellationToken ) ) ;
@@ -64,17 +65,17 @@ protected async Task<TResponse> PostWithCode<TRequest, TResponse>(string url, TR
64
65
Method = "POST" ,
65
66
Request = MaskIdentity ( request ) ,
66
67
Response = response ,
67
- Url = new Uri ( _options . Endpoint , $ "{ url } ?code={{code}}") . ToString ( ) ,
68
+ Url = new Uri ( Options . GetEndpoint ( conditionalStreamId ) , $ "{ url } ?code={{code}}") . ToString ( ) ,
68
69
TimeTaken = DateTime . Now - started ,
69
70
} ) ;
70
71
71
72
return response ;
72
73
}
73
74
74
- protected async Task < byte [ ] > PostWithCode < TRequest > ( string url , TRequest request )
75
+ protected async Task < byte [ ] > PostWithCode < TRequest > ( string url , TRequest request , string ? conditionalStreamId = null )
75
76
{
76
77
using var client = _httpClientFactory . CreateClient ( ) ;
77
- using var response = await client . PostAsJsonAsync ( new Uri ( _options . Endpoint , WithCode ( url ) ) , request , _jsonSerializerOptions ) ;
78
+ using var response = await client . PostAsJsonAsync ( new Uri ( Options . GetEndpoint ( conditionalStreamId ) , WithCode ( url ) ) , request , _jsonSerializerOptions ) ;
78
79
if ( response . StatusCode == HttpStatusCode . Unauthorized )
79
80
{
80
81
throw new UnauthorizedException ( await response . Content . ReadAsStringAsync ( ) ) ;
@@ -94,11 +95,11 @@ protected async Task<byte[]> PostWithCode<TRequest>(string url, TRequest request
94
95
return await response . Content . ReadAsByteArrayAsync ( ) ;
95
96
}
96
97
97
- protected async Task < TResponse > Post < TRequest , TResponse > ( string url , TRequest request , Action ? unauthorized = null , CancellationToken cancellationToken = default , ApiTraceBag ? apiTraceBag = null )
98
+ protected async Task < TResponse > Post < TRequest , TResponse > ( string url , TRequest request , Action ? unauthorized = null , CancellationToken cancellationToken = default , ApiTraceBag ? apiTraceBag = null , string ? conditionalStreamId = null )
98
99
{
99
100
var started = DateTime . Now ;
100
101
using var client = _httpClientFactory . CreateClient ( ) ;
101
- var uri = new Uri ( _options . Endpoint , url ) ;
102
+ var uri = new Uri ( Options . GetEndpoint ( conditionalStreamId ) , url ) ;
102
103
using var httpResponseMessage = await client . PostAsJsonAsync ( uri , request , _jsonSerializerOptions , cancellationToken ) ;
103
104
if ( httpResponseMessage . StatusCode == HttpStatusCode . Unauthorized )
104
105
{
@@ -131,11 +132,11 @@ protected async Task<TResponse> Post<TRequest, TResponse>(string url, TRequest r
131
132
return response ;
132
133
}
133
134
134
- protected async Task < TResponse > Get < TResponse > ( string url , CancellationToken cancellationToken = default , ApiTraceBag ? apiTraceBag = null )
135
+ protected async Task < TResponse > Get < TResponse > ( string url , CancellationToken cancellationToken = default , ApiTraceBag ? apiTraceBag = null , string ? conditionalStreamId = null )
135
136
{
136
137
var started = DateTime . Now ;
137
138
using var client = _httpClientFactory . CreateClient ( ) ;
138
- var uri = new Uri ( _options . Endpoint , url ) ;
139
+ var uri = new Uri ( Options . GetEndpoint ( conditionalStreamId ) , url ) ;
139
140
using var httpResponseMessage = await client . GetAsync ( uri , cancellationToken ) ;
140
141
141
142
if ( httpResponseMessage . StatusCode == HttpStatusCode . TooManyRequests )
@@ -162,11 +163,11 @@ protected async Task<TResponse> Get<TResponse>(string url, CancellationToken can
162
163
return response ;
163
164
}
164
165
165
- protected async Task < string > PostDirect ( string url , string request , Action ? unauthorized = null , ApiTraceBag ? apiTraceBag = null )
166
+ protected async Task < string > PostDirect ( string url , string request , Action ? unauthorized = null , ApiTraceBag ? apiTraceBag = null , string ? conditionalStreamId = null )
166
167
{
167
168
var started = DateTime . Now ;
168
169
using var client = _httpClientFactory . CreateClient ( ) ;
169
- using var httpResponseMessage = await client . PostAsync ( new Uri ( _options . Endpoint , url ) , new StringContent ( request , MediaTypeHeaderValue . Parse ( "application/json" ) ) ) ;
170
+ using var httpResponseMessage = await client . PostAsync ( new Uri ( Options . GetEndpoint ( conditionalStreamId ) , url ) , new StringContent ( request , MediaTypeHeaderValue . Parse ( "application/json" ) ) ) ;
170
171
if ( httpResponseMessage . StatusCode == HttpStatusCode . Unauthorized )
171
172
{
172
173
unauthorized ? . Invoke ( ) ;
@@ -191,23 +192,23 @@ protected async Task<string> PostDirect(string url, string request, Action? unau
191
192
Method = "POST" ,
192
193
Request = MaskIdentity ( request ) ,
193
194
Response = response ,
194
- Url = new Uri ( _options . Endpoint , $ "{ url } ?code={{code}}") . ToString ( ) ,
195
+ Url = new Uri ( Options . GetEndpoint ( conditionalStreamId ) , $ "{ url } ?code={{code}}") . ToString ( ) ,
195
196
TimeTaken = DateTime . Now - started ,
196
197
} ) ;
197
198
198
199
return response ;
199
200
}
200
201
201
- protected async Task Ping ( string url )
202
+ protected async Task Ping ( string url , string ? conditionalStreamId = null )
202
203
{
203
204
using var client = _httpClientFactory . CreateClient ( ) ;
204
- using var response = await client . GetAsync ( new Uri ( _options . Endpoint , url ) ) ;
205
+ using var response = await client . GetAsync ( new Uri ( Options . GetEndpoint ( conditionalStreamId ) , url ) ) ;
205
206
response . EnsureSuccessStatusCode ( ) ;
206
207
}
207
208
208
- private string WithCode ( string url )
209
+ private string WithCode ( string url , string ? conditionalStreamId = null )
209
210
{
210
- return $ "{ url } ?code={ _options . MasterCode } ";
211
+ return $ "{ url } ?code={ Options . GetMasterCode ( conditionalStreamId ) } ";
211
212
}
212
213
213
214
private static TRequest MaskIdentity < TRequest > ( TRequest request )
0 commit comments