Skip to content

Commit 9af016c

Browse files
committed
Removed the HttpClient as Singleton in LrsClient
Decided to not leave the HttpClient instance as a Singleton in the LrsClient class. This made the class awkward to use. I'll document that the best use of the LrsClient is as a Singleton for users of the class, but this leave the users the possibility to manage the lifecycle of the LrsClient more easily.
1 parent dd5da8e commit 9af016c

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

src/Mos.xApi/Client/LrsClient.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,13 @@ public class LrsClient : ILrsClient
2222
/// <summary>
2323
/// Singleton instanciated HttpClient
2424
/// </summary>
25-
private static readonly HttpClient HttpClient;
25+
private readonly HttpClient HttpClient;
2626

2727
/// <summary>
2828
/// The endpoint (URL) for the statement API endpoint.
2929
/// </summary>
3030
private readonly string _statementEndPoint;
3131

32-
/// <summary>
33-
/// Static initializer, instanciates the HttpClient.
34-
/// </summary>
35-
static LrsClient()
36-
{
37-
HttpClient = new HttpClient();
38-
}
39-
4032
/// <summary>
4133
/// Initializes a new instance of the LrsClient.
4234
/// </summary>
@@ -45,15 +37,13 @@ static LrsClient()
4537
/// <param name="xApiVersion">The version of xApi used.</param>
4638
public LrsClient(Uri lrsBaseUrl, string statementEndPoint = "statements", string xApiVersion = "1.0.0")
4739
{
48-
HttpClient.BaseAddress = lrsBaseUrl;
49-
_statementEndPoint = statementEndPoint;
50-
51-
if (HttpClient.DefaultRequestHeaders.Any(x => x.Key == "X-Experience-API-Version"))
40+
HttpClient = new HttpClient()
5241
{
53-
HttpClient.DefaultRequestHeaders.Remove("X-Experience-API-Version");
54-
}
55-
42+
BaseAddress = lrsBaseUrl
43+
};
5644
HttpClient.DefaultRequestHeaders.Add("X-Experience-API-Version", xApiVersion);
45+
46+
_statementEndPoint = statementEndPoint;
5747
}
5848

5949
/// <summary>
@@ -123,7 +113,7 @@ public async Task<Statement> GetVoidedStatementAsync(Guid statementId)
123113
{
124114
if (!response.IsSuccessStatusCode)
125115
{
126-
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
116+
if (response.StatusCode == HttpStatusCode.NotFound)
127117
{
128118
return null;
129119
}

0 commit comments

Comments
 (0)