Skip to content

Commit 12640cb

Browse files
feat: refactors, cache attributes, use helper methods (#1739)
1 parent 151b1d9 commit 12640cb

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

Refit/RequestBuilderImplementation.cs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class RequestBuilderImplementation<TApi>(RefitSettings? refitSettings = null)
1414

1515
partial class RequestBuilderImplementation : IRequestBuilder
1616
{
17-
static readonly HashSet<HttpMethod> BodylessMethods = [HttpMethod.Get, HttpMethod.Head];
17+
static readonly QueryAttribute DefaultQueryAttribute = new ();
18+
static readonly Uri BaseUri = new Uri("http://api");
1819
readonly Dictionary<string, List<RestMethodInfoInternal>> interfaceHttpMethods;
1920
readonly ConcurrentDictionary<
2021
CloseGenericMethodKey,
@@ -803,13 +804,12 @@ await content
803804
//if header collection, add to request headers
804805
if (restMethod.HeaderCollectionParameterMap.Contains(i))
805806
{
806-
var headerCollection =
807-
param as IDictionary<string, string>
808-
?? new Dictionary<string, string>();
809-
810-
foreach (var header in headerCollection)
807+
if (param is IDictionary<string, string> headerCollection)
811808
{
812-
headersToAdd[header.Key] = header.Value;
809+
foreach (var header in headerCollection)
810+
{
811+
headersToAdd[header.Key] = header.Value;
812+
}
813813
}
814814

815815
isParameterMappedToRequest = true;
@@ -850,7 +850,7 @@ param as IDictionary<string, string>
850850
|| queryAttribute != null
851851
)
852852
{
853-
var attr = queryAttribute ?? new QueryAttribute();
853+
var attr = queryAttribute ?? DefaultQueryAttribute;
854854
if (DoNotConvertToQueryMap(param))
855855
{
856856
queryParamsToAdd.AddRange(
@@ -924,7 +924,7 @@ param as IDictionary<string, string>
924924
// We could have content headers, so we need to make
925925
// sure we have an HttpContent object to add them to,
926926
// provided the HttpClient will allow it for the method
927-
if (ret.Content == null && !BodylessMethods.Contains(ret.Method))
927+
if (ret.Content == null && !IsBodyless(ret.Method))
928928
ret.Content = new ByteArrayContent(Array.Empty<byte>());
929929

930930
foreach (var header in headersToAdd)
@@ -979,7 +979,7 @@ param as IDictionary<string, string>
979979
// NB: The URI methods in .NET are dumb. Also, we do this
980980
// UriBuilder business so that we preserve any hardcoded query
981981
// parameters as well as add the parameterized ones.
982-
var uri = new UriBuilder(new Uri(new Uri("http://api"), urlTarget));
982+
var uri = new UriBuilder(new Uri(BaseUri, urlTarget));
983983
ParseExistingQueryString(uri, queryParamsToAdd);
984984

985985
if (queryParamsToAdd.Count != 0)
@@ -991,11 +991,8 @@ param as IDictionary<string, string>
991991
uri.Query = null;
992992
}
993993

994-
var uriFormat =
995-
restMethod.MethodInfo.GetCustomAttribute<QueryUriFormatAttribute>()?.UriFormat
996-
?? UriFormat.UriEscaped;
997994
ret.RequestUri = new Uri(
998-
uri.Uri.GetComponents(UriComponents.PathAndQuery, uriFormat),
995+
uri.Uri.GetComponents(UriComponents.PathAndQuery, restMethod.QueryUriFormat),
999996
UriKind.Relative
1000997
);
1001998
return ret;
@@ -1064,13 +1061,13 @@ var value in ParseEnumerableQueryParameterValue(
10641061

10651062
default:
10661063
var delimiter =
1067-
collectionFormat == CollectionFormat.Ssv
1068-
? " "
1069-
: collectionFormat == CollectionFormat.Tsv
1070-
? "\t"
1071-
: collectionFormat == CollectionFormat.Pipes
1072-
? "|"
1073-
: ",";
1064+
collectionFormat switch
1065+
{
1066+
CollectionFormat.Ssv => " ",
1067+
CollectionFormat.Tsv => "\t",
1068+
CollectionFormat.Pipes => "|",
1069+
_ => ","
1070+
};
10741071

10751072
// Missing a "default" clause was preventing the collection from serializing at all, as it was hitting "continue" thus causing an off-by-one error
10761073
var formattedValues = paramValues
@@ -1304,5 +1301,7 @@ static void SetHeader(HttpRequestMessage request, string name, string? value)
13041301
request.Content.Headers.TryAddWithoutValidation(name, value);
13051302
}
13061303
}
1304+
1305+
static bool IsBodyless(HttpMethod method) => method == HttpMethod.Get || method == HttpMethod.Head;
13071306
}
13081307
}

Refit/RestMethodInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ internal class RestMethodInfoInternal
3535
public bool IsMultipart { get; private set; }
3636
public string MultipartBoundary { get; private set; }
3737
public ParameterInfo? CancellationToken { get; set; }
38+
public UriFormat QueryUriFormat { get; set; }
3839
public Dictionary<string, string?> Headers { get; set; }
3940
public Dictionary<int, string> HeaderParameterMap { get; set; }
4041
public ISet<int> HeaderCollectionParameterMap { get; set; }
@@ -161,6 +162,9 @@ public RestMethodInfoInternal(
161162

162163
CancellationToken = ctParam;
163164

165+
QueryUriFormat = methodInfo.GetCustomAttribute<QueryUriFormatAttribute>()?.UriFormat
166+
?? UriFormat.UriEscaped;
167+
164168
IsApiResponse =
165169
ReturnResultType!.GetTypeInfo().IsGenericType
166170
&& (

0 commit comments

Comments
 (0)