Skip to content
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

feat: refactors, cache attributes, use helper methods #1739

Merged
merged 1 commit into from
Jun 26, 2024
Merged
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
41 changes: 20 additions & 21 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class RequestBuilderImplementation<TApi>(RefitSettings? refitSettings = null)

partial class RequestBuilderImplementation : IRequestBuilder
{
static readonly HashSet<HttpMethod> BodylessMethods = [HttpMethod.Get, HttpMethod.Head];
static readonly QueryAttribute DefaultQueryAttribute = new ();
static readonly Uri BaseUri = new Uri("http://api");
readonly Dictionary<string, List<RestMethodInfoInternal>> interfaceHttpMethods;
readonly ConcurrentDictionary<
CloseGenericMethodKey,
Expand Down Expand Up @@ -803,13 +804,12 @@ await content
//if header collection, add to request headers
if (restMethod.HeaderCollectionParameterMap.Contains(i))
{
var headerCollection =
param as IDictionary<string, string>
?? new Dictionary<string, string>();

foreach (var header in headerCollection)
if (param is IDictionary<string, string> headerCollection)
{
headersToAdd[header.Key] = header.Value;
foreach (var header in headerCollection)
{
headersToAdd[header.Key] = header.Value;
}
}

isParameterMappedToRequest = true;
Expand Down Expand Up @@ -850,7 +850,7 @@ param as IDictionary<string, string>
|| queryAttribute != null
)
{
var attr = queryAttribute ?? new QueryAttribute();
var attr = queryAttribute ?? DefaultQueryAttribute;
if (DoNotConvertToQueryMap(param))
{
queryParamsToAdd.AddRange(
Expand Down Expand Up @@ -924,7 +924,7 @@ param as IDictionary<string, string>
// 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 && !BodylessMethods.Contains(ret.Method))
if (ret.Content == null && !IsBodyless(ret.Method))
ret.Content = new ByteArrayContent(Array.Empty<byte>());

foreach (var header in headersToAdd)
Expand Down Expand Up @@ -979,7 +979,7 @@ param as IDictionary<string, string>
// 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.
var uri = new UriBuilder(new Uri(new Uri("http://api"), urlTarget));
var uri = new UriBuilder(new Uri(BaseUri, urlTarget));
ParseExistingQueryString(uri, queryParamsToAdd);

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

var uriFormat =
restMethod.MethodInfo.GetCustomAttribute<QueryUriFormatAttribute>()?.UriFormat
?? UriFormat.UriEscaped;
ret.RequestUri = new Uri(
uri.Uri.GetComponents(UriComponents.PathAndQuery, uriFormat),
uri.Uri.GetComponents(UriComponents.PathAndQuery, restMethod.QueryUriFormat),
UriKind.Relative
);
return ret;
Expand Down Expand Up @@ -1064,13 +1061,13 @@ var value in ParseEnumerableQueryParameterValue(

default:
var delimiter =
collectionFormat == CollectionFormat.Ssv
? " "
: collectionFormat == CollectionFormat.Tsv
? "\t"
: collectionFormat == CollectionFormat.Pipes
? "|"
: ",";
collectionFormat switch
{
CollectionFormat.Ssv => " ",
CollectionFormat.Tsv => "\t",
CollectionFormat.Pipes => "|",
_ => ","
};

// Missing a "default" clause was preventing the collection from serializing at all, as it was hitting "continue" thus causing an off-by-one error
var formattedValues = paramValues
Expand Down Expand Up @@ -1304,5 +1301,7 @@ static void SetHeader(HttpRequestMessage request, string name, string? value)
request.Content.Headers.TryAddWithoutValidation(name, value);
}
}

static bool IsBodyless(HttpMethod method) => method == HttpMethod.Get || method == HttpMethod.Head;
}
}
4 changes: 4 additions & 0 deletions Refit/RestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ internal class RestMethodInfoInternal
public bool IsMultipart { get; private set; }
public string MultipartBoundary { get; private set; }
public ParameterInfo? CancellationToken { get; set; }
public UriFormat QueryUriFormat { get; set; }
public Dictionary<string, string?> Headers { get; set; }
public Dictionary<int, string> HeaderParameterMap { get; set; }
public ISet<int> HeaderCollectionParameterMap { get; set; }
Expand Down Expand Up @@ -161,6 +162,9 @@ public RestMethodInfoInternal(

CancellationToken = ctParam;

QueryUriFormat = methodInfo.GetCustomAttribute<QueryUriFormatAttribute>()?.UriFormat
?? UriFormat.UriEscaped;

IsApiResponse =
ReturnResultType!.GetTypeInfo().IsGenericType
&& (
Expand Down
Loading