• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentRest / 9388947971

05 Jun 2024 05:56PM UTC coverage: 57.76% (+0.2%) from 57.587%
9388947971

push

github

pwelter34
fix flaky tests

277 of 614 branches covered (45.11%)

Branch coverage included in aggregate %.

847 of 1332 relevant lines covered (63.59%)

73.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.23
/src/FluentRest/HttpClientExtensions.cs
1
namespace FluentRest;
2

3
/// <summary>
4
/// Fluent extension methods for <see cref="HttpClient"/>.
5
/// </summary>
6
public static class HttpClientExtensions
7
{
8
    /// <summary>
9
    /// Sends a GET request using specified fluent <paramref name="builder" /> as an asynchronous operation.
10
    /// </summary>
11
    /// <param name="httpClient">The <see cref="HttpClient"/> used to send request.</param>
12
    /// <param name="builder">The fluent builder factory.</param>
13
    /// <returns>
14
    /// The task object representing the asynchronous operation.
15
    /// </returns>
16
    /// <exception cref="ArgumentNullException"><paramref name="httpClient" /> or <paramref name="builder" /> is <see langword="null" />.</exception>
17
    public static async Task<HttpResponseMessage> GetAsync(this HttpClient httpClient, Action<QueryBuilder> builder)
18
    {
19
        if (httpClient == null)
15!
20
            throw new ArgumentNullException(nameof(httpClient));
×
21

22
        if (builder == null)
15!
23
            throw new ArgumentNullException(nameof(builder));
×
24

25
        var requestMessage = new HttpRequestMessage(HttpMethod.Get, httpClient.BaseAddress);
15✔
26

27
        var fluentBuilder = new QueryBuilder(requestMessage);
15✔
28
        builder(fluentBuilder);
15✔
29

30
        var response = await FluentDispatcher.SendAsync(httpClient, requestMessage).ConfigureAwait(false);
15✔
31

32
        return response;
12✔
33
    }
12✔
34

35
    /// <summary>
36
    /// Sends a GET request using specified fluent <paramref name="builder"/> as an asynchronous operation.
37
    /// </summary>
38
    /// <typeparam name="TResponse">The type of the response.</typeparam>
39
    /// <param name="httpClient">The <see cref="HttpClient"/> used to send request.</param>
40
    /// <param name="builder">The fluent builder factory.</param>
41
    /// <returns>The task object representing the asynchronous operation.</returns>
42
    /// <exception cref="ArgumentNullException"><paramref name="httpClient" /> or <paramref name="builder" /> is <see langword="null" />.</exception>
43
    public static async Task<TResponse> GetAsync<TResponse>(this HttpClient httpClient, Action<QueryBuilder> builder)
44
    {
45
        if (httpClient == null)
15!
46
            throw new ArgumentNullException(nameof(httpClient));
×
47

48
        if (builder == null)
15!
49
            throw new ArgumentNullException(nameof(builder));
×
50

51
        var response = await httpClient.GetAsync(builder).ConfigureAwait(false);
15✔
52
        var data = await response.DeserializeAsync<TResponse>().ConfigureAwait(false);
12✔
53

54
        return data;
12✔
55
    }
12✔
56

57

58
    /// <summary>
59
    /// Sends a POST request using specified fluent builder as an asynchronous operation.
60
    /// </summary>
61
    /// <param name="httpClient">The <see cref="HttpClient"/> used to send request.</param>
62
    /// <param name="builder">The fluent builder factory.</param>
63
    /// <returns>The task object representing the asynchronous operation.</returns>
64
    /// <exception cref="ArgumentNullException"><paramref name="httpClient" /> or <paramref name="builder" /> is <see langword="null" />.</exception>
65
    public static async Task<HttpResponseMessage> PostAsync(this HttpClient httpClient, Action<FormBuilder> builder)
66
    {
67
        if (httpClient == null)
27!
68
            throw new ArgumentNullException(nameof(httpClient));
×
69

70
        if (builder == null)
27!
71
            throw new ArgumentNullException(nameof(builder));
×
72

73
        var requestMessage = new HttpRequestMessage(HttpMethod.Post, httpClient.BaseAddress);
27✔
74

75
        var fluentBuilder = new FormBuilder(requestMessage);
27✔
76
        builder(fluentBuilder);
27✔
77

78
        var response = await FluentDispatcher.SendAsync(httpClient, requestMessage).ConfigureAwait(false);
27✔
79

80
        return response;
27✔
81
    }
27✔
82

83
    /// <summary>
84
    /// Sends a POST request using specified fluent builder as an asynchronous operation.
85
    /// </summary>
86
    /// <typeparam name="TResponse">The type of the response.</typeparam>
87
    /// <param name="httpClient">The <see cref="HttpClient"/> used to send request.</param>
88
    /// <param name="builder">The fluent builder factory.</param>
89
    /// <returns>The task object representing the asynchronous operation.</returns>
90
    /// <exception cref="ArgumentNullException"><paramref name="httpClient" /> or <paramref name="builder" /> is <see langword="null" />.</exception>
91
    public static async Task<TResponse> PostAsync<TResponse>(this HttpClient httpClient, Action<FormBuilder> builder)
92
    {
93
        if (httpClient == null)
24!
94
            throw new ArgumentNullException(nameof(httpClient));
×
95

96
        if (builder == null)
24!
97
            throw new ArgumentNullException(nameof(builder));
×
98

99
        var response = await httpClient.PostAsync(builder).ConfigureAwait(false);
24✔
100
        var data = await response.DeserializeAsync<TResponse>().ConfigureAwait(false);
24✔
101

102
        return data;
21✔
103
    }
21✔
104

105

106
    /// <summary>
107
    /// Sends a PUT request using specified fluent builder as an asynchronous operation.
108
    /// </summary>
109
    /// <param name="httpClient">The <see cref="HttpClient"/> used to send request.</param>
110
    /// <param name="builder">The fluent builder factory.</param>
111
    /// <returns>The task object representing the asynchronous operation.</returns>
112
    /// <exception cref="ArgumentNullException"><paramref name="httpClient" /> or <paramref name="builder" /> is <see langword="null" />.</exception>
113
    public static async Task<HttpResponseMessage> PutAsync(this HttpClient httpClient, Action<FormBuilder> builder)
114
    {
115
        if (httpClient == null)
3!
116
            throw new ArgumentNullException(nameof(httpClient));
×
117

118
        if (builder == null)
3!
119
            throw new ArgumentNullException(nameof(builder));
×
120

121
        var requestMessage = new HttpRequestMessage(HttpMethod.Put, httpClient.BaseAddress);
3✔
122

123
        var fluentBuilder = new FormBuilder(requestMessage);
3✔
124
        builder(fluentBuilder);
3✔
125

126
        var response = await FluentDispatcher.SendAsync(httpClient, requestMessage).ConfigureAwait(false);
3✔
127

128
        return response;
3✔
129
    }
3✔
130

131
    /// <summary>
132
    /// Sends a PUT request using specified fluent builder as an asynchronous operation.
133
    /// </summary>
134
    /// <typeparam name="TResponse">The type of the response.</typeparam>
135
    /// <param name="httpClient">The <see cref="HttpClient"/> used to send request.</param>
136
    /// <param name="builder">The fluent builder factory.</param>
137
    /// <returns>The task object representing the asynchronous operation.</returns>
138
    /// <exception cref="ArgumentNullException"><paramref name="httpClient" /> or <paramref name="builder" /> is <see langword="null" />.</exception>
139
    public static async Task<TResponse> PutAsync<TResponse>(this HttpClient httpClient, Action<FormBuilder> builder)
140
    {
141
        if (httpClient == null)
3!
142
            throw new ArgumentNullException(nameof(httpClient));
×
143

144
        if (builder == null)
3!
145
            throw new ArgumentNullException(nameof(builder));
×
146

147
        var response = await httpClient.PutAsync(builder).ConfigureAwait(false);
3✔
148
        var data = await response.DeserializeAsync<TResponse>().ConfigureAwait(false);
3✔
149

150
        return data;
3✔
151
    }
3✔
152

153

154
    /// <summary>
155
    /// Sends a PATCH request using specified fluent builder as an asynchronous operation.
156
    /// </summary>
157
    /// <param name="httpClient">The <see cref="HttpClient"/> used to send request.</param>
158
    /// <param name="builder">The fluent builder factory.</param>
159
    /// <returns>The task object representing the asynchronous operation.</returns>
160
    /// <exception cref="ArgumentNullException"><paramref name="httpClient" /> or <paramref name="builder" /> is <see langword="null" />.</exception>
161
    public static async Task<HttpResponseMessage> PatchAsync(this HttpClient httpClient, Action<FormBuilder> builder)
162
    {
163
        if (httpClient == null)
6!
164
            throw new ArgumentNullException(nameof(httpClient));
×
165

166
        if (builder == null)
6!
167
            throw new ArgumentNullException(nameof(builder));
×
168

169
        var requestMessage = new HttpRequestMessage(FormBuilder.HttpPatch, httpClient.BaseAddress);
6✔
170

171
        var fluentBuilder = new FormBuilder(requestMessage);
6✔
172
        builder(fluentBuilder);
6✔
173

174
        var response = await FluentDispatcher.SendAsync(httpClient, requestMessage).ConfigureAwait(false);
6✔
175

176
        return response;
6✔
177
    }
6✔
178

179
    /// <summary>
180
    /// Sends a PATCH request using specified fluent builder as an asynchronous operation.
181
    /// </summary>
182
    /// <typeparam name="TResponse">The type of the response.</typeparam>
183
    /// <param name="httpClient">The <see cref="HttpClient"/> used to send request.</param>
184
    /// <param name="builder">The fluent builder factory.</param>
185
    /// <returns>The task object representing the asynchronous operation.</returns>
186
    /// <exception cref="ArgumentNullException"><paramref name="httpClient" /> or <paramref name="builder" /> is <see langword="null" />.</exception>
187
    public static async Task<TResponse> PatchAsync<TResponse>(this HttpClient httpClient, Action<FormBuilder> builder)
188
    {
189
        if (httpClient == null)
3!
190
            throw new ArgumentNullException(nameof(httpClient));
×
191

192
        if (builder == null)
3!
193
            throw new ArgumentNullException(nameof(builder));
×
194

195
        var response = await httpClient.PatchAsync(builder).ConfigureAwait(false);
3✔
196
        var data = await response.DeserializeAsync<TResponse>().ConfigureAwait(false);
3✔
197

198
        return data;
3✔
199
    }
3✔
200

201

202
    /// <summary>
203
    /// Sends a DELETE request using specified fluent builder as an asynchronous operation.
204
    /// </summary>
205
    /// <param name="httpClient">The <see cref="HttpClient"/> used to send request.</param>
206
    /// <param name="builder">The fluent builder factory.</param>
207
    /// <returns>The task object representing the asynchronous operation.</returns>
208
    /// <exception cref="ArgumentNullException"><paramref name="httpClient" /> or <paramref name="builder" /> is <see langword="null" />.</exception>
209
    public static async Task<HttpResponseMessage> DeleteAsync(this HttpClient httpClient, Action<FormBuilder> builder)
210
    {
211
        if (httpClient == null)
3!
212
            throw new ArgumentNullException(nameof(httpClient));
×
213

214
        if (builder == null)
3!
215
            throw new ArgumentNullException(nameof(builder));
×
216

217
        var requestMessage = new HttpRequestMessage(HttpMethod.Delete, httpClient.BaseAddress);
3✔
218

219
        var fluentBuilder = new FormBuilder(requestMessage);
3✔
220
        builder(fluentBuilder);
3✔
221

222
        var response = await FluentDispatcher.SendAsync(httpClient, requestMessage).ConfigureAwait(false);
3✔
223

224
        return response;
3✔
225
    }
3✔
226

227
    /// <summary>
228
    /// Sends a DELETE request using specified fluent builder as an asynchronous operation.
229
    /// </summary>
230
    /// <typeparam name="TResponse">The type of the response.</typeparam>
231
    /// <param name="httpClient">The <see cref="HttpClient"/> used to send request.</param>
232
    /// <param name="builder">The fluent builder factory.</param>
233
    /// <returns>The task object representing the asynchronous operation.</returns>
234
    /// <exception cref="ArgumentNullException"><paramref name="httpClient" /> or <paramref name="builder" /> is <see langword="null" />.</exception>
235
    public static async Task<TResponse> DeleteAsync<TResponse>(this HttpClient httpClient, Action<FormBuilder> builder)
236
    {
237
        if (httpClient == null)
3!
238
            throw new ArgumentNullException(nameof(httpClient));
×
239

240
        if (builder == null)
3!
241
            throw new ArgumentNullException(nameof(builder));
×
242

243
        var response = await httpClient.DeleteAsync(builder).ConfigureAwait(false);
3✔
244
        var data = await response.DeserializeAsync<TResponse>().ConfigureAwait(false);
3✔
245

246
        return data;
3✔
247
    }
3✔
248

249

250
    /// <summary>
251
    /// Sends a request using specified fluent builder as an asynchronous operation.
252
    /// </summary>
253
    /// <param name="httpClient">The <see cref="HttpClient"/> used to send request.</param>
254
    /// <param name="builder">The fluent builder factory.</param>
255
    /// <returns>The task object representing the asynchronous operation.</returns>
256
    /// <exception cref="ArgumentNullException"><paramref name="httpClient" /> or <paramref name="builder" /> is <see langword="null" />.</exception>
257
    public static async Task<HttpResponseMessage> SendAsync(this HttpClient httpClient, Action<SendBuilder> builder)
258
    {
259
        if (httpClient == null)
3!
260
            throw new ArgumentNullException(nameof(httpClient));
×
261

262
        if (builder == null)
3!
263
            throw new ArgumentNullException(nameof(builder));
×
264

265
        // build request
266
        var requestMessage = new HttpRequestMessage(HttpMethod.Post, httpClient.BaseAddress);
3✔
267

268
        var fluentBuilder = new SendBuilder(requestMessage);
3✔
269
        builder(fluentBuilder);
3✔
270

271
        var response = await FluentDispatcher.SendAsync(httpClient, requestMessage).ConfigureAwait(false);
3✔
272

273
        return response;
3✔
274
    }
3✔
275

276
    /// <summary>
277
    /// Sends a request using specified fluent builder as an asynchronous operation.
278
    /// </summary>
279
    /// <typeparam name="TResponse">The type of the response.</typeparam>
280
    /// <param name="httpClient">The <see cref="HttpClient"/> used to send request.</param>
281
    /// <param name="builder">The fluent builder factory.</param>
282
    /// <returns>The task object representing the asynchronous operation.</returns>
283
    /// <exception cref="ArgumentNullException"><paramref name="httpClient" /> or <paramref name="builder" /> is <see langword="null" />.</exception>
284
    public static async Task<TResponse> SendAsync<TResponse>(this HttpClient httpClient, Action<SendBuilder> builder)
285
    {
286
        if (httpClient == null)
3!
287
            throw new ArgumentNullException(nameof(httpClient));
×
288

289
        if (builder == null)
3!
290
            throw new ArgumentNullException(nameof(builder));
×
291

292
        var response = await httpClient.SendAsync(builder).ConfigureAwait(false);
3✔
293
        var data = await response.DeserializeAsync<TResponse>().ConfigureAwait(false);
3✔
294

295
        return data;
3✔
296
    }
3✔
297

298
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc