Skip to content

Commit

Permalink
Use VoidX.WPF for HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Sep 4, 2024
1 parent df28877 commit be029a7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 12 deletions.
7 changes: 5 additions & 2 deletions CavernSamples/CavernizeGUI/CavernizeGUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
</ItemGroup>

<ItemGroup>
<Compile Include="..\VoidX.WPF\TaskEngine.cs" Link="Tools\TaskEngine.cs" />
<Compile Include="..\VoidX.WPF\FFmpeg.cs" Link="VoidX.WPF\FFmpeg.cs" />
<Compile Include="..\VoidX.WPF\HTTP.cs" Link="VoidX.WPF\HTTP.cs" />
<Compile Include="..\VoidX.WPF\TaskEngine.cs" Link="VoidX.WPF\TaskEngine.cs" />
<Compile Include="..\VoidX.WPF\UpdateCheck.cs" Link="VoidX.WPF\UpdateCheck.cs" />
</ItemGroup>

<ItemGroup>
Expand All @@ -33,7 +36,7 @@
</ItemGroup>

<ItemGroup>
<Resource Include="..\_Common\CavernLogo.png" Link="Resources\CavernLogo.png"/>
<Resource Include="..\_Common\CavernLogo.png" Link="Resources\CavernLogo.png" />
</ItemGroup>

<ItemGroup>
Expand Down
File renamed without changes.
67 changes: 67 additions & 0 deletions CavernSamples/VoidX.WPF/HTTP.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Runtime.CompilerServices;

namespace VoidX.WPF {
/// <summary>
/// HTTP utilities.
/// </summary>
static class HTTP {
/// <summary>
/// Gets a HTTP resource with a timeout.
/// </summary>
public static string GET(string url, int timeoutSeconds = 5) {
HttpClient client = new HttpClient {
Timeout = TimeSpan.FromSeconds(timeoutSeconds)
};

try {
HttpResponseMessage response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode) {
return response.Content.ReadAsStringAsync().Result;
}
} catch { }
return null;
}

/// <summary>
/// Sends a POST request of key-value pairs with a timeout.
/// </summary>
public static string POST(string url, KeyValuePair<string, string>[] data, int timeoutSeconds = 5) {
using FormUrlEncodedContent content = new FormUrlEncodedContent(data);
return POST(url, content, timeoutSeconds);
}

/// <summary>
/// Sends a POST request of large binary data with a timeout.
/// </summary>
public static string POST(string url, (string key, byte[] value)[] data, int timeoutSeconds = 5) {
using MultipartFormDataContent form = new MultipartFormDataContent();
for (int i = 0; i < data.Length; i++) {
form.Add(new ByteArrayContent(data[i].value), data[i].key);
}
return POST(url, form, timeoutSeconds);
}

/// <summary>
/// Sends an arbitrary POST request.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string POST(string url, HttpContent content, int timeoutSeconds = 5) {
using HttpClient client = new HttpClient() {
Timeout = TimeSpan.FromSeconds(timeoutSeconds)
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url) {
Content = content
};
try {
HttpResponseMessage response = client.SendAsync(request).Result;
if (response.IsSuccessStatusCode) {
return response.Content.ReadAsStringAsync().Result;
}
} catch { }
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;

Expand All @@ -21,22 +20,17 @@ public static class UpdateCheck {
/// </summary>
/// <param name="lastCheck">Last time an update check was performed</param>
/// <param name="onChecked">When the check was performed, call this function - used to keep track of the last check</param>
static async void CheckForUpdate(DateTime lastCheck, Action onChecked) {
static void CheckForUpdate(DateTime lastCheck, Action onChecked) {
if (DateTime.Now < lastCheck + TimeSpan.FromDays(7)) {
return;
}

HttpClient client = new();
HttpResponseMessage response;
try {
response = await client.GetAsync(updateLocation);
} catch {
string body = HTTP.GET(updateLocation);
if (!int.TryParse(body, out int version)) {
return;
}
response.EnsureSuccessStatusCode();
string body = await response.Content.ReadAsStringAsync();

if (thisRevision < int.Parse(body)) {
if (thisRevision < version) {
if (MessageBox.Show("A new version is available! Do you want to download it?",
"Update available", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) {
Process.Start(new ProcessStartInfo {
Expand Down

0 comments on commit be029a7

Please sign in to comment.