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

loresoft / FluentRest / 7318593291

25 Dec 2023 02:55AM UTC coverage: 52.206%. Remained the same
7318593291

Pull #158

github

web-flow
Merge 38e3e6116 into 7b58509ff
Pull Request #158: Bump xunit.runner.visualstudio from 2.5.4 to 2.5.6

182 of 438 branches covered (0.0%)

Branch coverage included in aggregate %.

516 of 899 relevant lines covered (57.4%)

86.76 hits per line

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

39.74
/src/FluentRest/QueryBuilder.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Net.Http;
4

5
namespace FluentRest;
6

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

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

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

47
        var headerBuilder = new HeaderBuilder(RequestMessage);
24✔
48
        builder(headerBuilder);
24✔
49

50
        return this as TBuilder;
24✔
51
    }
52

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

65
        if (value == null)
15✔
66
            RequestMessage.Headers.Remove(name);
3✔
67
        else
68
            RequestMessage.Headers.Add(name, value);
12✔
69

70
        return this as TBuilder;
15✔
71
    }
72

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

86
        return Header(name, value);
×
87
    }
88

89

90
    /// <summary>
91
    /// Sets the base URI address used when sending requests.
92
    /// </summary>
93
    /// <param name="path">The path.</param>
94
    /// <returns>A fluent request builder.</returns>
95
    /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null" />.</exception>
96
    public TBuilder BaseUri(Uri path)
97
    {
98
        if (path == null)
27!
99
            throw new ArgumentNullException(nameof(path));
×
100

101
        var urlBuilder = new UrlBuilder(path);
27✔
102
        RequestMessage.SetUrlBuilder(urlBuilder);
27✔
103

104
        RequestMessage.Synchronize();
27✔
105

106

107
        return this as TBuilder;
27✔
108
    }
109

110
    /// <summary>
111
    /// Sets the base URI address used when sending requests.
112
    /// </summary>
113
    /// <param name="path">The path.</param>
114
    /// <returns>A fluent request builder.</returns>
115
    /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null" />.</exception>
116
    public TBuilder BaseUri(string path)
117
    {
118
        if (path == null)
24!
119
            throw new ArgumentNullException(nameof(path));
×
120

121
        var uri = new Uri(path, UriKind.Absolute);
24✔
122
        return BaseUri(uri);
24✔
123
    }
124

125

126
    /// <summary>
127
    /// Sets the base URI from the specified <paramref name="path"/>.
128
    /// </summary>
129
    /// <param name="path">The full Uri path.</param>
130
    /// <returns>A fluent request builder.</returns>
131
    /// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null" />.</exception>
132
    public TBuilder FullUri(Uri path)
133
    {
134
        if (path == null)
×
135
            throw new ArgumentNullException(nameof(path));
×
136

137
        return BaseUri(path);
×
138
    }
139

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

151
        var u = new Uri(path, UriKind.Absolute);
3✔
152
        return BaseUri(u);
3✔
153
    }
154

155

156
    /// <summary>
157
    /// Appends the specified <paramref name="path"/> to the BaseUri of the request.
158
    /// </summary>
159
    /// <param name="path">The path to append.</param>
160
    /// <returns>A fluent request builder.</returns>
161
    public TBuilder AppendPath(Uri path)
162
    {
163
        if (path == null)
×
164
            return this as TBuilder;
×
165

166
        var urlBuilder = RequestMessage.GetUrlBuilder();
×
167
        urlBuilder.AppendPath(path);
×
168

169
        RequestMessage.Synchronize();
×
170

171
        return this as TBuilder;
×
172
    }
173

174
    /// <summary>
175
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
176
    /// </summary>
177
    /// <param name="path">The path to append.</param>
178
    /// <returns>
179
    /// A fluent request builder.
180
    /// </returns>
181
    public TBuilder AppendPath(string path)
182
    {
183
        if (path == null)
228!
184
            return this as TBuilder;
×
185

186
        var urlBuilder = RequestMessage.GetUrlBuilder();
228✔
187
        urlBuilder.AppendPath(path);
228✔
188

189
        RequestMessage.Synchronize();
228✔
190

191
        return this as TBuilder;
228✔
192
    }
193

194
    /// <summary>
195
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
196
    /// </summary>
197
    /// <typeparam name="TValue">The type of the value.</typeparam>
198
    /// <param name="path">The path to append.</param>
199
    /// <returns>A fluent request builder.</returns>
200
    public TBuilder AppendPath<TValue>(TValue path)
201
    {
202
        if (path == null)
×
203
            return this as TBuilder;
×
204

205
        var urlBuilder = RequestMessage.GetUrlBuilder();
×
206
        urlBuilder.AppendPath(path);
×
207

208
        RequestMessage.Synchronize();
×
209

210
        return this as TBuilder;
×
211
    }
212

213
    /// <summary>
214
    /// Appends the specified <paramref name="paths"/> to the BaseUri of the request.
215
    /// </summary>
216
    /// <param name="paths">The paths to append.</param>
217
    /// <returns>A fluent request builder.</returns>
218
    public TBuilder AppendPath(IEnumerable<string> paths)
219
    {
220
        if (paths == null)
3!
221
            return this as TBuilder;
×
222

223
        var urlBuilder = RequestMessage.GetUrlBuilder();
3✔
224
        urlBuilder.AppendPath(paths);
3✔
225

226
        RequestMessage.Synchronize();
3✔
227

228
        return this as TBuilder;
3✔
229
    }
