-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
HttpClientJsonExtensions.cs
121 lines (110 loc) · 6.86 KB
/
HttpClientJsonExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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 Microsoft.JSInterop;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Components
{
/// <summary>
/// Extension methods for working with JSON APIs.
/// </summary>
public static class HttpClientJsonExtensions
{
/// <summary>
/// Sends a GET request to the specified URI, and parses the JSON response body
/// to create an object of the generic type.
/// </summary>
/// <typeparam name="T">A type into which the response body can be JSON-deserialized.</typeparam>
/// <param name="httpClient">The <see cref="HttpClient"/>.</param>
/// <param name="requestUri">The URI that the request will be sent to.</param>
/// <returns>The response parsed as an object of the generic type.</returns>
public static async Task<T> GetJsonAsync<T>(this HttpClient httpClient, string requestUri)
{
var responseJson = await httpClient.GetStringAsync(requestUri);
return Json.Deserialize<T>(responseJson);
}
/// <summary>
/// Sends a POST request to the specified URI, including the specified <paramref name="content"/>
/// in JSON-encoded format, and parses the JSON response body to create an object of the generic type.
/// </summary>
/// <param name="httpClient">The <see cref="HttpClient"/>.</param>
/// <param name="requestUri">The URI that the request will be sent to.</param>
/// <param name="content">Content for the request body. This will be JSON-encoded and sent as a string.</param>
/// <returns>The response parsed as an object of the generic type.</returns>
public static Task PostJsonAsync(this HttpClient httpClient, string requestUri, object content)
=> httpClient.SendJsonAsync(HttpMethod.Post, requestUri, content);
/// <summary>
/// Sends a POST request to the specified URI, including the specified <paramref name="content"/>
/// in JSON-encoded format, and parses the JSON response body to create an object of the generic type.
/// </summary>
/// <typeparam name="T">A type into which the response body can be JSON-deserialized.</typeparam>
/// <param name="httpClient">The <see cref="HttpClient"/>.</param>
/// <param name="requestUri">The URI that the request will be sent to.</param>
/// <param name="content">Content for the request body. This will be JSON-encoded and sent as a string.</param>
/// <returns>The response parsed as an object of the generic type.</returns>
public static Task<T> PostJsonAsync<T>(this HttpClient httpClient, string requestUri, object content)
=> httpClient.SendJsonAsync<T>(HttpMethod.Post, requestUri, content);
/// <summary>
/// Sends a PUT request to the specified URI, including the specified <paramref name="content"/>
/// in JSON-encoded format.
/// </summary>
/// <param name="httpClient">The <see cref="HttpClient"/>.</param>
/// <param name="requestUri">The URI that the request will be sent to.</param>
/// <param name="content">Content for the request body. This will be JSON-encoded and sent as a string.</param>
public static Task PutJsonAsync(this HttpClient httpClient, string requestUri, object content)
=> httpClient.SendJsonAsync(HttpMethod.Put, requestUri, content);
/// <summary>
/// Sends a PUT request to the specified URI, including the specified <paramref name="content"/>
/// in JSON-encoded format, and parses the JSON response body to create an object of the generic type.
/// </summary>
/// <typeparam name="T">A type into which the response body can be JSON-deserialized.</typeparam>
/// <param name="httpClient">The <see cref="HttpClient"/>.</param>
/// <param name="requestUri">The URI that the request will be sent to.</param>
/// <param name="content">Content for the request body. This will be JSON-encoded and sent as a string.</param>
/// <returns>The response parsed as an object of the generic type.</returns>
public static Task<T> PutJsonAsync<T>(this HttpClient httpClient, string requestUri, object content)
=> httpClient.SendJsonAsync<T>(HttpMethod.Put, requestUri, content);
/// <summary>
/// Sends an HTTP request to the specified URI, including the specified <paramref name="content"/>
/// in JSON-encoded format.
/// </summary>
/// <param name="httpClient">The <see cref="HttpClient"/>.</param>
/// <param name="method">The HTTP method.</param>
/// <param name="requestUri">The URI that the request will be sent to.</param>
/// <param name="content">Content for the request body. This will be JSON-encoded and sent as a string.</param>
public static Task SendJsonAsync(this HttpClient httpClient, HttpMethod method, string requestUri, object content)
=> httpClient.SendJsonAsync<IgnoreResponse>(method, requestUri, content);
/// <summary>
/// Sends an HTTP request to the specified URI, including the specified <paramref name="content"/>
/// in JSON-encoded format, and parses the JSON response body to create an object of the generic type.
/// </summary>
/// <typeparam name="T">A type into which the response body can be JSON-deserialized.</typeparam>
/// <param name="httpClient">The <see cref="HttpClient"/>.</param>
/// <param name="method">The HTTP method.</param>
/// <param name="requestUri">The URI that the request will be sent to.</param>
/// <param name="content">Content for the request body. This will be JSON-encoded and sent as a string.</param>
/// <returns>The response parsed as an object of the generic type.</returns>
public static async Task<T> SendJsonAsync<T>(this HttpClient httpClient, HttpMethod method, string requestUri, object content)
{
var requestJson = Json.Serialize(content);
var response = await httpClient.SendAsync(new HttpRequestMessage(method, requestUri)
{
Content = new StringContent(requestJson, Encoding.UTF8, "application/json")
});
// Make sure the call was successful before we
// attempt to process the response content
response.EnsureSuccessStatusCode();
if (typeof(T) == typeof(IgnoreResponse))
{
return default;
}
else
{
var responseJson = await response.Content.ReadAsStringAsync();
return Json.Deserialize<T>(responseJson);
}
}
class IgnoreResponse { }
}
}