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

loresoft / FluentRest / 11929962690

20 Nov 2024 08:57AM UTC coverage: 58.107%. Remained the same
11929962690

Pull #206

github

web-flow
Merge 7d96b20ca into dd4bfca3f
Pull Request #206: Bump Microsoft.NET.Test.Sdk from 17.11.1 to 17.12.0

264 of 594 branches covered (44.44%)

Branch coverage included in aggregate %.

829 of 1287 relevant lines covered (64.41%)

48.0 hits per line

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

36.6
/src/FluentRest/QueryBuilder.cs
1
namespace FluentRest;
2

3
/// <summary>
4
/// A fluent query builder.
5
/// </summary>
6
public class QueryBuilder : QueryBuilder<QueryBuilder>
7
{
8
    /// <summary>
9
    /// Initializes a new instance of the <see cref="QueryBuilder" /> class.
10
    /// </summary>
11
    /// <param name="requestMessage">The fluent HTTP request being built.</param>
12
    public QueryBuilder(HttpRequestMessage requestMessage) : base(requestMessage)
54✔
13
    {
14
    }
54✔
15
}
16

17
/// <summary>
18
/// A fluent query builder.
19
/// </summary>
20
/// <typeparam name="TBuilder">The type of the builder.</typeparam>
21
public abstract class QueryBuilder<TBuilder> : RequestBuilder<TBuilder>
22
    where TBuilder : QueryBuilder<TBuilder>
23
{
24
    /// <summary>
25
    /// Initializes a new instance of the <see cref="QueryBuilder{TBuilder}"/> class.
26
    /// </summary>
27
    /// <param name="requestMessage">The fluent HTTP request being built.</param>
28
    protected QueryBuilder(HttpRequestMessage requestMessage) : base(requestMessage)
128✔
29
    {
30
    }
128✔
31

32
    /// <summary>
33
    /// Start a fluent header builder.
34
    /// </summary>
35
    /// <param name="builder">The builder factory.</param>
36
    /// <returns>A fluent request builder.</returns>
37
    /// <exception cref="ArgumentNullException"><paramref name="builder" /> is <see langword="null" />.</exception>
38
    public TBuilder Header(Action<HeaderBuilder> builder)
39
    {
40
        if (builder == null)
16!
41
            throw new ArgumentNullException(nameof(builder));
×
42

43
        var headerBuilder = new HeaderBuilder(RequestMessage);
16✔
44
        builder(headerBuilder);
16✔
45

46
        return this as TBuilder;
16✔
47
    }
48

49
    /// <summary>
50
    /// Sets HTTP header with the specified <paramref name="name"/> and <paramref name="value"/>.
51
    /// </summary>
52
    /// <param name="name">The header name.</param>
53
    /// <param name="value">The header value.</param>
54
    /// <returns>A fluent request builder.</returns>
55
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
56
    public TBuilder Header(string name, string value)
57
    {
58
        if (name == null)
10!
59
            throw new ArgumentNullException(nameof(name));
×
60

61
        if (value == null)
10✔
62
            RequestMessage.Headers.Remove(name);
2✔
63
        else
64
            RequestMessage.Headers.Add(name, value);
8✔
65

66
        return this as TBuilder;
10✔
67
    }
68

69
    /// <summary>
70
    /// Sets HTTP header with the specified <paramref name="name"/> and <paramref name="value"/> if the specified <paramref name="condition"/> is true.
71
    /// </summary>
72
    /// <param name="condition">If condition is true, header will be added; otherwise ignore header.</param>
73
    /// <param name="name">The header name.</param>
74
    /// <param name="value">The header value.</param>
75
    /// <returns>A fluent request builder.</returns>
76
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
77
    public TBuilder HeaderIf(Func<bool> condition, string name, string value)
78
    {
79
        if (condition == null || !condition())
×
80
            return this as TBuilder;
×
81

82
        return Header(name, value);
×
83
    }
84

85
    /// <summary>
86
    /// Sets HTTP header with the specified <paramref name="name"/> and <paramref name="value"/> if the specified <paramref name="condition"/> is true.
87
    /// </summary>
88
    /// <param name="condition">If condition is true, header will be added; otherwise ignore header.</param>
89
    /// <param name="name">The header name.</param>
90
    /// <param name="value">The header value.</param>
91
    /// <returns>A fluent request builder.</returns>
92
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
93
    public TBuilder HeaderIf(bool condition, string name, string value)
94
    {
95
        if (!condition)
×
96
            return this as TBuilder;
×
97

98
        return Header(name, value);
×
99
    }
100

101

102
    /// <summary>
103
    /// Sets the base URI address used when sending requests.
104
    /// </summary>
105
    /// <param name="path">The path.</param>
106
    /// <returns>A fluent request builder.</returns>
107
    /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null" />.</exception>
108
    public TBuilder BaseUri(Uri path)
109
    {
110
        if (path == null)
20!
111
            throw new ArgumentNullException(nameof(path));
×
112

113
        var urlBuilder = new UrlBuilder(path);
20✔
114
        RequestMessage.SetUrlBuilder(urlBuilder);
20✔
115

116
        RequestMessage.Synchronize();
20✔
117

118

119
        return this as TBuilder;
20✔
120
    }
121

122
    /// <summary>
123
    /// Sets the base URI address used when sending requests.
124
    /// </summary>
125
    /// <param name="path">The path.</param>
126
    /// <returns>A fluent request builder.</returns>
127
    /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null" />.</exception>
128
    public TBuilder BaseUri(string path)
129
    {
130
        if (path == null)
18!
131
            throw new ArgumentNullException(nameof(path));
×
132

133
        var uri = new Uri(path, UriKind.Absolute);
18✔
134
        return BaseUri(uri);
18✔
135
    }
136

137

138
    /// <summary>
139
    /// Sets the base URI from the specified <paramref name="path"/>.
140
    /// </summary>
141
    /// <param name="path">The full Uri path.</param>
142
    /// <returns>A fluent request builder.</returns>
143
    /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null" />.</exception>
144
    public TBuilder FullUri(Uri path)
145
    {
146
        if (path == null)
×
147
            throw new ArgumentNullException(nameof(path));
×
148

149
        return BaseUri(path);
×
150
    }
151

152
    /// <summary>
153
    /// Sets the base URI from the specified <paramref name="path"/>.
154
    /// </summary>
155
    /// <param name="path">The full Uri path.</param>
156
    /// <returns>A fluent request builder.</returns>
157
    /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null" />.</exception>
158
    public TBuilder FullUri(string path)
159
    {
160
        if (path == null)
2!
161
            throw new ArgumentNullException(nameof(path));
×
162

163
        var u = new Uri(path, UriKind.Absolute);
2✔
164
        return BaseUri(u);
2✔
165
    }
166

167

168
    /// <summary>
169
    /// Appends the specified <paramref name="path"/> to the BaseUri of the request.
170
    /// </summary>
171
    /// <param name="path">The path to append.</param>
172
    /// <returns>A fluent request builder.</returns>
173
    public TBuilder AppendPath(Uri path)
174
    {
175
        if (path == null)
×
176
            return this as TBuilder;
×
177

178
        var urlBuilder = RequestMessage.GetUrlBuilder();
×
179
        urlBuilder.AppendPath(path);
×
180

181
        RequestMessage.Synchronize();
×
182

183
        return this as TBuilder;
×
184
    }
185

186
    /// <summary>
187
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
188
    /// </summary>
189
    /// <param name="path">The path to append.</param>
190
    /// <returns>
191
    /// A fluent request builder.
192
    /// </returns>
193
    public TBuilder AppendPath(string path)
194
    {
195
        if (path == null)
152!
196
            return this as TBuilder;
×
197

198
        var urlBuilder = RequestMessage.GetUrlBuilder();
152✔
199
        urlBuilder.AppendPath(path);
152✔
200

201
        RequestMessage.Synchronize();
152✔
202

203
        return this as TBuilder;
152✔
204
    }
205

206
    /// <summary>
207
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
208
    /// </summary>
209
    /// <typeparam name="TValue">The type of the value.</typeparam>
210
    /// <param name="path">The path to append.</param>
211
    /// <returns>A fluent request builder.</returns>
212
    public TBuilder AppendPath<TValue>(TValue path)
213
    {
214
        if (path == null)
×
215
            return this as TBuilder;
×
216

217
        var urlBuilder = RequestMessage.GetUrlBuilder();
×
218
        urlBuilder.AppendPath(path);
×
219

220
        RequestMessage.Synchronize();
×
221

222
        return this as TBuilder;
×
223
    }
224

225
    /// <summary>
226
    /// Appends the specified <paramref name="paths"/> to the BaseUri of the request.
227
    /// </summary>
228
    /// <param name="paths">The paths to append.</param>
229
    /// <returns>A fluent request builder.</returns>
230
    public TBuilder AppendPaths(IEnumerable<string> paths)
231
    {
232
        if (paths == null)
2!
233
            return this as TBuilder;
×
234

235
        var urlBuilder = RequestMessage.GetUrlBuilder();
2✔
236
        urlBuilder.AppendPaths(paths);
2✔
237

238
        RequestMessage.Synchronize();
2✔
239

240
        return this as TBuilder;
2✔
241
    }
242

243
    /// <summary>
244
    /// Appends the specified <paramref name="paths"/> to the BaseUri of the request.
245
    /// </summary>
246
    /// <param name="paths">The paths to append.</param>
247
    /// <returns>A fluent request builder.</returns>
248
    public TBuilder AppendPaths(params string[] paths)
249
    {
250
        if (paths == null)
2!
251
            return this as TBuilder;
×
252

253
        var urlBuilder = RequestMessage.GetUrlBuilder();
2✔
254
        urlBuilder.AppendPaths(paths);
2✔
255

256
        RequestMessage.Synchronize();
2✔
257

258
        return this as TBuilder;
2✔
259
    }
260

261
    /// <summary>
262
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
263
    /// </summary>
264
    /// <param name="condition">If condition is true, append path will be added; otherwise ignore path.</param>
265
    /// <param name="path">The path to append.</param>
266
    /// <returns>
267
    /// A fluent request builder.
268
    /// </returns>
269
    public TBuilder AppendPathIf(Func<bool> condition, string path)
270
    {
271
        if (path == null)
×
272
            return this as TBuilder;
×
273

274
        if (condition == null || !condition())
×
275
            return this as TBuilder;
×
276

277
        return AppendPath(path);
×
278
    }
279

280
    /// <summary>
281
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
282
    /// </summary>
283
    /// <param name="condition">If condition is true, append path will be added; otherwise ignore path.</param>
284
    /// <param name="path">The path to append.</param>
285
    /// <returns>
286
    /// A fluent request builder.
287
    /// </returns>
288
    public TBuilder AppendPathIf(bool condition, string path)
289
    {
290
        if (path == null)
×
291
            return this as TBuilder;
×
292

293
        if (!condition)
×
294
            return this as TBuilder;
×
295

296
        return AppendPath(path);
×
297
    }
298

299
    /// <summary>
300
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
301
    /// </summary>
302
    /// <typeparam name="TValue">The type of the value.</typeparam>
303
    /// <param name="condition">If condition is true, append path will be added; otherwise ignore path.</param>
304
    /// <param name="path">The path to append.</param>
305
    /// <returns>A fluent request builder.</returns>
306
    public TBuilder AppendPathIf<TValue>(Func<bool> condition, TValue path)
307
    {
308
        if (path == null)
×
309
            return this as TBuilder;
×
310

311
        if (condition == null || !condition())
×
312
            return this as TBuilder;
×
313

314
        return AppendPath(path);
×
315
    }
316

317
    /// <summary>
318
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
319
    /// </summary>
320
    /// <typeparam name="TValue">The type of the value.</typeparam>
321
    /// <param name="condition">If condition is true, append path will be added; otherwise ignore path.</param>
322
    /// <param name="path">The path to append.</param>
323
    /// <returns>A fluent request builder.</returns>
324
    public TBuilder AppendPathIf<TValue>(bool condition, TValue path)
325
    {
326
        if (path == null)
×
327
            return this as TBuilder;
×
328

329
        if (!condition)
×
330
            return this as TBuilder;
×
331

332
        return AppendPath(path);
×
333
    }
334

335

336
    /// <summary>
337
    /// Appends the specified <paramref name="name"/> and <paramref name="value"/> to the request Uri.
338
    /// </summary>
339
    /// <param name="name">The query parameter name.</param>
340
    /// <param name="value">The query parameter value.</param>
341
    /// <returns>A fluent request builder.</returns>
342
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
343
    public TBuilder QueryString(string name, string value)
344
    {
345
        if (name == null)
82!
346
            throw new ArgumentNullException(nameof(name));
×
347

348

349
        var urlBuilder = RequestMessage.GetUrlBuilder();
82✔
350
        urlBuilder.AppendQuery(name, value);
82✔
351

352
        RequestMessage.Synchronize();
82✔
353

354
        return this as TBuilder;
82✔
355

356
    }
357

358
    /// <summary>
359
    /// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the request Uri.
360
    /// </summary>
361
    /// <typeparam name="TValue">The type of the value.</typeparam>
362
    /// <param name="name">The query parameter name.</param>
363
    /// <param name="value">The query parameter value.</param>
364
    /// <returns>
365
    /// A fluent request builder.
366
    /// </returns>
367
    /// <exception cref="System.ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
368
    public TBuilder QueryString<TValue>(string name, TValue value)
369
    {
370
        if (name == null)
72!
371
            throw new ArgumentNullException(nameof(name));
×
372

373
        var v = value != null ? value.ToString() : string.Empty;
72!
374
        return QueryString(name, v);
72✔
375
    }
376

377
    /// <summary>
378
    /// Appends the specified <paramref name="name" /> and <paramref name="values" /> to the request Uri.
379
    /// </summary>
380
    /// <typeparam name="TValue">The type of the value.</typeparam>
381
    /// <param name="name">The query parameter name.</param>
382
    /// <param name="values">The query parameter values.</param>
383
    /// <returns>
384
    /// A fluent request builder.
385
    /// </returns>
386
    /// <exception cref="System.ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
387
    public TBuilder QueryStrings<TValue>(string name, IEnumerable<TValue> values)
388
    {
389
        if (name == null)
2!
390
            throw new ArgumentNullException(nameof(name));
×
391

392
        if (values == null)
2!
393
            return this as TBuilder;
×
394

395
        foreach (var value in values)
12✔
396
        {
397
            var v = value != null ? value.ToString() : string.Empty;
4!
398
            QueryString(name, v);
4✔
399
        }
400

401
        return this as TBuilder;
2✔
402
    }
403

404

405
    /// <summary>
406
    /// Appends the specified <paramref name="name"/> and <paramref name="value"/> to the request Uri if the specified <paramref name="condition"/> is true.
407
    /// </summary>
408
    /// <param name="condition">If condition is true, query string will be added; otherwise ignore query string.</param>
409
    /// <param name="name">The query parameter name.</param>
410
    /// <param name="value">The query parameter value.</param>
411
    /// <returns>A fluent request builder.</returns>
412
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
413
    public TBuilder QueryStringIf(Func<bool> condition, string name, string value)
414
    {
415
        if (condition == null || !condition())
×
416
            return this as TBuilder;
×
417

418
        return QueryString(name, value);
×
419
    }
420

421
    /// <summary>
422
    /// Appends the specified <paramref name="name"/> and <paramref name="value"/> to the request Uri if the specified <paramref name="condition"/> is true.
423
    /// </summary>
424
    /// <param name="condition">If condition is true, query string will be added; otherwise ignore query string.</param>
425
    /// <param name="name">The query parameter name.</param>
426
    /// <param name="value">The query parameter value.</param>
427
    /// <returns>A fluent request builder.</returns>
428
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
429
    public TBuilder QueryStringIf(bool condition, string name, string value)
430
    {
431
        if (!condition)
×
432
            return this as TBuilder;
×
433

434
        return QueryString(name, value);
×
435
    }
436

437
    /// <summary>
438
    /// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the request Uri if the specified <paramref name="condition" /> is true.
439
    /// </summary>
440
    /// <typeparam name="TValue">The type of the value.</typeparam>
441
    /// <param name="condition">If condition is true, query string will be added; otherwise ignore query string.</param>
442
    /// <param name="name">The query parameter name.</param>
443
    /// <param name="value">The query parameter value.</param>
444
    /// <returns>
445
    /// A fluent request builder.
446
    /// </returns>
447
    /// <exception cref="System.ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
448
    public TBuilder QueryStringIf<TValue>(Func<bool> condition, string name, TValue value)
449
    {
450
        if (condition == null || !condition())
×
451
            return this as TBuilder;
×
452

453
        return QueryString(name, value);
×
454
    }
455

456
    /// <summary>
457
    /// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the request Uri if the specified <paramref name="condition" /> is true.
458
    /// </summary>
459
    /// <typeparam name="TValue">The type of the value.</typeparam>
460
    /// <param name="condition">If condition is true, query string will be added; otherwise ignore query string.</param>
461
    /// <param name="name">The query parameter name.</param>
462
    /// <param name="value">The query parameter value.</param>
463
    /// <returns>
464
    /// A fluent request builder.
465
    /// </returns>
466
    /// <exception cref="System.ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
467
    public TBuilder QueryStringIf<TValue>(bool condition, string name, TValue value)
468
    {
469
        if (!condition)
×
470
            return this as TBuilder;
×
471

472
        return QueryString(name, value);
×
473
    }
474
}
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

© 2025 Coveralls, Inc