8000 feat: defer header dictionary creation by TimothyMakkison · Pull Request #1745 · reactiveui/refit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: defer header dictionary creation #1745

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

Merged
merged 3 commits into from
Jun 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
8000 Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,9 @@ bool paramsContainsCancellationToken
var urlTarget =
(basePath == "/" ? string.Empty : basePath) + restMethod.RelativePath;
var queryParamsToAdd = new List<KeyValuePair<string, string?>>();
var headersToAdd = new Dictionary<string, string?>(restMethod.Headers);
var headersToAdd = restMethod.Headers.Count > 0 ?
new Dictionary<string, string?>(restMethod.Headers)
: null;

RestMethodParameterInfo? parameterInfo = null;

Expand Down Expand Up @@ -726,6 +728,7 @@ bool paramsContainsCancellationToken
// if header, add to request headers
if (restMethod.HeaderParameterMap.TryGetValue(i, out var headerParameterValue))
{
headersToAdd ??= new Dictionary<string, string?>();
headersToAdd[headerParameterValue] = param?.ToString();
isParameterMappedToRequest = true;
}
Expand All @@ -737,6 +740,7 @@ bool paramsContainsCancellationToken
{
foreach (var header in headerCollection)
{
headersToAdd ??= new Dictionary<string, string?>();
headersToAdd[header.Key] = header.Value;
}
}
Expand All @@ -750,6 +754,7 @@ bool paramsContainsCancellationToken
&& restMethod.AuthorizeParameterInfo.Item2 == i
)
{
headersToAdd ??= new Dictionary<string, string?>();
headersToAdd["Authorization"] =
$"{restMethod.AuthorizeParameterInfo.Item1} {param}";
isParameterMappedToRequest = true;
Expand Down Expand Up @@ -952,22 +957,22 @@ void AddMultiPart(RestMethodInfoInternal restMethod, int i, object param,
}
}

static void AddHeadersToRequest(Dictionary<string, string?> headersToAdd, HttpRequestMessage ret)
static void AddHeadersToRequest(Dictionary<string, string?>? headersToAdd, HttpRequestMessage ret)
{
// NB: We defer setting headers until the body has been
// added so any custom content headers don't get left out.
if (headersToAdd.Count > 0)
{
// We could have content headers, so we need to make
// sure we have an HttpContent object to add them to,
// provided the HttpClient will allow it for the method
if (ret.Content == null && !IsBodyless(ret.Method))
ret.Content = new ByteArrayContent(Array.Empty<byte>());
if (headersToAdd is null || headersToAdd.Count <= 0)
return;

foreach (var header in headersToAdd)
{
SetHeader(ret, header.Key, header.Value);
}
// We could have content headers, so we need to make
// sure we have an HttpContent object to add them to,
// provided the HttpClient will allow it for the method
if (ret.Content == null && !IsBodyless(ret.Method))
ret.Content = new ByteArrayContent(Array.Empty<byte>());

foreach (var header in headersToAdd)
{
SetHeader(ret, header.Key, header.Value);
}
}

Expand Down
Loading
0