Skip to content

Commit 15a527b

Browse files
committed
add bool overload for If methods
1 parent 88b5c7a commit 15a527b

File tree

2 files changed

+121
-8
lines changed

2 files changed

+121
-8
lines changed

src/FluentRest/PostBuilder.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ public TBuilder FormValueIf(Func<bool> condition, string name, string value)
111111
return FormValue(name, value);
112112
}
113113

114+
/// <summary>
115+
/// Appends the specified <paramref name="name"/> and <paramref name="value"/> to the form post body if the specified <paramref name="condition"/> is true.
116+
/// </summary>
117+
/// <param name="condition">If condition is true, form data will be added; otherwise ignore form data.</param>
118+
/// <param name="name">The form parameter name.</param>
119+
/// <param name="value">The form parameter value.</param>
120+
/// <returns>A fluent request builder.</returns>
121+
/// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
122+
public TBuilder FormValueIf(bool condition, string name, string value)
123+
{
124+
if (!condition)
125+
return this as TBuilder;
126+
127+
return FormValue(name, value);
128+
}
129+
114130
/// <summary>
115131
/// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the form post body if the specified <paramref name="condition" /> is true.
116132
/// </summary>
@@ -130,6 +146,25 @@ public TBuilder FormValueIf<TValue>(Func<bool> condition, string name, TValue va
130146
return FormValue(name, value);
131147
}
132148

149+
/// <summary>
150+
/// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the form post body if the specified <paramref name="condition" /> is true.
151+
/// </summary>
152+
/// <typeparam name="TValue">The type of the value.</typeparam>
153+
/// <param name="condition">If condition is true, form data will be added; otherwise ignore form data.</param>
154+
/// <param name="name">The form parameter name.</param>
155+
/// <param name="value">The form parameter value.</param>
156+
/// <returns>
157+
/// A fluent request builder.
158+
/// </returns>
159+
/// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
160+
public TBuilder FormValueIf<TValue>(bool condition, string name, TValue value)
161+
{
162+
if (!condition)
163+
return this as TBuilder;
164+
165+
return FormValue(name, value);
166+
}
167+
133168
/// <summary>
134169
/// Sets the contents of the HTTP message with the <see cref="HttpContent"/> value.
135170
/// </summary>

src/FluentRest/QueryBuilder.cs

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ public TBuilder HeaderIf(Func<bool> condition, string name, string value)
8282
return Header(name, value);
8383
}
8484

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+
85101

86102
/// <summary>
87103
/// Sets the base URI address used when sending requests.
@@ -258,12 +274,26 @@ public TBuilder AppendPathIf(Func<bool> condition, string path)
258274
if (condition == null || !condition())
259275
return this as TBuilder;
260276

261-
var urlBuilder = RequestMessage.GetUrlBuilder();
262-
urlBuilder.AppendPath(path);
277+
return AppendPath(path);
278+
}
263279

264-
RequestMessage.Synchronize();
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;
265292

266-
return this as TBuilder;
293+
if (!condition)
294+
return this as TBuilder;
295+
296+
return AppendPath(path);
267297
}
268298

269299
/// <summary>
@@ -281,12 +311,25 @@ public TBuilder AppendPathIf<TValue>(Func<bool> condition, TValue path)
281311
if (condition == null || !condition())
282312
return this as TBuilder;
283313

284-
var urlBuilder = RequestMessage.GetUrlBuilder();
285-
urlBuilder.AppendPath(path);
314+
return AppendPath(path);
315+
}
286316

287-
RequestMessage.Synchronize();
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;
288328

289-
return this as TBuilder;
329+
if (!condition)
330+
return this as TBuilder;
331+
332+
return AppendPath(path);
290333
}
291334

292335

@@ -347,6 +390,22 @@ public TBuilder QueryStringIf(Func<bool> condition, string name, string value)
347390
return QueryString(name, value);
348391
}
349392

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+
{
403+
if (!condition)
404+
return this as TBuilder;
405+
406+
return QueryString(name, value);
407+
}
408+
350409
/// <summary>
351410
/// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the request Uri if the specified <paramref name="condition" /> is true.
352411
/// </summary>
@@ -365,4 +424,23 @@ public TBuilder QueryStringIf<TValue>(Func<bool> condition, string name, TValue
365424

366425
return QueryString(name, value);
367426
}
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+
{
441+
if (!condition)
442+
return this as TBuilder;
443+
444+
return QueryString(name, value);
445+
}
368446
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy