Skip to content

Commit

Permalink
Added ability to pass custom IFlurlClientFactory instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Janek91 committed Aug 30, 2024
1 parent c3b9bb8 commit 99d38c1
Show file tree
Hide file tree
Showing 20 changed files with 34 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/Flurl.CodeGen/CodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public CodeWriter(string filePath)
}

/// <summary>
/// use @0, @1, @2, etc for tokens. ({0} would be a pain because you'd alway need to escape "{" and "}")
/// use @0, @1, @2, etc. for tokens. ({0} would be a pain because you'd always need to escape "{" and "}")
/// </summary>
public CodeWriter WriteLine(string line, params object[] args)
{
Expand All @@ -30,7 +30,7 @@ public CodeWriter WriteLine(string line, params object[] args)
line = line.Replace("@" + i, val);
}

if (line == "}" || line == "{")
if (line is "}" or "{")
{
_indent--;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Flurl.CodeGen/HttpExtensionMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public HttpExtensionMethod(string verb, bool isGeneric, string reqBodyType, stri
" an asynchronous ",
(HttpVerb == null) ? "request." : $"{HttpVerb.ToUpperInvariant()} request.");

public bool HasRequestBody => (HttpVerb == "Post" || HttpVerb == "Put" || HttpVerb == "Patch" || HttpVerb == null);
public bool HasRequestBody => HttpVerb is "Post" or "Put" or "Patch" or null;

public string HttpVerb { get; }
public string RequestBodyType { get; }
Expand Down
4 changes: 1 addition & 3 deletions src/Flurl.CodeGen/Metadata.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Flurl.CodeGen
{
Expand Down
3 changes: 1 addition & 2 deletions src/Flurl.Http.Newtonsoft/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Dynamic;
using System.Threading.Tasks;
using Flurl.Http.Configuration;
Expand Down
16 changes: 13 additions & 3 deletions src/Flurl.Http/Configuration/FlurlClientBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Runtime.Versioning;
using Flurl.Util;
#if NET
using System.Runtime.Versioning;
#endif

namespace Flurl.Http.Configuration
{
Expand Down Expand Up @@ -42,6 +44,11 @@ public interface IFlurlClientBuilder : ISettingsContainer, IHeadersContainer, IE
/// Builds an instance of IFlurlClient based on configurations specified.
/// </summary>
IFlurlClient Build();

/// <summary>
/// Configure factory
/// </summary>
void WithFactory(Func<IFlurlClientFactory> create);
}

/// <summary>
Expand Down Expand Up @@ -72,6 +79,9 @@ public FlurlClientBuilder(string baseUrl = null) {
_baseUrl = baseUrl;
}

/// <inheritdoc />
public void WithFactory(Func<IFlurlClientFactory> create) => _factory = create();

/// <inheritdoc />
public IFlurlClientBuilder AddMiddleware(Func<DelegatingHandler> create) {
_addMiddleware.Add(create);
Expand Down Expand Up @@ -103,7 +113,7 @@ public IFlurlClientBuilder UseSocketsHttpHandler(Action<SocketsHttpHandler> conf
if (_factory is DefaultFlurlClientFactory && _handlerConfigs.Any())
throw new FlurlConfigurationException("ConfigureInnerHandler and UseSocketsHttpHandler cannot be used together. The former configures and instance of HttpClientHandler and would be ignored when switching to SocketsHttpHandler.");

if (!(_factory is SocketsHandlerFlurlClientFactory))
if (_factory is not SocketsHandlerFlurlClientFactory)
_factory = new SocketsHandlerFlurlClientFactory();

_handlerConfigs.Add(h => configure(h as SocketsHttpHandler));
Expand Down
5 changes: 1 addition & 4 deletions src/Flurl.Http/Configuration/ISerializer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.IO;

namespace Flurl.Http.Configuration
{
Expand Down
9 changes: 1 addition & 8 deletions src/Flurl.Http/Configuration/RedirectSettings.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Flurl.Http.Configuration
namespace Flurl.Http.Configuration
{
/// <summary>
/// A set of properties that affect Flurl.Http behavior specific to auto-redirecting.
Expand Down
4 changes: 1 addition & 3 deletions src/Flurl.Http/Content/CapturedJsonContent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text;

namespace Flurl.Http.Content
namespace Flurl.Http.Content
{
/// <summary>
/// Provides HTTP content based on a serialized JSON object, with the JSON string captured to a property
Expand Down
2 changes: 0 additions & 2 deletions src/Flurl.Http/Content/CapturedStringContent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

namespace Flurl.Http.Content
{
Expand Down
2 changes: 1 addition & 1 deletion src/Flurl.Http/FlurlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private async Task<IFlurlResponse> ProcessRedirectAsync(FlurlCall call, HttpComp

// partially lifted from https://github.com/dotnet/runtime/blob/master/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RedirectHandler.cs
private static FlurlRedirect GetRedirect(FlurlCall call) {
if (call.Response.StatusCode < 300 || call.Response.StatusCode > 399)
if (call.Response.StatusCode is < 300 or > 399)
return null;

if (!call.Response.Headers.TryGetFirst("Location", out var location))
Expand Down
1 change: 0 additions & 1 deletion src/Flurl.Http/FlurlEventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Flurl.Http
Expand Down
3 changes: 1 addition & 2 deletions src/Flurl.Http/FlurlRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -184,7 +183,7 @@ private void ApplyCookieJar(CookieJar jar) {

this.WithCookies(
from c in CookieJar
where c.ShouldSendTo(this.Url, out _)
where c.ShouldSendTo(Url, out _)
// sort by longest path, then earliest creation time, per #2: https://tools.ietf.org/html/rfc6265#section-5.4
orderby (c.Path ?? c.OriginUrl.Path).Length descending, c.DateReceived
select (c.Name, c.Value));
Expand Down
1 change: 0 additions & 1 deletion src/Flurl.Http/GeneratedExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// This file was auto-generated by Flurl.CodeGen. Do not edit directly.
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand Down
3 changes: 1 addition & 2 deletions src/Flurl.Http/HttpMessageExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Net.Http;
using System.Net.Http;
using System.Net.Http.Headers;
using Flurl.Http.Content;
using Flurl.Util;
Expand Down
2 changes: 1 addition & 1 deletion src/Flurl.Http/IHeadersContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static T WithHeaders<T>(this T obj, object headers, bool replaceUnderscor
return obj;

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

foreach (var kv in headers.ToKeyValuePairs()) {
var key = replaceUnderscoreWithHyphen ? kv.Key.Replace("_", "-") : kv.Key;
Expand Down
2 changes: 0 additions & 2 deletions src/Flurl.Http/ISettingsContainer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Linq;
using System.Net;
using Flurl.Http.Configuration;

namespace Flurl.Http
Expand Down
6 changes: 2 additions & 4 deletions src/Flurl.Http/Testing/Util.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -39,7 +37,7 @@ internal static bool HasQueryParam(this FlurlCall call, string name, object valu

if (!paramVals.Any())
return false;
if (!(value is string) && value is IEnumerable en) {
if (value is not string && value is IEnumerable en) {
var values = en.Cast<object>().Select(o => o.ToInvariantString()).ToList();
return values.Intersect(paramVals).Count() == values.Count;
}
Expand Down
6 changes: 1 addition & 5 deletions src/Flurl/NullValueHandling.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Flurl
namespace Flurl
{
/// <summary>
/// Describes how to handle null values in query parameters.
Expand Down
4 changes: 2 additions & 2 deletions src/Flurl/Url.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,12 +633,12 @@ public string ToString(bool encodeSpaceAsPlus) {
/// </summary>
/// <param name="obj">The object to compare to this instance.</param>
/// <returns></returns>
public override bool Equals(object obj) => obj is Url url && this.ToString().OrdinalEquals(url.ToString());
public override bool Equals(object obj) => obj is Url url && ToString().OrdinalEquals(url.ToString());

/// <summary>
/// Returns the hashcode for this Url.
/// </summary>
public override int GetHashCode() => this.ToString().GetHashCode();
public override int GetHashCode() => ToString().GetHashCode();
#endregion

#region static utility methods
Expand Down
9 changes: 4 additions & 5 deletions src/Flurl/Util/NameValueList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;

namespace Flurl.Util
Expand Down Expand Up @@ -96,11 +95,11 @@ public NameValueList(IEnumerable<(string Name, TValue Value)> items, bool caseSe
public void AddOrReplace(string name, TValue value) {
var i = 0;
var replaced = false;
while (i < this.Count) {
while (i < Count) {
if (!this[i].Name.OrdinalEquals(name, !_caseSensitiveNames))
i++;
else if (replaced)
this.RemoveAt(i);
RemoveAt(i);
else {
this[i] = (name, value);
replaced = true;
Expand All @@ -109,7 +108,7 @@ public void AddOrReplace(string name, TValue value) {
}

if (!replaced)
this.Add(name, value);
Add(name, value);
}

/// <inheritdoc />
Expand Down

0 comments on commit 99d38c1

Please sign in to comment.