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

feat: WIP optimize url creation #1730

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions Refit.Tests/API/ApiApprovalTests.Refit.DotNet6_0.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ namespace Refit
public System.Net.Http.HttpContent ToHttpContent<T>(T item) { }
public static System.Text.Json.JsonSerializerOptions GetDefaultJsonSerializerOptions() { }
}
public class UnreachableException : System.Exception
{
public UnreachableException() { }
public UnreachableException(string message) { }
}
[System.Serializable]
public class ValidationApiException : Refit.ApiException
{
Expand Down
5 changes: 5 additions & 0 deletions Refit.Tests/API/ApiApprovalTests.Refit.DotNet8_0.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ namespace Refit
public System.Net.Http.HttpContent ToHttpContent<T>(T item) { }
public static System.Text.Json.JsonSerializerOptions GetDefaultJsonSerializerOptions() { }
}
public class UnreachableException : System.Exception
{
public UnreachableException() { }
public UnreachableException(string message) { }
}
[System.Serializable]
public class ValidationApiException : Refit.ApiException
{
Expand Down
159 changes: 94 additions & 65 deletions Refit/RequestBuilderImplementation.cs 8000
6D40
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Net.Http;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -630,8 +631,6 @@
ret.Content = multiPartContent;
}

var urlTarget =
(basePath == "/" ? string.Empty : basePath) + restMethod.RelativePath;
var queryParamsToAdd = new List<KeyValuePair<string, string?>>();
var headersToAdd = restMethod.Headers.Count > 0 ?
new Dictionary<string, string?>(restMethod.Headers)
Expand All @@ -647,69 +646,10 @@
if (restMethod.ParameterMap.TryGetValue(i, out var parameterMapValue))
{
parameterInfo = parameterMapValue;
if (parameterInfo.IsObjectPropertyParameter)
if (!parameterInfo.IsObjectPropertyParameter)
{
foreach (var propertyInfo in parameterInfo.ParameterProperties)
{
var propertyObject = propertyInfo.PropertyInfo.GetValue(param);
urlTarget = Regex.Replace(
urlTarget,
"{" + propertyInfo.Name + "}",
Uri.EscapeDataString(
settings.UrlParameterFormatter.Format(
propertyObject,
propertyInfo.PropertyInfo,
propertyInfo.PropertyInfo.PropertyType
) ?? string.Empty
),
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
);
}
//don't continue here as we want it to fall through so any parameters on this object not bound here get passed as query parameters
}
else
{
string pattern;
string replacement;
if (parameterMapValue.Type == ParameterType.RoundTripping)
{
pattern = $@"{{\*\*{parameterMapValue.Name}}}";
var paramValue = (string)param;
replacement = string.Join(
"/",
paramValue
.Split('/')
.Select(
s =>
Uri.EscapeDataString(
settings.UrlParameterFormatter.Format(
s,
restMethod.ParameterInfoArray[i],
restMethod.ParameterInfoArray[i].ParameterType
) ?? string.Empty
)
)
);
}
else
{
pattern = "{" + parameterMapValue.Name + "}";
replacement = Uri.EscapeDataString(
settings.UrlParameterFormatter.Format(
param,
restMethod.ParameterInfoArray[i],
restMethod.ParameterInfoArray[i].ParameterType
) ?? string.Empty
);
}

urlTarget = Regex.Replace(
urlTarget,
pattern,
replacement,
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
);

// mark parameter mapped if not an object
// we want objects to fall through so any parameters on this object not bound here get passed as query parameters
isParameterMappedToRequest = true;
}
}
Expand All @@ -721,7 +661,6 @@
)
{
AddBodyToRequest(restMethod, param, ret);

isParameterMappedToRequest = true;
}

Expand Down Expand Up @@ -795,6 +734,8 @@

AddPropertiesToRequest(restMethod, ret, paramList);

var urlTarget = BuildRelativePath(basePath, restMethod, paramList);

// NB: The URI methods in .NET are dumb. Also, we do this
// UriBuilder business so that we preserve any hardcoded query
// parameters as well as add the parameterized ones.
Expand All @@ -818,6 +759,94 @@
};
}

string BuildRelativePath(string basePath, RestMethodInfoInternal restMethod, object[] paramList)
{
basePath = basePath == "/" ? string.Empty : basePath;
var pathFragments = restMethod.FragmentPath;
if (pathFragments.Count == 0)
{
return basePath;
}
if (string.IsNullOrEmpty(basePath) && pathFragments.Count == 1)
{
return GetPathFragmentValue(restMethod, paramList, pathFragments[0]);
}

#pragma warning disable CA2000
var vsb = new ValueStringBuilder(stackalloc char[512]);
#pragma warning restore CA2000
vsb.Append(basePath);

foreach (var fragment in pathFragments)
{
vsb.Append(GetPathFragmentValue(restMethod, paramList, fragment));
}

return vsb.ToString();
}

string GetPathFragmentValue(RestMethodInfoInternal restMethod, object[] paramList,
ParameterFragment fragment)
{
if (fragment.IsConstant)
{
return fragment.Value!;
}

var contains = restMethod.ParameterMap.TryGetValue(fragment.ArgumentIndex, out var parameterMapValue);
if (!contains || parameterMapValue is null)
throw new UnreachableException($"{restMethod.ParameterMap} should contain parameter.");

Check warning on line 798 in Refit/RequestBuilderImplementation.cs

View check run for this annotation

Codecov / codecov/patch

Refit/RequestBuilderImplementation.cs#L798

Added line #L798 was not covered by tests

if (fragment.IsObjectProperty)
{
var param = paramList[fragment.ArgumentIndex];
var property = parameterMapValue.ParameterProperties[fragment.PropertyIndex];
var propertyObject = property.PropertyInfo.GetValue(param);

return Uri.EscapeDataString(settings.UrlParameterFormatter.Format(
propertyObject,
property.PropertyInfo,
property.PropertyInfo.PropertyType
) ?? string.Empty);
}

if (fragment.IsDynamicRoute)
{
var param = paramList[fragment.ArgumentIndex];

if (parameterMapValue.Type != ParameterType.RoundTripping)
{
return Uri.EscapeDataString(
settings.UrlParameterFormatter.Format(
param,
restMethod.ParameterInfoArray[fragment.ArgumentIndex],
restMethod.ParameterInfoArray[fragment.ArgumentIndex].ParameterType
) ?? string.Empty
);
}

var paramValue = (string)param;
return string.Join(
"/",
paramValue
.Split('/')
.Select(
s =>
Uri.EscapeDataString(
settings.UrlParameterFormatter.Format(
s,
restMethod.ParameterInfoArray[fragment.ArgumentIndex],
restMethod.ParameterInfoArray[fragment.ArgumentIndex].ParameterType
) ?? string.Empty
)
)
);

}

throw new UnreachableException($"{nameof(ParameterFragment)} is in an invalid form.");

Check warning on line 847 in Refit/RequestBuilderImplementation.cs

View check run for this annotation

Codecov / codecov/patch

Refit/RequestBuilderImplementation.cs#L847

Added line #L847 was not covered by tests
}

void AddBodyToRequest(RestMethodInfoInternal restMethod, object param, HttpRequestMessage ret)
{
if (param is HttpContent httpContentParam)
Expand Down
Loading
Loading
0