Skip to content

Commit bfabd65

Browse files
Extensions for client and response (#2221)
* Added new extensions * Added polyfills
1 parent bc0262e commit bfabd65

File tree

84 files changed

+1032
-499
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1032
-499
lines changed

.editorconfig

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ resharper_wrap_array_initializer_style = chop_always
3636
resharper_wrap_before_arrow_with_expressions = true
3737
resharper_wrap_chained_binary_expressions = chop_if_long
3838
resharper_wrap_object_and_collection_initializer_style = chop_always
39+
resharper_invert_if_highlighting = none
3940

4041
# Microsoft .NET properties
4142
csharp_using_directive_placement = outside_namespace
@@ -45,17 +46,7 @@ csharp_preferred_modifier_order = public, private, protected, internal, new, abs
4546
csharp_style_var_elsewhere = true:suggestion
4647
csharp_style_var_for_built_in_types = true:suggestion
4748
csharp_style_var_when_type_is_apparent = true:suggestion
48-
dotnet_naming_rule.unity_serialized_field_rule.import_to_resharper = True
49-
dotnet_naming_rule.unity_serialized_field_rule.resharper_description = Unity serialized field
50-
dotnet_naming_rule.unity_serialized_field_rule.resharper_guid = 5f0fdb63-c892-4d2c-9324-15c80b22a7ef
51-
dotnet_naming_rule.unity_serialized_field_rule.severity = warning
52-
dotnet_naming_rule.unity_serialized_field_rule.style = lower_camel_case_style
53-
dotnet_naming_rule.unity_serialized_field_rule.symbols = unity_serialized_field_symbols
5449
dotnet_naming_style.lower_camel_case_style.capitalization = camel_case
55-
dotnet_naming_symbols.unity_serialized_field_symbols.applicable_accessibilities = *
56-
dotnet_naming_symbols.unity_serialized_field_symbols.applicable_kinds =
57-
dotnet_naming_symbols.unity_serialized_field_symbols.resharper_applicable_kinds = unity_serialised_field
58-
dotnet_naming_symbols.unity_serialized_field_symbols.resharper_required_modifiers = instance
5950
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
6051
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
6152
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
@@ -81,6 +72,7 @@ resharper_suggest_var_or_type_simple_types_highlighting = hint
8172
resharper_web_config_module_not_resolved_highlighting = warning
8273
resharper_web_config_type_not_resolved_highlighting = warning
8374
resharper_web_config_wrong_module_highlighting = warning
75+
resharper_redundant_using_directive_highlighting = error
8476

8577
[*.{appxmanifest,asax,ascx,aspx,axaml,build,cg,cginc,compute,cs,cshtml,dtd,fs,fsi,fsscript,fsx,hlsl,hlsli,hlslinc,master,ml,mli,nuspec,paml,razor,resw,resx,shader,skin,usf,ush,vb,xaml,xamlx,xoml,xsd}]
8678
indent_style = space

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<PropertyGroup Condition="'$(TargetFramework)' == 'net471' Or '$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'net48'">
2222
<AddSystemTextJson>true</AddSystemTextJson>
23-
<AddNullable>true</AddNullable>
23+
<AddPolyfills>true</AddPolyfills>
2424
</PropertyGroup>
2525
<ItemGroup>
2626
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All"/>

src/RestSharp.Serializers.CsvHelper/CsvHelperSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public CsvHelperSerializer() : this(new CsvConfiguration(CultureInfo.InvariantCu
5454
}
5555

5656
foreach (var record in csvReader.GetRecords(itemType)) {
57-
method.Invoke(result, new[] { record });
57+
method.Invoke(result, [record]);
5858
}
5959

6060
return result;

src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class JsonNetSerializer : IRestSerializer, ISerializer, IDeserializer {
3434
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
3535
};
3636

37-
[ThreadStatic] static WriterBuffer? writerBuffer;
37+
[ThreadStatic] static WriterBuffer? _writerBuffer;
3838

3939
readonly JsonSerializer _serializer;
4040

@@ -52,11 +52,11 @@ public class JsonNetSerializer : IRestSerializer, ISerializer, IDeserializer {
5252
public string? Serialize(object? obj) {
5353
if (obj == null) return null;
5454

55-
using var writerBuffer = JsonNetSerializer.writerBuffer ??= new WriterBuffer(_serializer);
55+
using var buffer = _writerBuffer ??= new WriterBuffer(_serializer);
5656

57-
_serializer.Serialize(writerBuffer.GetJsonTextWriter(), obj, obj.GetType());
57+
_serializer.Serialize(buffer.GetJsonTextWriter(), obj, obj.GetType());
5858

59-
return writerBuffer.GetStringWriter().ToString();
59+
return buffer.GetStringWriter().ToString();
6060
}
6161

6262
public string? Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value);

src/RestSharp.Serializers.Xml/SerializeAsAttribute.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
using System.Globalization;
1616
using RestSharp.Extensions;
17+
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
18+
// ReSharper disable MemberCanBePrivate.Global
1719

1820
// ReSharper disable once CheckNamespace
1921
namespace RestSharp.Serializers;

src/RestSharp.Serializers.Xml/XmlDeserializer.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Xml;
2020
using System.Xml.Linq;
2121
using RestSharp.Extensions;
22+
// ReSharper disable VirtualMemberNeverOverridden.Global
2223

2324
namespace RestSharp.Serializers.Xml;
2425

@@ -37,7 +38,7 @@ public class XmlDeserializer : IXmlDeserializer, IWithRootElement, IWithDateForm
3738
if (string.IsNullOrEmpty(response.Content))
3839
return default;
3940

40-
var doc = XDocument.Parse(response.Content);
41+
var doc = XDocument.Parse(response.Content!);
4142
var root = doc.Root;
4243
var rootElement = response.RootElement ?? RootElement;
4344

@@ -161,8 +162,7 @@ protected virtual object Map(object x, XElement? root) {
161162
var asType = type.AsType();
162163

163164
if (asType == typeof(bool)) {
164-
var toConvert = value.ToString()!
165-
.ToLower(Culture);
165+
var toConvert = value.ToString()!.ToLower(Culture);
166166

167167
prop.SetValue(x, XmlConvert.ToBoolean(toConvert), null);
168168
}
@@ -227,9 +227,7 @@ protected virtual object Map(object x, XElement? root) {
227227
else if (asType == typeof(Guid)) {
228228
var raw = value.ToString();
229229

230-
value = string.IsNullOrEmpty(raw)
231-
? Guid.Empty
232-
: new Guid(value.ToString()!);
230+
value = string.IsNullOrEmpty(raw) ? Guid.Empty : new Guid(value.ToString()!);
233231

234232
prop.SetValue(x, value, null);
235233
}

src/RestSharp.Serializers.Xml/XmlSerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public XmlSerializer() { }
3333
/// Specify the namespaced to be used when serializing
3434
/// </summary>
3535
/// <param name="namespace">XML namespace</param>
36+
[PublicAPI]
3637
public XmlSerializer(string @namespace) => Namespace = @namespace;
3738

3839
/// <summary>

src/RestSharp/AsyncHelpers.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static class AsyncHelpers {
2424
/// Executes a task synchronously on the calling thread by installing a temporary synchronization context that queues continuations
2525
/// </summary>
2626
/// <param name="task">Callback for asynchronous task to run</param>
27-
public static void RunSync(Func<Task> task) {
27+
static void RunSync(Func<Task> task) {
2828
var currentContext = SynchronizationContext.Current;
2929
var customContext = new CustomSynchronizationContext(task);
3030

@@ -80,19 +80,6 @@ public override void Post(SendOrPostCallback function, object? state) {
8080
/// Enqueues the function to be executed and executes all resulting continuations until it is completely done
8181
/// </summary>
8282
public void Run() {
83-
async void PostCallback(object? _) {
84-
try {
85-
await _task().ConfigureAwait(false);
86-
}
87-
catch (Exception exception) {
88-
_caughtException = ExceptionDispatchInfo.Capture(exception);
89-
throw;
90-
}
91-
finally {
92-
Post(_ => _done = true, null);
93-
}
94-
}
95-
9683
Post(PostCallback, null);
9784

9885
while (!_done) {
@@ -107,6 +94,21 @@ async void PostCallback(object? _) {
10794
_workItemsWaiting.WaitOne();
10895
}
10996
}
97+
98+
return;
99+
100+
async void PostCallback(object? _) {
101+
try {
102+
await _task().ConfigureAwait(false);
103+
}
104+
catch (Exception exception) {
105+
_caughtException = ExceptionDispatchInfo.Capture(exception);
106+
throw;
107+
}
108+
finally {
109+
Post(_ => _done = true, null);
110+
}
111+
}
110112
}
111113

112114
/// <summary>

src/RestSharp/Authenticators/OAuth/OAuth1Authenticator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
using RestSharp.Authenticators.OAuth;
1616
using RestSharp.Extensions;
1717
using System.Web;
18+
// ReSharper disable PropertyCanBeMadeInitOnly.Global
1819

1920
// ReSharper disable NotResolvedInText
2021
// ReSharper disable CheckNamespace
2122

2223
namespace RestSharp.Authenticators;
2324

2425
/// <seealso href="http://tools.ietf.org/html/rfc5849">RFC: The OAuth 1.0 Protocol</seealso>
26+
// ReSharper disable once ClassWithVirtualMembersNeverInherited.Global
2527
public class OAuth1Authenticator : IAuthenticator {
2628
public virtual string? Realm { get; set; }
2729
public virtual OAuthParameterHandling ParameterHandling { get; set; }
@@ -246,7 +248,7 @@ internal static void AddOAuthData(
246248
var url = client.BuildUri(request).ToString();
247249
var queryStringStart = url.IndexOf('?');
248250

249-
if (queryStringStart != -1) url = url.Substring(0, queryStringStart);
251+
if (queryStringStart != -1) url = url[..queryStringStart];
250252

251253
var method = request.Method.ToString().ToUpperInvariant();
252254
var parameters = new WebPairCollection();

src/RestSharp/Authenticators/OAuth/OAuthTools.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ static class OAuthTools {
3636
/// <summary>
3737
/// The set of characters that are unreserved in RFC 2396 but are NOT unreserved in RFC 3986.
3838
/// </summary>
39-
static readonly string[] UriRfc3986CharsToEscape = { "!", "*", "'", "(", ")" };
39+
static readonly string[] UriRfc3986CharsToEscape = ["!", "*", "'", "(", ")"];
4040

41-
static readonly string[] UriRfc3968EscapedHex = { "%21", "%2A", "%27", "%28", "%29" };
41+
static readonly string[] UriRfc3968EscapedHex = ["%21", "%2A", "%27", "%28", "%29"];
4242

4343
static OAuthTools() {
4444
var bytes = new byte[4];

0 commit comments

Comments
 (0)