Skip to content

Tuple Support for Headers #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Test/Flurl.Test/Http/SettingsExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public void can_set_headers_from_anon_object() {
Assert.AreEqual(2, sc.Headers["one"]);
}

[Test]
public void can_set_headers_from_tuple() {
var sc = GetSettingsContainer().WithHeaders(("a", 1), ("b", "c"));
Assert.AreEqual(2, sc.Headers.Count);
Assert.AreEqual(1, sc.Headers["a"]);
Assert.AreEqual("c", sc.Headers["b"]);
}

[Test]
public void can_remove_header_by_setting_null() {
var sc = GetSettingsContainer().WithHeaders(new { a = 1, b = 2 });
Expand Down
3 changes: 2 additions & 1 deletion src/Flurl.Http/Flurl.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Flurl" Version="3.0.0-pre3" />
<None Include="..\..\icon.png" Pack="true" PackagePath="\"/>
<PackageReference Include="System.ValueTuple" Version="4.3.0" />
<None Include="..\..\icon.png" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
Expand Down
121 changes: 67 additions & 54 deletions src/Flurl.Http/HeaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,79 @@ namespace Flurl.Http
/// <summary>
/// Fluent extension methods for working with HTTP request headers.
/// </summary>
public static class HeaderExtensions
{
/// <summary>
/// Sets an HTTP header to be sent with this IFlurlRequest or all requests made with this IFlurlClient.
/// </summary>
/// <param name="clientOrRequest">The IFlurlClient or IFlurlRequest.</param>
/// <param name="name">HTTP header name.</param>
/// <param name="value">HTTP header value.</param>
/// <returns>This IFlurlClient or IFlurlRequest.</returns>
public static T WithHeader<T>(this T clientOrRequest, string name, object value) where T : IHttpSettingsContainer {
if (value == null && clientOrRequest.Headers.ContainsKey(name))
clientOrRequest.Headers.Remove(name);
public static class HeaderExtensions
{
/// <summary>
/// Sets an HTTP header to be sent with this IFlurlRequest or all requests made with this IFlurlClient.
/// </summary>
/// <param name="clientOrRequest">The IFlurlClient or IFlurlRequest.</param>
/// <param name="name">HTTP header name.</param>
/// <param name="value">HTTP header value.</param>
/// <returns>This IFlurlClient or IFlurlRequest.</returns>
public static T WithHeader<T>(this T clientOrRequest, string name, object value) where T : IHttpSettingsContainer {
if (value == null && clientOrRequest.Headers.ContainsKey(name))
clientOrRequest.Headers.Remove(name);
else if (value != null)
clientOrRequest.Headers[name] = value;
return clientOrRequest;
}
clientOrRequest.Headers[name] = value;
return clientOrRequest;
}

/// <summary>
/// Sets HTTP headers based on property names/values of the provided object, or keys/values if object is a dictionary, to be sent with this IFlurlRequest or all requests made with this IFlurlClient.
/// </summary>
/// <param name="clientOrRequest">The IFlurlClient or IFlurlRequest.</param>
/// <param name="headers">Names/values of HTTP headers to set. Typically an anonymous object or IDictionary.</param>
/// <param name="replaceUnderscoreWithHyphen">If true, underscores in property names will be replaced by hyphens. Default is true.</param>
/// <returns>This IFlurlClient or IFlurlRequest.</returns>
public static T WithHeaders<T>(this T clientOrRequest, object headers, bool replaceUnderscoreWithHyphen = true) where T : IHttpSettingsContainer {
if (headers == null)
return clientOrRequest;
/// <summary>
/// Sets HTTP headers based on property names/values of the provided object, or keys/values if object is a dictionary, to be sent with this IFlurlRequest or all requests made with this IFlurlClient.
/// </summary>
/// <param name="clientOrRequest">The IFlurlClient or IFlurlRequest.</param>
/// <param name="headers">Names/values of HTTP headers to set. Typically an anonymous object or IDictionary.</param>
/// <param name="replaceUnderscoreWithHyphen">If true, underscores in property names will be replaced by hyphens. Default is true.</param>
/// <returns>This IFlurlClient or IFlurlRequest.</returns>
public static T WithHeaders<T>(this T clientOrRequest, object headers, bool replaceUnderscoreWithHyphen = true) where T : IHttpSettingsContainer {
if (headers == null)
return clientOrRequest;

// underscore replacement only applies when object properties are parsed to kv pairs
replaceUnderscoreWithHyphen = replaceUnderscoreWithHyphen && !(headers is string) && !(headers is IEnumerable);
replaceUnderscoreWithHyphen = replaceUnderscoreWithHyphen && !(headers is string) && !(headers is IEnumerable);

foreach (var kv in headers.ToKeyValuePairs()) {
var key = replaceUnderscoreWithHyphen ? kv.Key.Replace("_", "-") : kv.Key;
clientOrRequest.WithHeader(key, kv.Value);
}
foreach (var kv in headers.ToKeyValuePairs()) {
var key = replaceUnderscoreWithHyphen ? kv.Key.Replace("_", "-") : kv.Key;
clientOrRequest.WithHeader(key, kv.Value);
}

return clientOrRequest;
}
return clientOrRequest;
}

/// <summary>
/// Sets HTTP authorization header according to Basic Authentication protocol to be sent with this IFlurlRequest or all requests made with this IFlurlClient.
/// </summary>
/// <param name="clientOrRequest">The IFlurlClient or IFlurlRequest.</param>
/// <param name="username">Username of authenticating user.</param>
/// <param name="password">Password of authenticating user.</param>
/// <returns>This IFlurlClient or IFlurlRequest.</returns>
public static T WithBasicAuth<T>(this T clientOrRequest, string username, string password) where T : IHttpSettingsContainer {
// http://stackoverflow.com/questions/14627399/setting-authorization-header-of-httpclient
var encodedCreds = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));
return clientOrRequest.WithHeader("Authorization", $"Basic {encodedCreds}");
}
/// <summary>
/// Sets HTTP headers based on name/value tuples, to be sent with this IFlurlRequest or all requests made with this IFlurlClient.
/// </summary>
/// <param name="clientOrRequest">The IFlurlClient or IFlurlRequest.</param>
/// <param name="headers">Names/values of HTTP headers to set.</param>
/// <returns>This IFlurlClient or IFlurlRequest.</returns>
public static T WithHeaders<T>(this T clientOrRequest, params (string Name, object Value)[] headers) where T : IHttpSettingsContainer {
foreach (var (name, value) in headers)
clientOrRequest.WithHeader(name, value);

/// <summary>
/// Sets HTTP authorization header with acquired bearer token according to OAuth 2.0 specification to be sent with this IFlurlRequest or all requests made with this IFlurlClient.
/// </summary>
/// <param name="clientOrRequest">The IFlurlClient or IFlurlRequest.</param>
/// <param name="token">The acquired bearer token to pass.</param>
/// <returns>This IFlurlClient or IFlurlRequest.</returns>
public static T WithOAuthBearerToken<T>(this T clientOrRequest, string token) where T : IHttpSettingsContainer {
return clientOrRequest.WithHeader("Authorization", $"Bearer {token}");
}
}
return clientOrRequest;
}

