Skip to content

Instantly share code, notes, and snippets.

@Lupusa87
Created July 20, 2019 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lupusa87/08942c1e18c968303e626a7de8eacca9 to your computer and use it in GitHub Desktop.
Save Lupusa87/08942c1e18c968303e626a7de8eacca9 to your computer and use it in GitHub Desktop.
Extension for blazor httpclient to do some custom stuff.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace BlazorTodos
{
public static class HttpClientEx
{
//public static readonly JsonSerializerOptions Options = new JsonSerializerOptions
//{
// PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
//};
static JsonReaderOptions options = new JsonReaderOptions
{
AllowTrailingCommas = true
};
static JsonSerializerOptions opt = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true
};
public static async Task<T> MyGetJsonAsync<T>(this HttpClient client, string requestUri)
{
if (client is HttpClient)
{
string a = await client.GetStringAsync(requestUri);
JsonDocument document = JsonDocument.Parse(a, options);
return JsonSerializer.Parse<T>(document.RootElement.GetProperty("value").GetRawText(), opt);
}
else
{
throw new InvalidOperationException();
}
}
public static async Task<T> MyPostFormGetJsonAsync<T>(this HttpClient client, string requestUri, HttpContent content)
{
if (client is HttpClient)
{
var response = await client.PostAsync(requestUri, content);
JsonDocument document = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
return JsonSerializer.Parse<T>(document.RootElement.GetProperty("value").GetRawText(), opt);
}
else
{
throw new InvalidOperationException();
}
}
public static async Task<T> MyPostJsonGetJsonAsync<T>(this HttpClient client, string requestUri, T content)
{
if (client is HttpClient)
{
return await SendJsonAsync<T>(client, HttpMethod.Post, requestUri, content);
}
else
{
throw new InvalidOperationException();
}
}
public static async Task<string> MyPostJsonGetStringAsync(this HttpClient client, string requestUri, object content)
{
if (client is HttpClient)
{
var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Post, requestUri)
{
Content = new StringContent(JsonSerializer.ToString(content, opt), Encoding.UTF8, "application/json")
});
var stringContent = await response.Content.ReadAsStringAsync();
JsonDocument document = JsonDocument.Parse(stringContent);
return document.RootElement.GetProperty("value").GetRawText();
}
else
{
throw new InvalidOperationException();
}
}
private static async Task<T> SendJsonAsync<T>( HttpClient httpClient, HttpMethod method, string requestUri, object content)
{
var response = await httpClient.SendAsync(new HttpRequestMessage(method, requestUri)
{
Content = new StringContent(JsonSerializer.ToString(content, opt), Encoding.UTF8, "application/json")
});
var stringContent = await response.Content.ReadAsStringAsync();
JsonDocument document = JsonDocument.Parse(stringContent);
T a = JsonSerializer.Parse<T>(document.RootElement.GetProperty("value").GetRawText(), opt);
return a;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment