Skip to content

Commit

Permalink
add bool overload for If methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Aug 8, 2024
1 parent 88b5c7a commit 15a527b
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 8 deletions.
35 changes: 35 additions & 0 deletions src/FluentRest/PostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ public TBuilder FormValueIf(Func<bool> condition, string name, string value)
return FormValue(name, value);
}

/// <summary>
/// Appends the specified <paramref name="name"/> and <paramref name="value"/> to the form post body if the specified <paramref name="condition"/> is true.
/// </summary>
/// <param name="condition">If condition is true, form data will be added; otherwise ignore form data.</param>
/// <param name="name">The form parameter name.</param>
/// <param name="value">The form parameter value.</param>
/// <returns>A fluent request builder.</returns>
/// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
public TBuilder FormValueIf(bool condition, string name, string value)
{
if (!condition)
return this as TBuilder;

return FormValue(name, value);
}

/// <summary>
/// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the form post body if the specified <paramref name="condition" /> is true.
/// </summary>
Expand All @@ -130,6 +146,25 @@ public TBuilder FormValueIf<TValue>(Func<bool> condition, string name, TValue va
return FormValue(name, value);
}

/// <summary>
/// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the form post body if the specified <paramref name="condition" /> is true.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="condition">If condition is true, form data will be added; otherwise ignore form data.</param>
/// <param name="name">The form parameter name.</param>
/// <param name="value">The form parameter value.</param>
/// <returns>
/// A fluent request builder.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
public TBuilder FormValueIf<TValue>(bool condition, string name, TValue value)
{
if (!condition)
return this as TBuilder;

return FormValue(name, value);
}

/// <summary>
/// Sets the contents of the HTTP message with the <see cref="HttpContent"/> value.
/// </summary>
Expand Down
94 changes: 86 additions & 8 deletions src/FluentRest/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ public TBuilder HeaderIf(Func<bool> condition, string name, string value)
return Header(name, value);
}

/// <summary>
/// Sets HTTP header with the specified <paramref name="name"/> and <paramref name="value"/> if the specified <paramref name="condition"/> is true.
/// </summary>
/// <param name="condition">If condition is true, header will be added; otherwise ignore header.</param>
/// <param name="name">The header name.</param>
/// <param name="value">The header value.</param>
/// <returns>A fluent request builder.</returns>
/// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
public TBuilder HeaderIf(bool condition, string name, string value)
{
if (!condition)
return this as TBuilder;

return Header(name, value);
}


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

var urlBuilder = RequestMessage.GetUrlBuilder();
urlBuilder.AppendPath(path);
return AppendPath(path);
}

RequestMessage.Synchronize();
/// <summary>
/// Appends the specified <paramref name="path" /> to the BaseUri of the request.
/// </summary>
/// <param name="condition">If condition is true, append path will be added; otherwise ignore path.</param>
/// <param name="path">The path to append.</param>
/// <returns>
/// A fluent request builder.
/// </returns>
public TBuilder AppendPathIf(bool condition, string path)
{
if (path == null)
return this as TBuilder;

return this as TBuilder;
if (!condition)
return this as TBuilder;

return AppendPath(path);
}

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

var urlBuilder = RequestMessage.GetUrlBuilder();
urlBuilder.AppendPath(path);
return AppendPath(path);
}

RequestMessage.Synchronize();
/// <summary>
/// Appends the specified <paramref name="path" /> to the BaseUri of the request.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="condition">If condition is true, append path will be added; otherwise ignore path.</param>
/// <param name="path">The path to append.</param>
/// <returns>A fluent request builder.</returns>
public TBuilder AppendPathIf<TValue>(bool condition, TValue path)
{
if (path == null)
return this as TBuilder;

return this as TBuilder;
if (!condition)
return this as TBuilder;

return AppendPath(path);
}


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

/// <summary>
/// Appends the specified <paramref name="name"/> and <paramref name="value"/> to the request Uri if the specified <paramref name="condition"/> is true.
/// </summary>
/// <param name="condition">If condition is true, query string will be added; otherwise ignore query string.</param>
/// <param name="name">The query parameter name.</param>
/// <param name="value">The query parameter value.</param>
/// <returns>A fluent request builder.</returns>
/// <exception cref="ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
public TBuilder QueryStringIf(bool condition, string name, string value)
{
if (!condition)
return this as TBuilder;

return QueryString(name, value);
}

/// <summary>
/// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the request Uri if the specified <paramref name="condition" /> is true.
/// </summary>
Expand All @@ -365,4 +424,23 @@ public TBuilder QueryStringIf<TValue>(Func<bool> condition, string name, TValue

return QueryString(name, value);
}

/// <summary>
/// Appends the specified <paramref name="name" /> and <paramref name="value" /> to the request Uri if the specified <paramref name="condition" /> is true.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="condition">If condition is true, query string will be added; otherwise ignore query string.</param>
/// <param name="name">The query parameter name.</param>
/// <param name="value">The query parameter value.</param>
/// <returns>
/// A fluent request builder.
/// </returns>
/// <exception cref="System.ArgumentNullException"><paramref name="name" /> is <see langword="null" />.</exception>
public TBuilder QueryStringIf<TValue>(bool condition, string name, TValue value)
{
if (!condition)
return this as TBuilder;

return QueryString(name, value);
}
}

0 comments on commit 15a527b

Please sign in to comment.