Skip to content

Commit

Permalink
chore: code cleanup, reduce nestings, use TryGetValue, remove unnee…
Browse files Browse the repository at this point in the history
…ded `ToArray` call
  • Loading branch information
TimothyMakkison committed Nov 13, 2023
1 parent e726d19 commit d583996
Show file tree
Hide file tree
Showing 29 changed files with 133 additions and 227 deletions.
2 changes: 1 addition & 1 deletion Refit.Tests/MethodOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface IUseOverloadedGenericMethods<TResponse, in TParam, in THeader>
}


public class MethodOverladTests
public class MethodOverloadTests
{
[Fact]
public async Task BasicMethodOverloadTest()
Expand Down
4 changes: 1 addition & 3 deletions Refit/AnonymousDisposable.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Refit
namespace Refit
{
sealed class AnonymousDisposable : IDisposable
{
Expand Down
10 changes: 4 additions & 6 deletions Refit/ApiException.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Net;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace Refit
{
Expand Down Expand Up @@ -134,15 +132,15 @@ public static async Task<ApiException> Create(string exceptionMessage, HttpReque

if (response.Content.Headers?.ContentType?.MediaType?.Equals("application/problem+json") ?? false)
{
exception = ValidationApiException.Create(exception);
exception = ValidationApiException.Create(exception);
}

response.Content.Dispose();
}
catch
{
// NB: We're already handling an exception at this point,
// so we want to make sure we don't throw another one
// NB: We're already handling an exception at this point,
// so we want to make sure we don't throw another one
// that hides the real error.
}

Expand Down
4 changes: 1 addition & 3 deletions Refit/ApiResponse.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace Refit
{
Expand Down
3 changes: 1 addition & 2 deletions Refit/Attributes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Net.Http;
using System.Net.Http;

namespace Refit
{
Expand Down
5 changes: 1 addition & 4 deletions Refit/AuthenticatedHttpClientHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Net.Http;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;

namespace Refit
{
Expand Down
6 changes: 1 addition & 5 deletions Refit/Buffers/PooledBufferWriter.Stream.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Buffers;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Buffers;

#nullable enable

Expand Down
3 changes: 1 addition & 2 deletions Refit/Buffers/PooledBufferWriter.ThrowExceptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;

namespace Refit.Buffers
{
Expand Down
4 changes: 1 addition & 3 deletions Refit/Buffers/PooledBufferWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Buffers;
using System.IO;
using System.Buffers;
using System.Runtime.CompilerServices;

namespace Refit.Buffers
Expand Down
8 changes: 3 additions & 5 deletions Refit/CachedRequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Collections.Concurrent;
using System.Net.Http;

namespace Refit
Expand Down Expand Up @@ -40,7 +38,7 @@ static string GetCacheKey(string methodName, Type[] parameterTypes, Type[] gener

static string GetArgumentString(Type[] parameterTypes)
{
if (parameterTypes == null || parameterTypes.Length == 0)
if (parameterTypes.Length == 0)
{
return "";
}
Expand All @@ -50,7 +48,7 @@ static string GetArgumentString(Type[] parameterTypes)

static string GetGenericString(Type[] genericArgumentTypes)
{
if (genericArgumentTypes == null || genericArgumentTypes.Length == 0)
if (genericArgumentTypes.Length == 0)
{
return "";
}
Expand Down
4 changes: 1 addition & 3 deletions Refit/CloseGenericMethodKey.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Linq;
using System.Reflection;
using System.Reflection;

namespace Refit
{
Expand Down
96 changes: 46 additions & 50 deletions Refit/FormValueMultimap.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Reflection;
using System.Text.Json.Serialization;

namespace Refit
{
Expand Down Expand Up @@ -47,62 +43,62 @@ public FormValueMultimap(object source, RefitSettings settings)

lock (PropertyCache)
{
if (!PropertyCache.ContainsKey(type))
if (!PropertyCache.TryGetValue(type, out var properties))
{
PropertyCache[type] = GetProperties(type);
properties = GetProperties(type);
PropertyCache[type] = properties;
}

foreach (var property in PropertyCache[type])
foreach (var property in properties)
{
var value = property.GetValue(source, null);
if (value != null)
{
var fieldName = GetFieldNameForProperty(property);
if (value == null) continue;

var fieldName = GetFieldNameForProperty(property);

// see if there's a query attribute
var attrib = property.GetCustomAttribute<QueryAttribute>(true);
// see if there's a query attribute
var attrib = property.GetCustomAttribute<QueryAttribute>(true);

if (value is not IEnumerable enumerable)
{
Add(fieldName, settings.FormUrlEncodedParameterFormatter.Format(value, attrib?.Format));
continue;
}

if (value is IEnumerable enumerable)
{
var collectionFormat = attrib != null && attrib.IsCollectionFormatSpecified
? attrib.CollectionFormat
: settings.CollectionFormat;
var collectionFormat = attrib is { IsCollectionFormatSpecified: true }
? attrib.CollectionFormat
: settings.CollectionFormat;

switch (collectionFormat)
switch (collectionFormat)
{
case CollectionFormat.Multi:
foreach (var item in enumerable)
{
case CollectionFormat.Multi:
foreach (var item in enumerable)
{
Add(fieldName, settings.FormUrlEncodedParameterFormatter.Format(item, attrib?.Format));
}
break;
case CollectionFormat.Csv:
case CollectionFormat.Ssv:
case CollectionFormat.Tsv:
case CollectionFormat.Pipes:
var delimiter = collectionFormat switch
{
CollectionFormat.Csv => ",",
CollectionFormat.Ssv => " ",
CollectionFormat.Tsv => "\t",
_ => "|"
};

var formattedValues = enumerable
.Cast<object>()
.Select(v => settings.FormUrlEncodedParameterFormatter.Format(v, attrib?.Format));
Add(fieldName, string.Join(delimiter, formattedValues));
break;
default:
Add(fieldName, settings.FormUrlEncodedParameterFormatter.Format(value, attrib?.Format));
break;
Add(fieldName,
settings.FormUrlEncodedParameterFormatter.Format(item, attrib?.Format));
}
}
else
{
Add(fieldName, settings.FormUrlEncodedParameterFormatter.Format(value, attrib?.Format));
}

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

var formattedValues = enumerable
.Cast<object>()
.Select(v => settings.FormUrlEncodedParameterFormatter.Format(v, attrib?.Format));
Add(fieldName, string.Join(delimiter, formattedValues));
break;
default:
Add(fieldName, settings.FormUrlEncodedParameterFormatter.Format(value, attrib?.Format));
break;
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions Refit/HttpContentExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Http;

namespace Refit
{
Expand Down
7 changes: 1 addition & 6 deletions Refit/JsonContentSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Refit
{
Expand Down
4 changes: 1 addition & 3 deletions Refit/MultipartItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http;
using System.Net.Http.Headers;

namespace Refit
Expand Down
5 changes: 1 addition & 4 deletions Refit/NameValueCollection.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.Linq;

namespace System.Collections.Specialized
namespace System.Collections.Specialized
{
class NameValueCollection : Dictionary<string, string>
{
Expand Down
4 changes: 1 addition & 3 deletions Refit/ProblemDetails.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;

namespace Refit
{
Expand Down
3 changes: 0 additions & 3 deletions Refit/PushStreamContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@

using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.IO;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;

namespace System.Net.Http
{
Expand Down
9 changes: 2 additions & 7 deletions Refit/RefitSettings.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;

namespace Refit
{
Expand Down Expand Up @@ -86,7 +81,7 @@ public RefitSettings(
public bool Buffered { get; set; } = false;

/// <summary>
/// Optional Key-Value pairs, which are displayed in the property <see cref="HttpRequestMessage.Options"/> or <see cref="HttpRequestMessage.Properties"/>.
/// Optional Key-Value pairs, which are displayed in the property <see cref="HttpRequestMessage.Options"/> or <see cref="HttpRequestMessage.Properties"/>.
/// </summary>
public Dictionary<string, object> HttpRequestMessageOptions { get; set; }
}
Expand Down
3 changes: 1 addition & 2 deletions Refit/RequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Net.Http;
using System.Net.Http;

namespace Refit
{
Expand Down
4 changes: 1 addition & 3 deletions Refit/RequestBuilderFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Refit
namespace Refit
{
interface IRequestBuilderFactory
{
Expand Down
6 changes: 1 addition & 5 deletions Refit/RequestBuilderImplementation.TaskToObservable.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Refit
namespace Refit
{
partial class RequestBuilderImplementation
{
Expand Down
Loading

0 comments on commit d583996

Please sign in to comment.