Skip to content

Commit ba8a27d

Browse files
committed
Using the new HttpClient in AuthErrorHandler
1 parent c03f3ce commit ba8a27d

File tree

7 files changed

+69
-140
lines changed

7 files changed

+69
-140
lines changed

FirebaseAdmin/FirebaseAdmin.Tests/Auth/AuthErrorHandlerTest.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ public void KnownErrorCode(
6767
Content = new StringContent(json, Encoding.UTF8, "application/json"),
6868
};
6969

70-
var error = Assert.Throws<FirebaseAuthException>(
71-
() => AuthErrorHandler.Instance.ThrowIfError(resp, json));
70+
var error = AuthErrorHandler.Instance.HandleHttpErrorResponse(resp, json);
7271

7372
Assert.Equal(expectedCode, error.ErrorCode);
7473
Assert.Equal(expectedAuthCode, error.AuthErrorCode);
@@ -93,8 +92,7 @@ public void KnownErrorCodeWithDetails(
9392
Content = new StringContent(json, Encoding.UTF8, "application/json"),
9493
};
9594

96-
var error = Assert.Throws<FirebaseAuthException>(
97-
() => AuthErrorHandler.Instance.ThrowIfError(resp, json));
95+
var error = AuthErrorHandler.Instance.HandleHttpErrorResponse(resp, json);
9896

9997
Assert.Equal(expectedCode, error.ErrorCode);
10098
Assert.Equal(expectedAuthCode, error.AuthErrorCode);
@@ -117,8 +115,7 @@ public void UnknownErrorCode()
117115
Content = new StringContent(json, Encoding.UTF8, "application/json"),
118116
};
119117

120-
var error = Assert.Throws<FirebaseAuthException>(
121-
() => AuthErrorHandler.Instance.ThrowIfError(resp, json));
118+
var error = AuthErrorHandler.Instance.HandleHttpErrorResponse(resp, json);
122119

123120
Assert.Equal(ErrorCode.Internal, error.ErrorCode);
124121
Assert.Equal(
@@ -141,8 +138,7 @@ public void UnspecifiedErrorCode()
141138
Content = new StringContent(json, Encoding.UTF8, "application/json"),
142139
};
143140

144-
var error = Assert.Throws<FirebaseAuthException>(
145-
() => AuthErrorHandler.Instance.ThrowIfError(resp, json));
141+
var error = AuthErrorHandler.Instance.HandleHttpErrorResponse(resp, json);
146142

147143
Assert.Equal(ErrorCode.Internal, error.ErrorCode);
148144
Assert.Equal(
@@ -163,8 +159,7 @@ public void NoDetails()
163159
Content = new StringContent(json, Encoding.UTF8, "application/json"),
164160
};
165161

166-
var error = Assert.Throws<FirebaseAuthException>(
167-
() => AuthErrorHandler.Instance.ThrowIfError(resp, json));
162+
var error = AuthErrorHandler.Instance.HandleHttpErrorResponse(resp, json);
168163

169164
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
170165
Assert.Equal(
@@ -185,8 +180,7 @@ public void NonJson()
185180
Content = new StringContent(text, Encoding.UTF8, "text/plain"),
186181
};
187182

188-
var error = Assert.Throws<FirebaseAuthException>(
189-
() => AuthErrorHandler.Instance.ThrowIfError(resp, text));
183+
var error = AuthErrorHandler.Instance.HandleHttpErrorResponse(resp, text);
190184