230

231
    /// <summary>
232
    /// Appends the specified <paramref name="paths"/> to the BaseUri of the request.
233
    /// </summary>
234
    /// <param name="paths">The paths to append.</param>
235
    /// <returns>A fluent request builder.</returns>
236
    public TBuilder AppendPath(params string[] paths)
237
    {
238
        if (paths == null)
3!
239
            return this as TBuilder;
×
240

241
        var urlBuilder = RequestMessage.GetUrlBuilder();
3✔
242
        urlBuilder.AppendPaths(paths);
3✔
243

244
        RequestMessage.Synchronize();
3✔
245

246
        return this as TBuilder;
3✔
247
    }
248

249
    /// <summary>
250
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
251
    /// </summary>
252
    /// <param name="condition">If condition is true, append path will be added; otherwise ignore path.</param>
253
    /// <param name="path">The path to append.</param>
254
    /// <returns>
255
    /// A fluent request builder.
256
    /// </returns>
257
    public TBuilder AppendPathIf(Func<bool> condition, string path)
258
    {
259
        if (path == null)
×
260
            return this as TBuilder;
×
261

262
        if (condition == null || !condition())
×
263
            return this as TBuilder;
×
264

265
        var urlBuilder = RequestMessage.GetUrlBuilder();
×
266
        urlBuilder.AppendPath(path);
×
267

268
        RequestMessage.Synchronize();
×
269

270
        return this as TBuilder;
×
271
    }
272

273
    /// <summary>
274
    /// Appends the specified <paramref name="path" /> to the BaseUri of the request.
275
    /// </summary>
276
    /// <typeparam name="TValue">The type of the value.</typeparam>
277
    /// <param name="condition">If condition is true, append path will be added; otherwise ignore path.</param>
278
    /// <param name="path">The path to append.</param>
279
    /// <returns>A fluent request builder.</returns>
280
    public TBuilder AppendPathIf<TValue>(Func<bool> condition, TValue path)
281
    {
282
        if (path == null)
×
283
            return this as TBuilder;
×
284

285
        if (condition == null || !condition())
×
286
            return this as TBuilder;
×
287

288
        var urlBuilder = RequestMessage.GetUrlBuilder();
×
289
        urlBuilder.AppendPath(path);
×
290

291
        RequestMessage.Synchronize();
×
292

293
        return this as TBuilder;
×
294
    }
295

296

297
    /// <summary>
298
    /// Appends the specified <paramref name="name"/> and <paramref name="value"/> to the request Uri.
299
    /// </summary>
300
    /// <param name="name">The query parameter name.</param>
301
    /// <param name="value">The query parameter value.</param>
302
    /// <returns>A fluent request builder.</returns>
303
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
304
    public TBuilder QueryString(string name, string value)
305
    {
306
        if (name == null)
117!
307
            throw new ArgumentNullException(nameof(name));
×
308

309

310
        var urlBuilder = RequestMessage.GetUrlBuilder();
117✔
311
        urlBuilder.AppendQuery(name, value);
117✔
312

313
        RequestMessage.Synchronize();
117✔
314

315
        return this as TBuilder;
117✔
316

317
    }
318

319
    /// <summary>
320
    /// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the request Uri.
321
    /// </summary>
322
    /// <typeparam name="TValue">The type of the value.</typeparam>
323
    /// <param name="name">The query parameter name.</param>
324
    /// <param name="value">The query parameter value.</param>
325
    /// <returns>
326
    /// A fluent request builder.
327
    /// </returns>
328
    /// <exception cref="System.ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
329
    public TBuilder QueryString<TValue>(string name, TValue value)
330
    {
331
        if (name == null)
108!
332
            throw new ArgumentNullException(nameof(name));
×
333

334
        var v = value != null ? value.ToString() : string.Empty;
108!
335
        return QueryString(name, v);
108✔
336
    }
337

338
    /// <summary>
339
    /// Appends the specified <paramref name="name"/> and <paramref name="value"/> to the request Uri if the specified <paramref name="condition"/> is true.
340
    /// </summary>
341
    /// <param name="condition">If condition is true, query string will be added; otherwise ignore query string.</param>
342
    /// <param name="name">The query parameter name.</param>
343
    /// <param name="value">The query parameter value.</param>
344
    /// <returns>A fluent request builder.</returns>
345
    /// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
346
    public TBuilder QueryStringIf(Func<bool> condition, string name, string value)
347
    {
348
        if (condition == null || !condition())
×
349
            return this as TBuilder;
×
350

351
        return QueryString(name, value);
×
352
    }
353

354
    /// <summary>
355
    /// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the request Uri if the specified <paramref name="condition" /> is true.
356
    /// </summary>
357
    /// <typeparam name="TValue">The type of the value.</typeparam>
358
    /// <param name="condition">If condition is true, query string will be added; otherwise ignore query string.</param>
359
    /// <param name="name">The query parameter name.</param>
360
    /// <param name="value">The query parameter value.</param>
361
    /// <returns>
362
    /// A fluent request builder.
363
    /// </returns>
364
    /// <exception cref="System.ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
365
    public TBuilder QueryStringIf<TValue>(Func<bool> condition, string name, TValue value)
366
    {
367
        if (condition == null || !condition())
×
368
            return this as TBuilder;
×
369

370
        return QueryString(name, value);
×
371
    }
372
}
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