Skip to content

Commit 6985161

Browse files
feat: extract logic and small cleanups (#1755)
1 parent 3415304 commit 6985161

File tree

1 file changed

+72
-65
lines changed

1 file changed

+72
-65
lines changed

Refit/RequestBuilderImplementation.cs

Lines changed: 72 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -649,67 +649,12 @@ bool paramsContainsCancellationToken
649649
parameterInfo = parameterMapValue;
650650
if (parameterInfo.IsObjectPropertyParameter)
651651
{
652-
foreach (var propertyInfo in parameterInfo.ParameterProperties)
653-
{
654-
var propertyObject = propertyInfo.PropertyInfo.GetValue(param);
655-
urlTarget = Regex.Replace(
656-
urlTarget,
657-
"{" + propertyInfo.Name + "}",
658-
Uri.EscapeDataString(
659-
settings.UrlParameterFormatter.Format(
660-
propertyObject,
661-
propertyInfo.PropertyInfo,
662-
propertyInfo.PropertyInfo.PropertyType
663-
) ?? string.Empty
664-
),
665-
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
666-
);
667-
}
652+
urlTarget = AddObjectParametersToUrl(parameterInfo, param, urlTarget);
668653
//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
669654
}
670655
else
671656
{
672-
string pattern;
673-
string replacement;
674-
if (parameterMapValue.Type == ParameterType.RoundTripping)
675-
{
676-
pattern = $@"{{\*\*{parameterMapValue.Name}}}";
677-
var paramValue = (string)param;
678-
replacement = string.Join(
679-
"/",
680-
paramValue
681-
.Split('/')
682-
.Select(
683-
s =>
684-
Uri.EscapeDataString(
685-
settings.UrlParameterFormatter.Format(
686-
s,
687-
restMethod.ParameterInfoArray[i],
688-
restMethod.ParameterInfoArray[i].ParameterType
689-
) ?? string.Empty
690-
)
691-
)
692-
);
693-
}
694-
else
695-
{
696-
pattern = "{" + parameterMapValue.Name + "}";
697-
replacement = Uri.EscapeDataString(
698-
settings.UrlParameterFormatter.Format(
699-
param,
700-
restMethod.ParameterInfoArray[i],
701-
restMethod.ParameterInfoArray[i].ParameterType
702-
) ?? string.Empty
703-
);
704-
}
705-
706-
urlTarget = Regex.Replace(
707-
urlTarget,
708-
pattern,
709-
replacement,
710-
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
711-
);
712-
657+
urlTarget = AddValueParameterToUrl(restMethod, parameterMapValue, param, i, urlTarget);
713658
isParameterMappedToRequest = true;
714659
}
715660
}
@@ -721,7 +666,6 @@ bool paramsContainsCancellationToken
721666
)
722667
{
723668
AddBodyToRequest(restMethod, param, ret);
724-
725669
isParameterMappedToRequest = true;
726670
}
727671

@@ -784,7 +728,6 @@ bool paramsContainsCancellationToken
784728
)
785729
{
786730
AddQueryParameters(restMethod, queryAttribute, param, queryParamsToAdd, i, parameterInfo);
787-
788731
continue;
789732
}
790733

@@ -818,6 +761,74 @@ bool paramsContainsCancellationToken
818761
};
819762
}
820763

764+
string AddObjectParametersToUrl(RestMethodParameterInfo parameterInfo, object param, string urlTarget)
765+
{
766+
foreach (var propertyInfo in parameterInfo.ParameterProperties)
767+
{
768+
var propertyObject = propertyInfo.PropertyInfo.GetValue(param);
769+
urlTarget = Regex.Replace(
770+
urlTarget,
771+
"{" + propertyInfo.Name + "}",
772+
Uri.EscapeDataString(
773+
settings.UrlParameterFormatter.Format(
774+
propertyObject,
775+
propertyInfo.PropertyInfo,
776+
propertyInfo.PropertyInfo.PropertyType
777+
) ?? string.Empty
778+
),
779+
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
780+
);
781+
}
782+
783+
return urlTarget;
784+
}
785+
786+
string AddValueParameterToUrl(RestMethodInfoInternal restMethod, RestMethodParameterInfo parameterMapValue,
787+
object param, int i, string urlTarget)
788+
{
789+
string pattern;
790+
string replacement;
791+
if (parameterMapValue.Type == ParameterType.RoundTripping)
792+
{
793+
pattern = $@"{{\*\*{parameterMapValue.Name}}}";
794+
var paramValue = (string)param;
795+
replacement = string.Join(
796+
"/",
797+
paramValue
798+
.Split('/')
799+
.Select(
800+
s =>
801+
Uri.EscapeDataString(
802+
settings.UrlParameterFormatter.Format(
803+
s,
804+
restMethod.ParameterInfoArray[i],
805+
restMethod.ParameterInfoArray[i].ParameterType
806+
) ?? string.Empty
807+
)
808+
)
809+
);
810+
}
811+
else
812+
{
813+
pattern = "{" + parameterMapValue.Name + "}";
814+
replacement = Uri.EscapeDataString(
815+
settings.UrlParameterFormatter.Format(
816+
param,
817+
restMethod.ParameterInfoArray[i],
818+
restMethod.ParameterInfoArray[i].ParameterType
819+
) ?? string.Empty
820+
);
821+
}
822+
823+
urlTarget = Regex.Replace(
824+
urlTarget,
825+
pattern,
826+
replacement,
827+
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
828+
);
829+
return urlTarget;
830+
}
831+
821832
void AddBodyToRequest(RestMethodInfoInternal restMethod, object param, HttpRequestMessage ret)
822833
{
823834
if (param is HttpContent httpContentParam)
@@ -940,11 +951,7 @@ void AddMultiPart(RestMethodInfoInternal restMethod, int i, object param,
940951
}
941952

942953
// Check to see if it's an IEnumerable
943-
var itemValue = param;
944-
var enumerable = itemValue as IEnumerable<object>;
945-
var typeIsCollection = enumerable != null;
946-
947-
if (typeIsCollection)
954+
if (param is IEnumerable<object> enumerable)
948955
{
949956
foreach (var item in enumerable!)
950957
{
@@ -953,7 +960,7 @@ void AddMultiPart(RestMethodInfoInternal restMethod, int i, object param,
953960
}
954961
else
955962
{
956-
AddMultipartItem(multiPartContent!, itemName, parameterName, itemValue);
963+
AddMultipartItem(multiPartContent!, itemName, parameterName, param);
957964
}
958965
}
959966

0 commit comments

Comments
 (0)