/// <summary>
/// Sets HTTP authorization header according to Basic Authentication protocol to be sent with this IFlurlRequest or all requests made with this IFlurlClient.
/// </summary>
/// <param name="clientOrRequest">The IFlurlClient or IFlurlRequest.</param>
/// <param name="username">Username of authenticating user.</param>
/// <param name="password">Password of authenticating user.</param>
/// <returns>This IFlurlClient or IFlurlRequest.</returns>
public static T WithBasicAuth<T>(this T clientOrRequest, string username, string password) where T : IHttpSettingsContainer {
// http://stackoverflow.com/questions/14627399/setting-authorization-header-of-httpclient
var encodedCreds = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));
return clientOrRequest.WithHeader("Authorization", $"Basic {encodedCreds}");
}

/// <summary>
/// Sets HTTP authorization header with acquired bearer token according to OAuth 2.0 specification to be sent with this IFlurlRequest or all requests made with this IFlurlClient.
/// </summary>
/// <param name="clientOrRequest">The IFlurlClient or IFlurlRequest.</param>
/// <param name="token">The acquired bearer token to pass.</param>
/// <returns>This IFlurlClient or IFlurlRequest.</returns>
public static T WithOAuthBearerToken<T>(this T clientOrRequest, string token) where T : IHttpSettingsContainer {
return clientOrRequest.WithHeader("Authorization", $"Bearer {token}");
}
}
}