Merge pull request #45 from bluepilledgreat/reuse-httpclient

reuse httpclient
This commit is contained in:
pizzaboxer 2022-11-14 17:15:51 +00:00 committed by GitHub
commit c6410eb407
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 30 deletions

View File

@ -1,6 +1,7 @@
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Net;
using System.Net.Http; using System.Net.Http;
using Microsoft.Win32; using Microsoft.Win32;
@ -66,7 +67,7 @@ namespace Bloxstrap
"By default, two mod presets are provided for restoring the old death\n" + "By default, two mod presets are provided for restoring the old death\n" +
"sound and the old mouse cursor.\n"; "sound and the old mouse cursor.\n";
private static readonly HttpClient Client = new(); public static readonly HttpClient Client = new(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.All });
private string? LaunchCommandLine; private string? LaunchCommandLine;

View File

@ -104,9 +104,7 @@ namespace Bloxstrap.Helpers
string baseUrl = BuildBaseUrl(channel); string baseUrl = BuildBaseUrl(channel);
string lastDeploy = ""; string lastDeploy = "";
using (HttpClient client = new()) string deployHistory = await Bootstrapper.Client.GetStringAsync($"{baseUrl}/DeployHistory.txt");
{
string deployHistory = await client.GetStringAsync($"{baseUrl}/DeployHistory.txt");
using (StringReader reader = new(deployHistory)) using (StringReader reader = new(deployHistory))
{ {
@ -118,7 +116,6 @@ namespace Bloxstrap.Helpers
lastDeploy = line; lastDeploy = line;
} }
} }
}
if (String.IsNullOrEmpty(lastDeploy)) if (String.IsNullOrEmpty(lastDeploy))
throw new Exception($"Could not get latest deploy for channel {channel}"); throw new Exception($"Could not get latest deploy for channel {channel}");

View File

@ -97,16 +97,13 @@ namespace Bloxstrap.Helpers.Integrations
Debug.WriteLine("Installing/Updating rbxfpsunlocker..."); Debug.WriteLine("Installing/Updating rbxfpsunlocker...");
using (HttpClient client = new()) byte[] bytes = await Bootstrapper.Client.GetByteArrayAsync(downloadUrl);
{
byte[] bytes = await client.GetByteArrayAsync(downloadUrl);
using (MemoryStream zipStream = new(bytes)) using (MemoryStream zipStream = new(bytes))
{ {
ZipArchive zip = new(zipStream); ZipArchive zip = new(zipStream);
zip.ExtractToDirectory(folderLocation, true); zip.ExtractToDirectory(folderLocation, true);
} }
}
if (!File.Exists(settingsLocation)) if (!File.Exists(settingsLocation))
{ {

View File

@ -75,11 +75,8 @@ namespace Bloxstrap.Helpers.RSMM
string pkgManifestUrl = $"{DeployManager.BaseUrl}/{versionGuid}-rbxPkgManifest.txt"; string pkgManifestUrl = $"{DeployManager.BaseUrl}/{versionGuid}-rbxPkgManifest.txt";
string pkgManifestData; string pkgManifestData;
using (HttpClient http = new()) var getData = Bootstrapper.Client.GetStringAsync(pkgManifestUrl);
{
var getData = http.GetStringAsync(pkgManifestUrl);
pkgManifestData = await getData.ConfigureAwait(false); pkgManifestData = await getData.ConfigureAwait(false);
}
return new PackageManifest(pkgManifestData); return new PackageManifest(pkgManifestData);
} }

View File

@ -15,13 +15,15 @@ namespace Bloxstrap.Helpers
public static async Task<T?> GetJson<T>(string url) public static async Task<T?> GetJson<T>(string url)
{ {
using (HttpClient client = new()) var request = await Bootstrapper.Client.SendAsync(new()
{ {
client.DefaultRequestHeaders.Add("User-Agent", Program.ProjectRepository); RequestUri = new Uri(url),
Headers = {
string json = await client.GetStringAsync(url); { "User-Agent", Program.ProjectRepository }
return JsonSerializer.Deserialize<T>(json);
} }
});
var json = await request.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<T>(json);
} }
public static string MD5File(string filename) public static string MD5File(string filename)