generated from goatcorp/SamplePlugin
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provide an httpclient generator, to control reuse and dynamically cha…
…nging base url
- Loading branch information
Showing
5 changed files
with
75 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Net.Http; | ||
using Dalamud.Plugin.Services; | ||
|
||
namespace ScoutHelper.Utils; | ||
|
||
public class HttpClientGenerator : IDisposable { | ||
private readonly IPluginLog _log; | ||
private readonly Func<string> _baseUrlSupplier; | ||
private readonly Action<HttpClient> _clientConfigurer; | ||
|
||
private HttpClient _client = new(); | ||
|
||
public HttpClient Client { | ||
get { | ||
var latestUrl = _baseUrlSupplier().AsUri(); | ||
if (_client.BaseAddress != latestUrl) { | ||
InitializeNewClient(); | ||
} | ||
|
||
return _client; | ||
} | ||
} | ||
|
||
public HttpClientGenerator(IPluginLog log, Func<string> baseUrlSupplier, Action<HttpClient> clientConfigurer) { | ||
_log = log; | ||
_baseUrlSupplier = baseUrlSupplier; | ||
_clientConfigurer = clientConfigurer; | ||
|
||
InitializeNewClient(); | ||
} | ||
|
||
public void Dispose() { | ||
_client.Dispose(); | ||
|
||
GC.SuppressFinalize(this); | ||
} | ||
|
||
private void InitializeNewClient() { | ||
var client = new HttpClient(); | ||
client.BaseAddress = _baseUrlSupplier().AsUri(); | ||
_log.Debug("generating a new http client for base address: {0:l}", client.BaseAddress.ToString()); | ||
client.DefaultRequestHeaders.UserAgent.Add(Constants.UserAgent); | ||
client.DefaultRequestHeaders.Accept.Add(Constants.MediaTypeJson); | ||
_clientConfigurer(client); | ||
|
||
_client.Dispose(); | ||
_client = client; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters