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

loresoft / FluentRest / 10294975376

08 Aug 2024 02:31AM UTC coverage: 56.682%. First build
10294975376

push

github

pwelter34
add bool overload for If methods

277 of 632 branches covered (43.83%)

Branch coverage included in aggregate %.

0 of 27 new or added lines in 2 files covered. (0.0%)

847 of 1351 relevant lines covered (62.69%)

72.59 hits per line

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

33.71
/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)
78✔
13
    {
14
    }
78✔
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)
189✔
29
    {
30
    }
189✔
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)
24!
41
            throw new ArgumentNullException(nameof(builder));
×
42

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

46
        return this as TBuilder;
24✔
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)
15!
59
            throw new ArgumentNullException(nameof(name));
×
60

61
        if (value == null)
15✔
62
            RequestMessage.Headers.Remove(name);
3✔
63
        else
64
            RequestMessage.Headers.Add(name, value);
12✔
65

66
        return this as TBuilder;
15✔
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
    {
NEW
95
        if (!condition)
×
NEW
96
            return this as TBuilder;
×
97

NEW
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)
27!
111
            throw new ArgumentNullException(nameof(path));
×
112

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

116
        RequestMessage.Synchronize();
27✔
117

118

119
        return this as TBuilder;
27✔
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)
24!
131
            throw new ArgumentNullException(nameof(path));
×
132

133
        var uri = new Uri(path, UriKind.Absolute);
24✔
134
        return BaseUri(uri);
24✔
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)
3!
161
            throw new ArgumentNullException(nameof(path));
×
162

163
        var u = new Uri(path, UriKind.Absolute);
3✔
164
        return BaseUri(u);
3✔
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)
228!
196
            return this as TBuilder;
×
197

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

201
        RequestMessage.Synchronize();
228✔
202

203
        return this as TBuilder;
228✔
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 AppendPath(IEnumerable<string> paths)
231
    {
232
        if (paths == null)
3!
233
            return this as TBuilder;
×
234

235
        var urlBuilder = RequestMessage.GetUrlBuilder();
3✔
236
        urlBuilder.AppendPath(paths);
3✔
237

238
        RequestMessage.Synchronize();
3✔
239

240
        return this as TBuilder;
3✔
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 AppendPath(params string[] paths)
249
    {
250
        if (paths == null)
3!
251
            return this as TBuilder;
×
252

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

256
        RequestMessage.Synchronize();
3✔
257

258
        return this as TBuilder;
3✔
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

NEW
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
    {
NEW
290
        if (path == null)
×
NEW
291
            return this as TBuilder;
×
292

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

NEW
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

NEW
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
    {
NEW
326
        if (path == null)
×
NEW
327
            return this as TBuilder;
×
328

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

NEW
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)
117!
346
            throw new ArgumentNullException(nameof(name));
×
347

348

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

352
        RequestMessage.Synchronize();
117✔
353

354
        return this as TBuilder;
117✔
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)
108!
371
            throw new ArgumentNullException(nameof(name));
×
372

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

377
    /// <summary>
378
    /// Appends the specified <paramref name="name"/> and <paramref name="value"/> to the request Uri if the specified <paramref name="condition"/> is true.
379
    /// </summary>
380
    /// <param name="condition">If condition is true, query string will be added; otherwise ignore query string.</param>
381
    /// <param name="name">The query parameter name.</param>
382
    /// <param name="value">The query parameter value.</param>
383
    /// <returns>A fluent request builder.</returns>
384
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
385
    public TBuilder QueryStringIf(Func<bool> condition, string name, string value)
386
    {
387
        if (condition == null || !condition())
×
388
            return this as TBuilder;
×
389

390
        return QueryString(name, value);
×
391
    }
392

393
    /// <summary>
394
    /// Appends the specified <paramref name="name"/> and <paramref name="value"/> to the request Uri if the specified <paramref name="condition"/> is true.
395
    /// </summary>
396
    /// <param name="condition">If condition is true, query string will be added; otherwise ignore query string.</param>
397
    /// <param name="name">The query parameter name.</param>
398
    /// <param name="value">The query parameter value.</param>
399
    /// <returns>A fluent request builder.</returns>
400
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
401
    public TBuilder QueryStringIf(bool condition, string name, string value)
402
    {
NEW
403
        if (!condition)
×
NEW
404
            return this as TBuilder;
×
405

NEW
406
        return QueryString(name, value);
×
407
    }
408

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

425
        return QueryString(name, value);
×
426
    }
427

428
    /// <summary>
429
    /// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the request Uri if the specified <paramref name="condition" /> is true.
430
    /// </summary>
431
    /// <typeparam name="TValue">The type of the value.</typeparam>
432
    /// <param name="condition">If condition is true, query string will be added; otherwise ignore query string.</param>
433
    /// <param name="name">The query parameter name.</param>
434
    /// <param name="value">The query parameter value.</param>
435
    /// <returns>
436
    /// A fluent request builder.
437
    /// </returns>
438
    /// <exception cref="System.ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
439
    public TBuilder QueryStringIf<TValue>(bool condition, string name, TValue value)
440
    {
NEW
441
        if (!condition)
×
NEW
442
            return this as TBuilder;
×
443

NEW
444
        return QueryString(name, value);
×
445
    }
446
}
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