Skip to content

Commit

Permalink
feat: extract logic and small cleanups (#1755)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison authored Jul 8, 2024
1 parent 3415304 commit 6985161
Showing 1 changed file with 72 additions and 65 deletions.
137 changes: 72 additions & 65 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -649,67 +649,12 @@ bool paramsContainsCancellationToken
parameterInfo = parameterMapValue;
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
);
}
urlTarget = AddObjectParametersToUrl(parameterInfo, param, urlTarget);
//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
);

urlTarget = AddValueParameterToUrl(restMethod, parameterMapValue, param, i, urlTarget);
isParameterMappedToRequest = true;
}
}
Expand All @@ -721,7 +666,6 @@ bool paramsContainsCancellationToken
)
{
AddBodyToRequest(restMethod, param, ret);

isParameterMappedToRequest = true;
}

Expand Down Expand Up @@ -784,7 +728,6 @@ bool paramsContainsCancellationToken
)
{
AddQueryParameters(restMethod, queryAttribute, param, queryParamsToAdd, i, parameterInfo);

continue;
}

Expand Down Expand Up @@ -818,6 +761,74 @@ bool paramsContainsCancellationToken
};
}

string AddObjectParametersToUrl(RestMethodParameterInfo parameterInfo, object param, string urlTarget)
{
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
);
}

return urlTarget;
}

string AddValueParameterToUrl(RestMethodInfoInternal restMethod, RestMethodParameterInfo parameterMapValue,
object param, int i, string urlTarget)
{
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
);
return urlTarget;
}

void AddBodyToRequest(RestMethodInfoInternal restMethod, object param, HttpRequestMessage ret)
{
if (param is HttpContent httpContentParam)
Expand Down Expand Up @@ -940,11 +951,7 @@ void AddMultiPart(RestMethodInfoInternal restMethod, int i, object param,
}

// Check to see if it's an IEnumerable
var itemValue = param;
var enumerable = itemValue as IEnumerable<object>;
var typeIsCollection = enumerable != null;

if (typeIsCollection)
if (param is IEnumerable<object> enumerable)
{
foreach (var item in enumerable!)
{
Expand All @@ -953,7 +960,7 @@ void AddMultiPart(RestMethodInfoInternal restMethod, int i, object param,
}
else
{
AddMultipartItem(multiPartContent!, itemName, parameterName, itemValue);
AddMultipartItem(multiPartContent!, itemName, parameterName, param);
}
}

Expand Down

0 comments on commit 6985161

Please sign in to comment.