-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpClientExtensions.cs
118 lines (100 loc) · 4.27 KB
/
HttpClientExtensions.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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace DotNetLiberty.Http
{
public static class HttpClientExtensions
{
public static async Task<IEnumerable<TEntity>> GetManyAsync<TEntity>(this HttpClient client)
{
return await client.GetManyAsync<TEntity>((Uri)null);
}
public static async Task<IEnumerable<TEntity>> GetManyAsync<TEntity>(this HttpClient client, string requestUri)
{
return await client.GetManyAsync<TEntity>(new Uri(requestUri));
}
public static async Task<IEnumerable<TEntity>> GetManyAsync<TEntity>(this HttpClient client, Uri requestUri)
{
var result = await client.GetAsync(requestUri);
result.ThrowIfUnsuccessful();
return await result.ReadEntities<TEntity>();
}
public static async Task<TEntity> GetSingleAsync<TEntity>(this HttpClient client, object id)
{
return await client.GetSingleAsync<TEntity>(id.ToString());
}
public static async Task<TEntity> GetSingleAsync<TEntity>(this HttpClient client, string requestUri)
{
var result = await client.GetAsync(requestUri);
result.ThrowIfUnsuccessful();
return await result.ReadEntity<TEntity>();
}
public static async Task<TEntity> GetSingleAsync<TEntity>(this HttpClient client, Uri requestUri)
{
var result = await client.GetAsync(requestUri);
result.ThrowIfUnsuccessful();
return await result.ReadEntity<TEntity>();
}
public static async Task<TEntity> PostAsync<TEntity>(this HttpClient client, TEntity entity)
{
return await client.PostAsync((Uri)null, entity);
}
public static async Task<TEntity> PostAsync<TEntity>(this HttpClient client, string requestUri, TEntity entity)
{
return await client.PostAsync(new Uri(requestUri), entity);
}
public static async Task<TEntity> PostAsync<TEntity>(this HttpClient client, Uri requestUri, TEntity entity)
{
var result = await client.PostAsync(requestUri, JsonContent(entity));
result.ThrowIfUnsuccessful();
return await result.ReadEntity<TEntity>();
}
public static async Task<TEntity> PutAsync<TEntity>(this HttpClient client, object id, TEntity entity)
{
return await client.PutAsync(id.ToString(), entity);
}
public static async Task<TEntity> PutAsync<TEntity>(this HttpClient client, string requestUri, TEntity entity)
{
var result = await client.PutAsync(requestUri, JsonContent(entity));
result.ThrowIfUnsuccessful();
return await result.ReadEntity<TEntity>();
}
public static async Task<TEntity> PutAsync<TEntity>(this HttpClient client, Uri requestUri, TEntity entity)
{
var result = await client.PutAsync(requestUri, JsonContent(entity));
result.ThrowIfUnsuccessful();
return await result.ReadEntity<TEntity>();
}
public static async Task DeleteAsync(this HttpClient client, object id)
{
await client.DeleteAsync(id.ToString());
}
public static async Task DeleteAsync(this HttpClient client, string requestUri)
{
var result = await client.DeleteAsync(requestUri);
result.ThrowIfUnsuccessful();
}
public static async Task DeleteAsync(this HttpClient client, Uri requestUri)
{
var result = await client.DeleteAsync(requestUri);
result.ThrowIfUnsuccessful();
}
public static HttpClient WithBaseUri(this HttpClient client, Uri baseUri)
{
client.BaseAddress = baseUri;
return client;
}
public static HttpClient AcceptJson(this HttpClient client)
{
client.DefaultRequestHeaders.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
private static JsonContent JsonContent<TEntity>(TEntity entity)
{
return new JsonContent<TEntity>(entity);
}
}
}