Skip to content

Commit c40d592

Browse files
committed
Added async methods
1 parent 268bafc commit c40d592

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

LessAnnoyingHttp/Http.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static class Http {
1515
/// <param name="endpoint">The URL to send the request to</param>
1616
/// <param name="headers">Optional headers</param>
1717
/// <returns><see cref="Response"/></returns>
18-
public static Response Get(string endpoint, Header[]? headers = null) {
18+
public static async Task<Response> GetAsync(string endpoint, Header[]? headers = null) {
1919
using HttpClient client = new ();
2020
client.Timeout = TimeSpan.FromSeconds(Timeout);
2121
HttpRequestMessage request = new () {
@@ -29,7 +29,7 @@ public static Response Get(string endpoint, Header[]? headers = null) {
2929

3030
HttpResponseMessage response;
3131
try {
32-
response = client.SendAsync(request).Result;
32+
response = await client.SendAsync(request);
3333
} catch (TaskCanceledException) {
3434
throw new TimeoutException($"Timeout waiting for response for request to {endpoint}");
3535
} catch (Exception e) {
@@ -38,8 +38,10 @@ public static Response Get(string endpoint, Header[]? headers = null) {
3838

3939
return new Response { IsSuccessful = response.IsSuccessStatusCode, Body = response.Content.ReadAsStringAsync().Result, Headers = new Dictionary<string, IEnumerable<string>>(response.Headers.ToDictionary(), StringComparer.OrdinalIgnoreCase), StatusCode = response.StatusCode };
4040
}
41+
42+
public static Response Get(string endpoint, Header[]? headers = null) => GetAsync(endpoint, headers).GetAwaiter().GetResult();
4143

42-
private static Response BodyRequest(string endpoint, HttpMethod method, string body, Header[]? headers, string contentType) {
44+
private static async Task<Response> BodyRequest(string endpoint, HttpMethod method, string body, Header[]? headers, string contentType) {
4345
using HttpClient client = new ();
4446
client.Timeout = TimeSpan.FromSeconds(Timeout);
4547
HttpRequestMessage request = new () {
@@ -54,7 +56,7 @@ private static Response BodyRequest(string endpoint, HttpMethod method, string b
5456

5557
HttpResponseMessage response;
5658
try {
57-
response = client.SendAsync(request).Result;
59+
response = await client.SendAsync(request);
5860
} catch (TaskCanceledException) {
5961
throw new TimeoutException($"Timeout waiting for response for request to {endpoint}");
6062
} catch (Exception e) {
@@ -72,7 +74,9 @@ private static Response BodyRequest(string endpoint, HttpMethod method, string b
7274
/// <param name="headers">Optional headers</param>
7375
/// <param name="contentType">The content type of the body - default = application/json</param>
7476
/// <returns><see cref="Response"/></returns>
75-
public static Response Delete(string endpoint, string body, Header[]? headers = null, string contentType = "application/json") => BodyRequest(endpoint, HttpMethod.Delete, body, headers, contentType);
77+
public static async Task<Response> DeleteAsync(string endpoint, string body, Header[]? headers = null, string contentType = "application/json") => await BodyRequest(endpoint, HttpMethod.Delete, body, headers, contentType);
78+
79+
public static Response Delete(string endpoint, string body, Header[]? headers = null, string contentType = "application/json") => DeleteAsync(endpoint, body, headers, contentType).GetAwaiter().GetResult();
7680

7781
/// <summary>
7882
/// Sends a PATCH request
@@ -82,7 +86,9 @@ private static Response BodyRequest(string endpoint, HttpMethod method, string b
8286
/// <param name="headers">Optional headers</param>
8387
/// <param name="contentType">The content type of the body - default = application/json</param>
8488
/// <returns><see cref="Response"/></returns>
85-
public static Response Patch(string endpoint, string body, Header[]? headers = null, string contentType = "application/json") => BodyRequest(endpoint, HttpMethod.Patch, body, headers, contentType);
89+
public static async Task<Response> PatchAsync(string endpoint, string body, Header[]? headers = null, string contentType = "application/json") => await BodyRequest(endpoint, HttpMethod.Patch, body, headers, contentType);
90+
91+
public static Response Patch(string endpoint, string body, Header[]? headers = null, string contentType = "application/json") => PatchAsync(endpoint, body, headers, contentType).GetAwaiter().GetResult();
8692

8793
/// <summary>
8894
/// Sends a POST request
@@ -92,7 +98,9 @@ private static Response BodyRequest(string endpoint, HttpMethod method, string b
9298
/// <param name="headers">Optional headers</param>
9399
/// <param name="contentType">The content type of the body - default = application/json</param>
94100
/// <returns><see cref="Response"/></returns>
95-
public static Response Post(string endpoint, string body, Header[]? headers = null, string contentType = "application/json") => BodyRequest(endpoint, HttpMethod.Post, body, headers, contentType);
101+
public static async Task<Response> PostAsync(string endpoint, string body, Header[]? headers = null, string contentType = "application/json") => await BodyRequest(endpoint, HttpMethod.Post, body, headers, contentType);
102+
103+
public static Response Post(string endpoint, string body, Header[]? headers = null, string contentType = "application/json") => PostAsync(endpoint, body, headers, contentType).GetAwaiter().GetResult();
96104

97105
/// <summary>
98106
/// Sends a PUT request
@@ -102,5 +110,7 @@ private static Response BodyRequest(string endpoint, HttpMethod method, string b
102110
/// <param name="headers">Optional headers</param>
103111
/// <param name="contentType">The content type of the body - default = application/json</param>
104112
/// <returns><see cref="Response"/></returns>
105-
public static Response Put(string endpoint, string body, Header[]? headers = null, string contentType = "application/json") => BodyRequest(endpoint, HttpMethod.Put, body, headers, contentType);
113+
public static async Task<Response> PutAsync(string endpoint, string body, Header[]? headers = null, string contentType = "application/json") => await BodyRequest(endpoint, HttpMethod.Put, body, headers, contentType);
114+
115+
public static Response Put(string endpoint, string body, Header[]? headers = null, string contentType = "application/json") => PutAsync(endpoint, body, headers, contentType).GetAwaiter().GetResult();
106116
}

LessAnnoyingHttp/LessAnnoyingHttp.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
5-
<Version>1.3.0</Version>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Version>1.4.0</Version>
66
<Authors>DemonExposer</Authors>
77
<Description>Simpler HTTP requests in .NET</Description>
88
<PackageTags>HTTP</PackageTags>
@@ -14,7 +14,7 @@
1414
<ImplicitUsings>enable</ImplicitUsings>
1515
<Nullable>enable</Nullable>
1616
<Title>LessAnnoyingHttp</Title>
17-
<Copyright>Copyright (c) 2025 DemonExposer</Copyright>
17+
<Copyright>Copyright (c) 2026 DemonExposer</Copyright>
1818
</PropertyGroup>
1919

2020
<ItemGroup>

0 commit comments

Comments
 (0)