using System; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using Flurl.Util; namespace Flurl.Http { public static class ClientConfigExtensions { /// /// Fluently specify that an existing FlurlClient should be used to call the Url, rather than creating a new one. /// Enables re-using the underlying HttpClient. /// /// The FlurlClient to use in calling the Url /// public static FlurlClient WithClient(this Url url, FlurlClient fc) { fc.Url = url; return fc; } /// /// Fluently specify that an existing FlurlClient should be used to call the Url, rather than creating a new one. /// Enables re-using the underlying HttpClient. /// /// The FlurlClient to use in calling the Url /// public static FlurlClient WithClient(this string url, FlurlClient fc) { return new Url(url).WithClient(fc); } /// /// Fluently specify the URL to be called with the current FlurlClient instance. /// /// The Url to call. /// public static FlurlClient WithUrl(this FlurlClient client, Url url) { client.Url = url; return client; } /// /// Provides access to modifying the underlying HttpClient. /// /// Action to perform on the HttpClient. /// The FlurlClient with the modified HttpClient public static FlurlClient ConfigureHttpClient(this FlurlClient client, Action action) { action(client.HttpClient); return client; } /// /// Creates a FlurlClient from the URL and provides access to modifying the underlying HttpClient. /// /// Action to perform on the HttpClient. /// The FlurlClient with the modified HttpClient public static FlurlClient ConfigureHttpClient(this string url, Action action) { return new FlurlClient(url, true).ConfigureHttpClient(action); } /// /// Creates a FlurlClient from the URL and provides access to modifying the underlying HttpClient. /// /// Action to perform on the HttpClient. /// The FlurlClient with the modified HttpClient public static FlurlClient ConfigureHttpClient(this Url url, Action action) { return new FlurlClient(url, true).ConfigureHttpClient(action); } /// /// Sets the client timout to the specified timespan. /// /// Time to wait before the request times out. /// The modified FlurlClient. public static FlurlClient WithTimeout(this FlurlClient client, TimeSpan timespan) { client.HttpClient.Timeout = timespan; return client; } /// /// Creates a FlurlClient from the URL and sets the client timout to the specified timespan. /// /// Time to wait before the request times out. /// The created FlurlClient. public static FlurlClient WithTimeout(this string url, TimeSpan timespan) { return new FlurlClient(url, true).WithTimeout(timespan); } /// /// Creates a FlurlClient from the URL and sets the client timout to the specified timespan. /// /// Time to wait before the request times out. /// The created FlurlClient. public static FlurlClient WithTimeout(this Url url, TimeSpan timespan) { return new FlurlClient(url, true).WithTimeout(timespan); } /// /// Sets the client timout to the specified number of seconds. /// /// Number of seconds to wait before the request times out. /// The modified FlurlClient. public static FlurlClient WithTimeout(this FlurlClient client, int seconds) { return client.WithTimeout(TimeSpan.FromSeconds(seconds)); } /// /// Creates a FlurlClient from the URL and sets the client timout to the specified number of seconds. /// /// Number of seconds to wait before the request times out. /// The created FlurlClient. public static FlurlClient WithTimeout(this string url, int seconds) { return new FlurlClient(url, true).WithTimeout(seconds); } /// /// Creates a FlurlClient from the URL and sets the client timout to the specified number of seconds. /// /// Number of seconds to wait before the request times out. /// The created FlurlClient. public static FlurlClient WithTimeout(this Url url, int seconds) { return new FlurlClient(url, true).WithTimeout(seconds); } /// /// Sets an HTTP header to be sent with all requests made with this FlurlClient. /// /// HTTP header name. /// HTTP header value. /// The modified FlurlClient. public static FlurlClient WithHeader(this FlurlClient client, string name, object value) { var values = new[] { (value == null) ? null : value.ToString() }; client.HttpClient.DefaultRequestHeaders.Add(name, values); return client; } /// /// Creates a FlurlClient from the URL and sets an HTTP header to be sent with all requests made with this FlurlClient. /// /// HTTP header name. /// HTTP header value. /// The modified FlurlClient. public static FlurlClient WithHeader(this string url, string name, object value) { return new FlurlClient(url, true).WithHeader(name, value); } /// /// Creates a FlurlClient from the URL and sets an HTTP header to be sent with all requests made with this FlurlClient. /// /// HTTP header name. /// HTTP header value. /// The modified FlurlClient. public static FlurlClient WithHeader(this Url url, string name, object value) { return new FlurlClient(url, true).WithHeader(name, value); } /// /// Sets HTTP headers based on property names/values of the provided object, or keys/values if object is a dictionary, to be sent with all requests made with this FlurlClient. /// /// Names/values of HTTP headers to set. Typically an anonymous object or IDictionary. /// The modified FlurlClient. public static FlurlClient WithHeaders(this FlurlClient client, object headers) { if (headers == null) return client; foreach (var kv in headers.ToKeyValuePairs()) { if (kv.Value == null) continue; client.HttpClient.DefaultRequestHeaders.Add(kv.Key, new[] { kv.Value.ToString() }); } return client; } /// /// Creates a FlurlClient from the URL and sets HTTP headers based on property names/values of the provided object, or keys/values if object is a dictionary, to be sent with all requests made with this FlurlClient. /// /// Names/values of HTTP headers to set. Typically an anonymous object or IDictionary. /// The modified FlurlClient. public static FlurlClient WithHeaders(this Url url, object headers) { return new FlurlClient(url, true).WithHeaders(headers); } /// /// Creates a FlurlClient from the URL and sets HTTP headers based on property names/values of the provided object, or keys/values if object is a dictionary, to be sent with all requests made with this FlurlClient. /// /// Names/values of HTTP headers to set. Typically an anonymous object or IDictionary. /// The modified FlurlClient. public static FlurlClient WithHeaders(this string url, object headers) { return new FlurlClient(url, true).WithHeaders(headers); } /// /// Sets HTTP authorization header according to Basic Authentication protocol to be sent with all requests made with this FlurlClient. /// /// Username of authenticating user. /// Password of authenticating user. /// The modified FlurlClient. public static FlurlClient WithBasicAuth(this FlurlClient client, string username, string password) { // http://stackoverflow.com/questions/14627399/setting-authorization-header-of-httpclient var value = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", username, password))); client.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", value); return client; } /// /// Creates a FlurlClient from the URL and sets HTTP authorization header according to Basic Authentication protocol to be sent with all requests made with this FlurlClient. /// /// Username of authenticating user. /// Password of authenticating user. /// The modified FlurlClient. public static FlurlClient WithBasicAuth(this Url url, string username, string password) { return new FlurlClient(url, true).WithBasicAuth(username, password); } /// /// Creates a FlurlClient from the URL and sets HTTP authorization header according to Basic Authentication protocol to be sent with all requests made with this FlurlClient. /// /// Username of authenticating user. /// Password of authenticating user. /// The modified FlurlClient. public static FlurlClient WithBasicAuth(this string url, string username, string password) { return new FlurlClient(url, true).WithBasicAuth(username, password); } /// /// Sets HTTP authorization header with acquired bearer token according to OAuth 2.0 specification to be sent with all requests made with this FlurlClient. /// /// The acquired bearer token to pass. /// The modified FlurlClient. public static FlurlClient WithOAuthBearerToken(this FlurlClient client, string token) { client.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); return client; } /// /// Creates a FlurlClient from the URL and sets HTTP authorization header with acquired bearer token according to OAuth 2.0 specification to be sent with all requests made with this FlurlClient. /// /// The acquired bearer token to pass. /// The modified FlurlClient. public static FlurlClient WithOAuthBearerToken(this Url url, string token) { return new FlurlClient(url, true).WithOAuthBearerToken(token); } /// /// Creates a FlurlClient from the URL and sets HTTP authorization header with acquired bearer token according to OAuth 2.0 specification to be sent with all requests made with this FlurlClient. /// /// The acquired bearer token to pass. /// The modified FlurlClient. public static FlurlClient WithOAuthBearerToken(this string url, string token) { return new FlurlClient(url, true).WithOAuthBearerToken(token); } } }