Skip to content

Commit f114f35

Browse files
committed
Cleaning up the code
1 parent 437bd9e commit f114f35

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

FirebaseAdmin/FirebaseAdmin/Auth/FirebaseAuth.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,8 @@ void IFirebaseService.Delete()
321321
lock (this.authLock)
322322
{
323323
this.deleted = true;
324-
if (this.tokenFactory.IsValueCreated)
325-
{
326-
this.tokenFactory.Value.Dispose();
327-
}
328-
329-
if (this.userManager.IsValueCreated)
330-
{
331-
this.userManager.Value.Dispose();
332-
}
324+
this.tokenFactory.DisposeIfCreated();
325+
this.userManager.DisposeIfCreated();
333326
}
334327
}
335328

FirebaseAdmin/FirebaseAdmin/Auth/FirebaseUserManager.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,36 @@ public void Dispose()
8888
private async Task<TResult> PostAndDeserializeAsync<TResult>(
8989
string path, object body, CancellationToken cancellationToken)
9090
{
91+
var json = await this.PostAsync(path, body, cancellationToken).ConfigureAwait(false);
9192
try
9293
{
93-
var json = await this.PostAsync(path, body, cancellationToken)
94-
.ConfigureAwait(false);
9594
return NewtonsoftJsonSerializer.Instance.Deserialize<TResult>(json);
9695
}
97-
catch (FirebaseException)
98-
{
99-
throw;
100-
}
10196
catch (Exception e)
10297
{
103-
throw new FirebaseException("Error while calling Firebase Auth service", e);
98+
throw new FirebaseException("Error while parsing Auth service response", e);
10499
}
105100
}
106101

107102
private async Task<string> PostAsync(
108103
string path, object body, CancellationToken cancellationToken)
109104
{
110-
var requestUri = $"{this.baseUrl}/{path}";
111-
var response = await this.httpClient
112-
.PostJsonAsync(requestUri, body, cancellationToken)
105+
try
106+
{
107+
var url = $"{this.baseUrl}/{path}";
108+
return await this.SendRequestAsync(url, body, cancellationToken)
109+
.ConfigureAwait(false);
110+
}
111+
catch (HttpRequestException e)
112+
{
113+
throw new FirebaseException("Error while calling Firebase Auth service", e);
114+
}
115+
}
116+
117+
private async Task<string> SendRequestAsync(
118+
string url, object body, CancellationToken cancellationToken)
119+
{
120+
var response = await this.httpClient.PostJsonAsync(url, body, cancellationToken)
113121
.ConfigureAwait(false);
114122
var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
115123
if (!response.IsSuccessStatusCode)

FirebaseAdmin/FirebaseAdmin/Extensions.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using System;
1616
using System.Net.Http;
17+
using System.Text;
1718
using System.Threading;
1819
using System.Threading.Tasks;
1920
using Google.Apis.Auth.OAuth2;
@@ -93,7 +94,7 @@ public static async Task<HttpResponseMessage> PostJsonAsync<T>(
9394
this HttpClient client, string requestUri, T body, CancellationToken cancellationToken)
9495
{
9596
var payload = NewtonsoftJsonSerializer.Instance.Serialize(body);
96-
var content = new StringContent(payload, System.Text.Encoding.UTF8, "application/json");
97+
var content = new StringContent(payload, Encoding.UTF8, "application/json");
9798
return await client.PostAsync(requestUri, content, cancellationToken)
9899
.ConfigureAwait(false);
99100
}
@@ -108,5 +109,19 @@ public static long UnixTimestamp(this IClock clock)
108109
var timeSinceEpoch = clock.UtcNow.Subtract(new DateTime(1970, 1, 1));
109110
return (long)timeSinceEpoch.TotalSeconds;
110111
}
112+
113+
/// <summary>
114+
/// Disposes a lazy-initialized object if the object has already been created.
115+
/// </summary>
116+
/// <param name="lazy">The lazy initializer containing a disposable object.</param>
117+
/// <typeparam name="T">Type of the object that needs to be disposed.</typeparam>
118+
public static void DisposeIfCreated<T>(this Lazy<T> lazy)
119+
where T : IDisposable
120+
{
121+
if (lazy.IsValueCreated)
122+
{
123+
lazy.Value.Dispose();
124+
}
125+
}
111126
}
112127
}

0 commit comments

Comments
 (0)