191185
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
192186
Assert.Equal(

FirebaseAdmin/FirebaseAdmin.Tests/Auth/FirebaseUserManagerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ public async Task ListUsersNonJsonResponse()
734734
Assert.Equal(ErrorCode.Unknown, exception.ErrorCode);
735735
Assert.Equal(AuthErrorCode.UnexpectedResponse, exception.AuthErrorCode);
736736
Assert.Equal(
737-
"Error while parsing Auth service response.",
737+
"Error while parsing Auth service response: not json",
738738
exception.Message);
739739
Assert.NotNull(exception.HttpResponse);
740740
Assert.NotNull(exception.InnerException);

FirebaseAdmin/FirebaseAdmin/Auth/AuthErrorHandler.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using System;
1516
using System.Collections.Generic;
1617
using System.Net.Http;
18+
using FirebaseAdmin.Util;
1719
using Google.Apis.Json;
1820
using Newtonsoft.Json;
1921

@@ -23,7 +25,10 @@ namespace FirebaseAdmin.Auth
2325
/// Parses error responses received from the Auth service, and creates instances of
2426
/// <see cref="FirebaseAuthException"/>.
2527
/// </summary>
26-
internal sealed class AuthErrorHandler : HttpErrorHandler
28+
internal sealed class AuthErrorHandler
29+
: HttpErrorHandler<FirebaseAuthException>,
30+
IHttpRequestExceptionHandler<FirebaseAuthException>,
31+
IDeserializeExceptionHandler<FirebaseAuthException>
2732
{
2833
internal static readonly AuthErrorHandler Instance = new AuthErrorHandler();
2934

@@ -62,6 +67,28 @@ internal sealed class AuthErrorHandler : HttpErrorHandler
6267

6368
private AuthErrorHandler() { }
6469

70+
public FirebaseAuthException HandleHttpRequestException(
71+
HttpRequestException exception)
72+
{
73+
var temp = exception.ToFirebaseException();
74+
return new FirebaseAuthException(
75+
temp.ErrorCode,
76+
temp.Message,
77+
inner: temp.InnerException,
78+
response: temp.HttpResponse);
79+
}
80+
81+
public FirebaseAuthException HandleDeserializeException(
82+
Exception exception, ResponseInfo responseInfo)
83+
{
84+
return new FirebaseAuthException(
85+
ErrorCode.Unknown,
86+
$"Error while parsing Auth service response: {responseInfo.Body}",
87+
AuthErrorCode.UnexpectedResponse,
88+
inner: exception,
89+
response: responseInfo.HttpResponse);
90+
}
91+
6592
protected sealed override FirebaseExceptionArgs CreateExceptionArgs(
6693
HttpResponseMessage response, string body)
6794
{
@@ -81,7 +108,7 @@ protected sealed override FirebaseExceptionArgs CreateExceptionArgs(
81108
};
82109
}
83110

84-
protected override FirebaseException CreateException(FirebaseExceptionArgs args)
111+
protected override FirebaseAuthException CreateException(FirebaseExceptionArgs args)
85112
{
86113
return new FirebaseAuthException(
87114
args.Code,

FirebaseAdmin/FirebaseAdmin/Auth/FirebaseUserManager.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
using System.Net.Http;
1818
using System.Threading;
1919
using System.Threading.Tasks;
20+
using FirebaseAdmin.Util;
2021
using Google.Api.Gax;
2122
using Google.Api.Gax.Rest;
22-
using Google.Apis.Http;
2323
using Google.Apis.Json;
2424
using Google.Apis.Util;
2525
using Newtonsoft.Json.Linq;
@@ -36,7 +36,7 @@ internal class FirebaseUserManager : IDisposable
3636
{
3737
private const string IdTooklitUrl = "https://identitytoolkit.googleapis.com/v1/projects/{0}";
3838

39-
private readonly ConfigurableHttpClient httpClient;
39+
private readonly ErrorHandlingHttpClient<FirebaseAuthException> httpClient;
4040
private readonly string baseUrl;
4141

4242
internal FirebaseUserManager(FirebaseUserManagerArgs args)
@@ -47,7 +47,15 @@ internal FirebaseUserManager(FirebaseUserManagerArgs args)
4747
"Must initialize FirebaseApp with a project ID to manage users.");
4848
}
4949

50-
this.httpClient = args.ClientFactory.CreateAuthorizedHttpClient(args.Credential);
50+
this.httpClient = new ErrorHandlingHttpClient<FirebaseAuthException>(
51+
new ErrorHandlingHttpClientArgs<FirebaseAuthException>()
52+
{
53+
HttpClientFactory = args.ClientFactory,
54+
Credential = args.Credential,
55+
ErrorResponseHandler = AuthErrorHandler.Instance,
56+
RequestExceptionHandler = AuthErrorHandler.Instance,
57+
DeserializeExceptionHandler = AuthErrorHandler.Instance,
58+
});
5159
this.baseUrl = string.Format(IdTooklitUrl, args.ProjectId);
5260
}
5361

@@ -253,15 +261,7 @@ private async Task<UserRecord> GetUserAsync(
253261
return new UserRecord(result.Users[0]);
254262
}
255263

256-
private async Task<HttpExtensions.ParsedResponseInfo<TResult>> PostAndDeserializeAsync<TResult>(
257-
string path, object body, CancellationToken cancellationToken)
258-
{
259-
var response = await this.PostAsync(path, body, cancellationToken)
260-
.ConfigureAwait(false);
261-
return response.SafeDeserialize<TResult>();
262-
}
263-
264-
private async Task<HttpExtensions.ResponseInfo> PostAsync(
264+
private async Task<DeserializedResponseInfo<TResult>> PostAndDeserializeAsync<TResult>(
265265
string path, object body, CancellationToken cancellationToken)
266266
{
267267
var request = new HttpRequestMessage()
@@ -270,14 +270,8 @@ private async Task<UserRecord> GetUserAsync(
270270
RequestUri = new Uri($"{this.baseUrl}/{path}"),
271271
Content = NewtonsoftJsonSerializer.Instance.CreateJsonHttpContent(body),
272272
};
273-
return await this.SendAsync(request, cancellationToken).ConfigureAwait(false);
274-
}
275-
276-
private async Task<HttpExtensions.ResponseInfo> SendAsync(
277-
HttpRequestMessage request, CancellationToken cancellationToken)
278-
{
279273
return await this.httpClient
280-
.SendAndReadAsync(request, cancellationToken)
274+
.SendAndDeserializeAsync<TResult>(request, cancellationToken)
281275
.ConfigureAwait(false);
282276
}
283277

FirebaseAdmin/FirebaseAdmin/Auth/HttpExtensions.cs

Lines changed: 0 additions & 91 deletions
This file was deleted.

FirebaseAdmin/FirebaseAdmin/Auth/ListUsersRequest.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Net.Http;
2020
using System.Threading;
2121
using System.Threading.Tasks;
22+
using FirebaseAdmin.Util;
2223
using Google.Apis.Discovery;
2324
using Google.Apis.Http;
2425
using Google.Apis.Requests;
@@ -35,10 +36,12 @@ internal sealed class ListUsersRequest : IClientServiceRequest<ExportedUserRecor
3536
private const int MaxListUsersResults = 1000;
3637

3738
private readonly string baseUrl;
38-
private readonly ConfigurableHttpClient httpClient;
39+
private readonly ErrorHandlingHttpClient<FirebaseAuthException> httpClient;
3940

4041
private ListUsersRequest(
41-
string baseUrl, ConfigurableHttpClient httpClient, ListUsersOptions options)
42+
string baseUrl,
43+
ErrorHandlingHttpClient<FirebaseAuthException> httpClient,
44+
ListUsersOptions options)
4245
{
4346
this.baseUrl = baseUrl;
4447
this.httpClient = httpClient;
@@ -171,17 +174,10 @@ private void AddOrUpdate(string paramName, string value)
171174
private async Task<DownloadAccountResponse> SendAndDeserializeAsync(
172175
HttpRequestMessage request, CancellationToken cancellationToken)
173176
{
174-
var response = await this.SendAsync(request, cancellationToken)
175-
.ConfigureAwait(false);
176-
var parsed = response.SafeDeserialize<DownloadAccountResponse>();
177-
return parsed.Result;
178-
}
179-
180-
private async Task<HttpExtensions.ResponseInfo> SendAsync(
181-
HttpRequestMessage request, CancellationToken cancellationToken)
182-
{
183-
return await this.httpClient.SendAndReadAsync(request, cancellationToken)
177+
var response = await this.httpClient
178+
.SendAndDeserializeAsync<DownloadAccountResponse>(request, cancellationToken)
184179
.ConfigureAwait(false);
180+
return response.Result;
185181
}
186182

187183
/// <summary>
@@ -191,11 +187,13 @@ private async Task<DownloadAccountResponse> SendAndDeserializeAsync(
191187
internal sealed class Factory
192188
{
193189
private readonly string baseUrl;
194-
private readonly ConfigurableHttpClient httpClient;
190+
private readonly ErrorHandlingHttpClient<FirebaseAuthException> httpClient;
195191
private readonly ListUsersOptions options;
196192

197193
internal Factory(
198-
string baseUrl, ConfigurableHttpClient httpClient, ListUsersOptions options = null)
194+
string baseUrl,
195+
ErrorHandlingHttpClient<FirebaseAuthException> httpClient,
196+
ListUsersOptions options = null)
199197
{
200198
this.baseUrl = baseUrl;
201199
this.httpClient = httpClient;

FirebaseAdmin/FirebaseAdmin/Util/ErrorHandlingHttpClient.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,19 @@ internal async Task<DeserializedResponseInfo<TResult>> SendAndDeserializeAsync<T
8181
}
8282
}
8383

84+
internal async Task<HttpResponseMessage> SendAsync(
85+
HttpRequestMessage request, CancellationToken cancellationToken = default)
86+
{
87+
return await this.httpClient.SendAsync(request, cancellationToken)
88+
.ConfigureAwait(false);
89+
}
90+
8491
private async Task<ResponseInfo> SendAndReadAsync(
8592
HttpRequestMessage request, CancellationToken cancellationToken)
8693
{
8794
try
8895
{
89-
var response = await this.httpClient.SendAsync(request, cancellationToken)
96+
var response = await this.SendAsync(request, cancellationToken)
9097
.ConfigureAwait(false);
9198
var body = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
9299
if (!response.IsSuccessStatusCode)

0 commit comments

Comments
 (0)