From a74124efbf734681457812a0d5360ef5cbcf53b4 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Sun, 4 Mar 2018 21:47:15 +0000 Subject: [PATCH] Add extension methods for HTTP JSON requests/responses --- .../Json/HttpClientJsonExtensions.cs | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/Microsoft.AspNetCore.Blazor/Json/HttpClientJsonExtensions.cs diff --git a/src/Microsoft.AspNetCore.Blazor/Json/HttpClientJsonExtensions.cs b/src/Microsoft.AspNetCore.Blazor/Json/HttpClientJsonExtensions.cs new file mode 100644 index 0000000000..5b013ed8f5 --- /dev/null +++ b/src/Microsoft.AspNetCore.Blazor/Json/HttpClientJsonExtensions.cs @@ -0,0 +1,117 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace Microsoft.AspNetCore.Blazor +{ + /// + /// Extension methods for working with JSON APIs. + /// + public static class HttpClientJsonExtensions + { + /// + /// Sends a GET request to the specified URI, and parses the JSON response body + /// to create an object of the generic type. + /// + /// A type into which the response body can be JSON-deserialized. + /// The . + /// The URI that the request will be sent to. + /// The response parsed as an object of the generic type. + public static async Task GetJsonAsync(this HttpClient httpClient, string requestUri) + { + var responseJson = await httpClient.GetStringAsync(requestUri); + return JsonUtil.Deserialize(responseJson); + } + + /// + /// Sends a POST request to the specified URI, including the specified + /// in JSON-encoded format, and parses the JSON response body to create an object of the generic type. + /// + /// A type into which the response body can be JSON-deserialized. + /// The . + /// The URI that the request will be sent to. + /// Content for the request body. This will be JSON-encoded and sent as a string. + /// The response parsed as an object of the generic type. + public static Task PostJsonAsync(this HttpClient httpClient, string requestUri, object content) + => httpClient.SendJsonAsync(HttpMethod.Post, requestUri, content); + + /// + /// Sends a POST request to the specified URI, including the specified + /// in JSON-encoded format, and parses the JSON response body to create an object of the generic type. + /// + /// A type into which the response body can be JSON-deserialized. + /// The . + /// The URI that the request will be sent to. + /// Content for the request body. This will be JSON-encoded and sent as a string. + /// The response parsed as an object of the generic type. + public static Task PostJsonAsync(this HttpClient httpClient, string requestUri, object content) + => httpClient.SendJsonAsync(HttpMethod.Post, requestUri, content); + + /// + /// Sends a PUT request to the specified URI, including the specified + /// in JSON-encoded format. + /// + /// The . + /// The URI that the request will be sent to. + /// Content for the request body. This will be JSON-encoded and sent as a string. + public static Task PutJsonAsync(this HttpClient httpClient, string requestUri, object content) + => httpClient.SendJsonAsync(HttpMethod.Put, requestUri, content); + + /// + /// Sends a PUT request to the specified URI, including the specified + /// in JSON-encoded format, and parses the JSON response body to create an object of the generic type. + /// + /// A type into which the response body can be JSON-deserialized. + /// The . + /// The URI that the request will be sent to. + /// Content for the request body. This will be JSON-encoded and sent as a string. + /// The response parsed as an object of the generic type. + public static Task PutJsonAsync(this HttpClient httpClient, string requestUri, object content) + => httpClient.SendJsonAsync(HttpMethod.Put, requestUri, content); + + /// + /// Sends an HTTP request to the specified URI, including the specified + /// in JSON-encoded format. + /// + /// The . + /// The HTTP method. + /// The URI that the request will be sent to. + /// Content for the request body. This will be JSON-encoded and sent as a string. + public static Task SendJsonAsync(this HttpClient httpClient, HttpMethod method, string requestUri, object content) + => httpClient.SendJsonAsync(method, requestUri, content); + + /// + /// Sends an HTTP request to the specified URI, including the specified + /// in JSON-encoded format, and parses the JSON response body to create an object of the generic type. + /// + /// A type into which the response body can be JSON-deserialized. + /// The . + /// The HTTP method. + /// The URI that the request will be sent to. + /// Content for the request body. This will be JSON-encoded and sent as a string. + /// The response parsed as an object of the generic type. + public static async Task SendJsonAsync(this HttpClient httpClient, HttpMethod method, string requestUri, object content) + { + var requestJson = JsonUtil.Serialize(content); + var response = await httpClient.SendAsync(new HttpRequestMessage(method, requestUri) + { + Content = new StringContent(requestJson, Encoding.UTF8, "application/json") + }); + + if (typeof(T) == typeof(IgnoreResponse)) + { + return default; + } + else + { + var responseJson = await response.Content.ReadAsStringAsync(); + return JsonUtil.Deserialize(responseJson); + } + } + + class IgnoreResponse { } + } +}