From 922bf070ec5f31005d6941d3acdac1395b56f03b Mon Sep 17 00:00:00 2001 From: Alexey Zimarev Date: Thu, 15 Jul 2021 13:45:13 +0200 Subject: [PATCH] Cleanup before the release --- src/Directory.Build.props | 3 +- .../JsonNetSerializer.cs | 31 +++++----- .../RestClientExtensions.cs | 8 +-- .../RestRequestExtensions.cs | 16 +++-- .../WriterBuffer.cs | 20 +++--- .../RestClientExtensions.cs | 8 +-- .../RestRequestExtensions.cs | 16 +++-- ...estSharp.Serializers.SystemTextJson.csproj | 4 +- .../SystemTextJsonSerializer.cs | 9 +-- .../RestClientExtensions.cs | 9 ++- .../RestRequestExtensions.cs | 16 +++-- .../RestSharp.Serializers.Utf8Json.csproj | 4 +- .../Utf8JsonSerializer.cs | 19 +++--- .../OAuth/OAuth1Authenticator.cs | 4 +- src/RestSharp/Authenticators/OAuth/WebPair.cs | 2 +- .../Extensions/ReflectionExtensions.cs | 6 +- src/RestSharp/FileParameter.cs | 26 ++++---- src/RestSharp/HttpCookie.cs | 2 +- src/RestSharp/HttpResponse.cs | 2 +- src/RestSharp/JsonRequest.cs | 45 -------------- src/RestSharp/Legacy/RestClient.cs | 2 +- src/RestSharp/NameValuePair.cs | 18 +++--- src/RestSharp/RestClientJsonRequest.cs | 61 ------------------- src/RestSharp/RestRequest.cs | 8 +-- src/RestSharp/Serializers/IDeserializer.cs | 2 +- .../Serializers/Xml/DotNetXmlSerializer.cs | 2 +- .../Serializers/Xml/XmlDeserializer.cs | 28 ++++----- .../Serializers/Xml/XmlRestSerializer.cs | 15 ++--- .../Serializers/Xml/XmlSerializer.cs | 2 +- 29 files changed, 134 insertions(+), 254 deletions(-) delete mode 100644 src/RestSharp/JsonRequest.cs delete mode 100644 src/RestSharp/RestClientJsonRequest.cs diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 8a0230628..ba7762fac 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -13,7 +13,8 @@ true true true - 8 + 9 + enable true snupkg diff --git a/src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs b/src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs index c20b2bc18..713c1813b 100644 --- a/src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs +++ b/src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs @@ -1,13 +1,12 @@ using System; using System.IO; +using JetBrains.Annotations; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using RestSharp.Serialization; -namespace RestSharp.Serializers.NewtonsoftJson -{ - public class JsonNetSerializer : IRestSerializer - { +namespace RestSharp.Serializers.NewtonsoftJson { + public class JsonNetSerializer : IRestSerializer { /// /// Default serialization settings: /// - Camel-case contract resolver @@ -16,8 +15,7 @@ public class JsonNetSerializer : IRestSerializer /// - Non-indented formatting /// - Allow using non-public constructors /// - public static readonly JsonSerializerSettings DefaultSettings = new JsonSerializerSettings - { + public static readonly JsonSerializerSettings DefaultSettings = new() { ContractResolver = new CamelCasePropertyNamesContractResolver(), DefaultValueHandling = DefaultValueHandling.Include, TypeNameHandling = TypeNameHandling.None, @@ -26,7 +24,7 @@ public class JsonNetSerializer : IRestSerializer ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor }; - [ThreadStatic] static WriterBuffer tWriterBuffer; + [ThreadStatic] static WriterBuffer? tWriterBuffer; readonly JsonSerializer _serializer; @@ -41,8 +39,9 @@ public class JsonNetSerializer : IRestSerializer /// Json.Net serializer settings public JsonNetSerializer(JsonSerializerSettings settings) => _serializer = JsonSerializer.Create(settings); - public string Serialize(object obj) - { + public string? Serialize(object? obj) { + if (obj == null) return null; + using var writerBuffer = tWriterBuffer ??= new WriterBuffer(_serializer); _serializer.Serialize(writerBuffer.GetJsonTextWriter(), obj, obj.GetType()); @@ -50,22 +49,20 @@ public string Serialize(object obj) return writerBuffer.GetStringWriter().ToString(); } - public string Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value); + public string? Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value); - public T Deserialize(IRestResponse response) - { - using var reader = new JsonTextReader(new StringReader(response.Content)) {CloseInput = true}; + public T? Deserialize(IRestResponse response) { + using var reader = new JsonTextReader(new StringReader(response.Content)) { CloseInput = true }; return _serializer.Deserialize(reader); } - public string[] SupportedContentTypes { get; } = - { + public string[] SupportedContentTypes { get; } = { "application/json", "text/json", "text/x-json", "text/javascript", "*+json" }; public string ContentType { get; set; } = "application/json"; - public DataFormat DataFormat { get; } = DataFormat.Json; + public DataFormat DataFormat => DataFormat.Json; } -} +} \ No newline at end of file diff --git a/src/RestSharp.Serializers.NewtonsoftJson/RestClientExtensions.cs b/src/RestSharp.Serializers.NewtonsoftJson/RestClientExtensions.cs index 29c737e25..5e0aa015e 100644 --- a/src/RestSharp.Serializers.NewtonsoftJson/RestClientExtensions.cs +++ b/src/RestSharp.Serializers.NewtonsoftJson/RestClientExtensions.cs @@ -1,9 +1,9 @@ +using JetBrains.Annotations; using Newtonsoft.Json; -namespace RestSharp.Serializers.NewtonsoftJson -{ - public static class RestClientExtensions - { +namespace RestSharp.Serializers.NewtonsoftJson { + [PublicAPI] + public static class RestClientExtensions { /// /// Use Newtonsoft.Json serializer with default settings /// diff --git a/src/RestSharp.Serializers.NewtonsoftJson/RestRequestExtensions.cs b/src/RestSharp.Serializers.NewtonsoftJson/RestRequestExtensions.cs index 6ff1aaf63..bd2162382 100644 --- a/src/RestSharp.Serializers.NewtonsoftJson/RestRequestExtensions.cs +++ b/src/RestSharp.Serializers.NewtonsoftJson/RestRequestExtensions.cs @@ -1,28 +1,26 @@ +using JetBrains.Annotations; using Newtonsoft.Json; -namespace RestSharp.Serializers.NewtonsoftJson -{ - public static class RestRequestExtensions - { +namespace RestSharp.Serializers.NewtonsoftJson { + [PublicAPI] + public static class RestRequestExtensions { /// /// Use Newtonsoft.Json serializer for a single request /// /// /// - public static IRestRequest UseNewtonsoftJson(this IRestRequest request) - { + public static IRestRequest UseNewtonsoftJson(this IRestRequest request) { request.JsonSerializer = new JsonNetSerializer(); return request; } - + /// /// Use Newtonsoft.Json serializer for a single request, with custom settings /// /// /// Newtonsoft.Json serializer settings /// - public static IRestRequest UseNewtonsoftJson(this IRestRequest request, JsonSerializerSettings settings) - { + public static IRestRequest UseNewtonsoftJson(this IRestRequest request, JsonSerializerSettings settings) { request.JsonSerializer = new JsonNetSerializer(settings); return request; } diff --git a/src/RestSharp.Serializers.NewtonsoftJson/WriterBuffer.cs b/src/RestSharp.Serializers.NewtonsoftJson/WriterBuffer.cs index a94be62f9..4ab385f3d 100644 --- a/src/RestSharp.Serializers.NewtonsoftJson/WriterBuffer.cs +++ b/src/RestSharp.Serializers.NewtonsoftJson/WriterBuffer.cs @@ -34,25 +34,23 @@ using System.Text; using Newtonsoft.Json; -namespace RestSharp.Serializers.NewtonsoftJson -{ - public sealed class WriterBuffer : IDisposable - { - private readonly StringWriter _stringWriter; - private readonly JsonTextWriter _jsonTextWriter; +namespace RestSharp.Serializers.NewtonsoftJson { + sealed class WriterBuffer : IDisposable { + readonly StringWriter _stringWriter; + readonly JsonTextWriter _jsonTextWriter; - public WriterBuffer(JsonSerializer jsonSerializer) - { + public WriterBuffer(JsonSerializer jsonSerializer) { _stringWriter = new StringWriter(new StringBuilder(256), CultureInfo.InvariantCulture); - _jsonTextWriter = new JsonTextWriter(_stringWriter) - { + _jsonTextWriter = new JsonTextWriter(_stringWriter) { Formatting = jsonSerializer.Formatting, CloseOutput = false }; } public JsonTextWriter GetJsonTextWriter() => _jsonTextWriter; + public StringWriter GetStringWriter() => _stringWriter; + public void Dispose() => _stringWriter.GetStringBuilder().Clear(); } -} +} \ No newline at end of file diff --git a/src/RestSharp.Serializers.SystemTextJson/RestClientExtensions.cs b/src/RestSharp.Serializers.SystemTextJson/RestClientExtensions.cs index 10393d30a..dccd70322 100644 --- a/src/RestSharp.Serializers.SystemTextJson/RestClientExtensions.cs +++ b/src/RestSharp.Serializers.SystemTextJson/RestClientExtensions.cs @@ -1,9 +1,9 @@ using System.Text.Json; +using JetBrains.Annotations; -namespace RestSharp.Serializers.SystemTextJson -{ - public static class RestClientExtensions - { +namespace RestSharp.Serializers.SystemTextJson { + [PublicAPI] + public static class RestClientExtensions { /// /// Use System.Text.Json serializer with default settings /// diff --git a/src/RestSharp.Serializers.SystemTextJson/RestRequestExtensions.cs b/src/RestSharp.Serializers.SystemTextJson/RestRequestExtensions.cs index 3225bfda8..049e676b9 100644 --- a/src/RestSharp.Serializers.SystemTextJson/RestRequestExtensions.cs +++ b/src/RestSharp.Serializers.SystemTextJson/RestRequestExtensions.cs @@ -1,28 +1,26 @@ using System.Text.Json; +using JetBrains.Annotations; -namespace RestSharp.Serializers.SystemTextJson -{ - public static class RestRequestExtensions - { +namespace RestSharp.Serializers.SystemTextJson { + [PublicAPI] + public static class RestRequestExtensions { /// /// Use System.Text.Json serializer for a single request /// /// /// - public static IRestRequest UseSystemTextJson(this IRestRequest request) - { + public static IRestRequest UseSystemTextJson(this IRestRequest request) { request.JsonSerializer = new SystemTextJsonSerializer(); return request; } - + /// /// Use System.Text.Json serializer for a single request with custom options /// /// /// System.Text.Json serializer options /// - public static IRestRequest UseSystemTextJson(this IRestRequest request, JsonSerializerOptions options) - { + public static IRestRequest UseSystemTextJson(this IRestRequest request, JsonSerializerOptions options) { request.JsonSerializer = new SystemTextJsonSerializer(options); return request; } diff --git a/src/RestSharp.Serializers.SystemTextJson/RestSharp.Serializers.SystemTextJson.csproj b/src/RestSharp.Serializers.SystemTextJson/RestSharp.Serializers.SystemTextJson.csproj index e378708ce..58808819a 100644 --- a/src/RestSharp.Serializers.SystemTextJson/RestSharp.Serializers.SystemTextJson.csproj +++ b/src/RestSharp.Serializers.SystemTextJson/RestSharp.Serializers.SystemTextJson.csproj @@ -3,9 +3,9 @@ netstandard2.0;net461 - + - + diff --git a/src/RestSharp.Serializers.SystemTextJson/SystemTextJsonSerializer.cs b/src/RestSharp.Serializers.SystemTextJson/SystemTextJsonSerializer.cs index 92f4a84ba..01a2faaa3 100644 --- a/src/RestSharp.Serializers.SystemTextJson/SystemTextJsonSerializer.cs +++ b/src/RestSharp.Serializers.SystemTextJson/SystemTextJsonSerializer.cs @@ -1,4 +1,5 @@ using System.Text.Json; +using JetBrains.Annotations; using RestSharp.Serialization; namespace RestSharp.Serializers.SystemTextJson @@ -18,11 +19,11 @@ public class SystemTextJsonSerializer : IRestSerializer /// Json serializer settings public SystemTextJsonSerializer(JsonSerializerOptions options) => _options = options; - public string Serialize(object obj) => JsonSerializer.Serialize(obj, _options); + public string? Serialize(object? obj) => obj == null ? null : JsonSerializer.Serialize(obj, _options); - public string Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value); + public string? Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value); - public T Deserialize(IRestResponse response) => JsonSerializer.Deserialize(response.Content, _options); + public T? Deserialize(IRestResponse response) => JsonSerializer.Deserialize(response.Content, _options); public string[] SupportedContentTypes { get; } = { @@ -31,6 +32,6 @@ public class SystemTextJsonSerializer : IRestSerializer public string ContentType { get; set; } = "application/json"; - public DataFormat DataFormat { get; } = DataFormat.Json; + public DataFormat DataFormat => DataFormat.Json; } } \ No newline at end of file diff --git a/src/RestSharp.Serializers.Utf8Json/RestClientExtensions.cs b/src/RestSharp.Serializers.Utf8Json/RestClientExtensions.cs index bcb5b1eb0..6e913f7b2 100644 --- a/src/RestSharp.Serializers.Utf8Json/RestClientExtensions.cs +++ b/src/RestSharp.Serializers.Utf8Json/RestClientExtensions.cs @@ -1,10 +1,9 @@ - +using JetBrains.Annotations; using Utf8Json; -namespace RestSharp.Serializers.Utf8Json -{ - public static class RestClientExtensions - { +namespace RestSharp.Serializers.Utf8Json { + [PublicAPI] + public static class RestClientExtensions { /// /// Use Utf8Json serializer with default formatter resolver /// diff --git a/src/RestSharp.Serializers.Utf8Json/RestRequestExtensions.cs b/src/RestSharp.Serializers.Utf8Json/RestRequestExtensions.cs index 9463e50c3..3b70e04af 100644 --- a/src/RestSharp.Serializers.Utf8Json/RestRequestExtensions.cs +++ b/src/RestSharp.Serializers.Utf8Json/RestRequestExtensions.cs @@ -1,28 +1,26 @@ +using JetBrains.Annotations; using Utf8Json; -namespace RestSharp.Serializers.Utf8Json -{ - public static class RestRequestExtensions - { +namespace RestSharp.Serializers.Utf8Json { + [PublicAPI] + public static class RestRequestExtensions { /// /// Use Utf8Json serializer for a single request /// /// /// - public static IRestRequest UseUtf8Json(this IRestRequest request) - { + public static IRestRequest UseUtf8Json(this IRestRequest request) { request.JsonSerializer = new Utf8JsonSerializer(); return request; } - + /// /// Use Utf8Json serializer for a single request /// /// /// JSON formatter resolver instance to provide custom options to Utf8Json /// - public static IRestRequest UseUtf8Json(this IRestRequest request, IJsonFormatterResolver resolver) - { + public static IRestRequest UseUtf8Json(this IRestRequest request, IJsonFormatterResolver resolver) { request.JsonSerializer = new Utf8JsonSerializer(resolver); return request; } diff --git a/src/RestSharp.Serializers.Utf8Json/RestSharp.Serializers.Utf8Json.csproj b/src/RestSharp.Serializers.Utf8Json/RestSharp.Serializers.Utf8Json.csproj index a080a322c..b109251b7 100644 --- a/src/RestSharp.Serializers.Utf8Json/RestSharp.Serializers.Utf8Json.csproj +++ b/src/RestSharp.Serializers.Utf8Json/RestSharp.Serializers.Utf8Json.csproj @@ -3,9 +3,9 @@ netstandard2.0;net452 - + - + diff --git a/src/RestSharp.Serializers.Utf8Json/Utf8JsonSerializer.cs b/src/RestSharp.Serializers.Utf8Json/Utf8JsonSerializer.cs index 3fc194f82..e16ba5467 100644 --- a/src/RestSharp.Serializers.Utf8Json/Utf8JsonSerializer.cs +++ b/src/RestSharp.Serializers.Utf8Json/Utf8JsonSerializer.cs @@ -32,27 +32,24 @@ using Utf8Json; using Utf8Json.Resolvers; -namespace RestSharp.Serializers.Utf8Json -{ - public class Utf8JsonSerializer : IRestSerializer - { - public Utf8JsonSerializer(IJsonFormatterResolver resolver = null) => Resolver = resolver ?? StandardResolver.AllowPrivateExcludeNullCamelCase; +namespace RestSharp.Serializers.Utf8Json { + public class Utf8JsonSerializer : IRestSerializer { + public Utf8JsonSerializer(IJsonFormatterResolver? resolver = null) => Resolver = resolver ?? StandardResolver.AllowPrivateExcludeNullCamelCase; IJsonFormatterResolver Resolver { get; } - public string Serialize(object obj) => JsonSerializer.NonGeneric.ToJsonString(obj, Resolver); + public string? Serialize(object? obj) => obj == null ? null : JsonSerializer.NonGeneric.ToJsonString(obj, Resolver); - public string Serialize(Parameter parameter) => Serialize(parameter.Value); + public string? Serialize(Parameter parameter) => Serialize(parameter.Value); - public T Deserialize(IRestResponse response) => JsonSerializer.Deserialize(response.RawBytes, Resolver); + public T? Deserialize(IRestResponse response) => JsonSerializer.Deserialize(response.RawBytes, Resolver); - public string[] SupportedContentTypes { get; } = - { + public string[] SupportedContentTypes { get; } = { "application/json", "text/json", "text/x-json", "text/javascript", "*+json" }; public string ContentType { get; set; } = "application/json"; - public DataFormat DataFormat { get; } = DataFormat.Json; + public DataFormat DataFormat => DataFormat.Json; } } \ No newline at end of file diff --git a/src/RestSharp/Authenticators/OAuth/OAuth1Authenticator.cs b/src/RestSharp/Authenticators/OAuth/OAuth1Authenticator.cs index 7b3384c56..a4641ff5a 100644 --- a/src/RestSharp/Authenticators/OAuth/OAuth1Authenticator.cs +++ b/src/RestSharp/Authenticators/OAuth/OAuth1Authenticator.cs @@ -274,7 +274,7 @@ void AddOAuthData(IRestClient client, IRestRequest request, OAuthWorkflow workfl // if this change causes trouble we need to introduce a flag indicating the specific OAuth implementation level, // or implement a separate class for each OAuth version static bool BaseQuery(Parameter x) - => x.Type == ParameterType.GetOrPost || x.Type == ParameterType.QueryString || x.Type == ParameterType.QueryStringWithoutEncode; + => x.Type is ParameterType.GetOrPost or ParameterType.QueryString or ParameterType.QueryStringWithoutEncode; var query = request.AlwaysMultipartFormData || request.Files.Count > 0 @@ -334,6 +334,6 @@ string GetAuthorizationHeader() static class ParametersExtensions { - internal static IEnumerable ToWebParameters(this IEnumerable p) => p.Select(x => new WebPair(x.Name, x.Value.ToString(), false)); + internal static IEnumerable ToWebParameters(this IEnumerable p) => p.Select(x => new WebPair(x.Name, x.Value.ToString())); } } \ No newline at end of file diff --git a/src/RestSharp/Authenticators/OAuth/WebPair.cs b/src/RestSharp/Authenticators/OAuth/WebPair.cs index 97b5c882b..ed32f0669 100644 --- a/src/RestSharp/Authenticators/OAuth/WebPair.cs +++ b/src/RestSharp/Authenticators/OAuth/WebPair.cs @@ -31,7 +31,7 @@ public WebPair(string name, string value, bool encode = false) public string WebValue { get; } public bool Encode { get; } - internal static WebPairComparer Comparer { get; } = new WebPairComparer(); + internal static WebPairComparer Comparer { get; } = new(); internal class WebPairComparer : IComparer { diff --git a/src/RestSharp/Extensions/ReflectionExtensions.cs b/src/RestSharp/Extensions/ReflectionExtensions.cs index 603c7a5ee..7fa0ed879 100644 --- a/src/RestSharp/Extensions/ReflectionExtensions.cs +++ b/src/RestSharp/Extensions/ReflectionExtensions.cs @@ -30,7 +30,7 @@ public static class ReflectionExtensions /// Type of attribute to retrieve /// Member to retrieve attribute from /// - public static T GetAttribute(this MemberInfo prop) where T : Attribute => Attribute.GetCustomAttribute(prop, typeof(T)) as T; + public static T? GetAttribute(this MemberInfo prop) where T : Attribute => Attribute.GetCustomAttribute(prop, typeof(T)) as T; /// /// Retrieve an attribute from a type @@ -38,7 +38,7 @@ public static class ReflectionExtensions /// Type of attribute to retrieve /// Type to retrieve attribute from /// - public static T GetAttribute(this Type type) where T : Attribute => Attribute.GetCustomAttribute(type, typeof(T)) as T; + public static T? GetAttribute(this Type type) where T : Attribute => Attribute.GetCustomAttribute(type, typeof(T)) as T; /// /// Checks a type to see if it derives from a raw generic (e.g. List[[]]) @@ -74,7 +74,7 @@ public static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic) /// Value for which to search /// The culture used to calculate the name variants /// - public static object FindEnumValue(this Type type, string value, CultureInfo culture) + public static object? FindEnumValue(this Type type, string value, CultureInfo culture) { var caseInsensitiveComparer = StringComparer.Create(culture, true); diff --git a/src/RestSharp/FileParameter.cs b/src/RestSharp/FileParameter.cs index 2f5c9a0e3..b0dcaa655 100644 --- a/src/RestSharp/FileParameter.cs +++ b/src/RestSharp/FileParameter.cs @@ -14,14 +14,14 @@ using System; using System.IO; +using JetBrains.Annotations; -namespace RestSharp -{ +namespace RestSharp { /// /// Container for files to be uploaded with requests /// - public class FileParameter - { + [PublicAPI] + public class FileParameter { /// /// The length of data to be sent /// @@ -40,7 +40,7 @@ public class FileParameter /// /// MIME content type of file /// - public string ContentType { get; set; } + public string? ContentType { get; set; } /// /// Name of the parameter @@ -55,9 +55,8 @@ public class FileParameter /// The filename to use in the request. /// The content type to use in the request. /// The - public static FileParameter Create(string name, byte[] data, string filename, string contentType) - => new FileParameter - { + public static FileParameter Create(string name, byte[] data, string filename, string? contentType) + => new() { Writer = s => s.Write(data, 0, data.Length), FileName = filename, ContentType = contentType, @@ -84,14 +83,13 @@ public static FileParameter Create(string name, byte[] data, string filename, st /// Optional: parameter content type /// The using the default content type. public static FileParameter Create( - string name, + string name, Action writer, - long contentLength, - string fileName, - string contentType = null + long contentLength, + string fileName, + string? contentType = null ) - => new FileParameter - { + => new() { Name = name, FileName = fileName, ContentType = contentType, diff --git a/src/RestSharp/HttpCookie.cs b/src/RestSharp/HttpCookie.cs index 19ade7392..ec4421bcf 100644 --- a/src/RestSharp/HttpCookie.cs +++ b/src/RestSharp/HttpCookie.cs @@ -19,7 +19,7 @@ namespace RestSharp /// /// Representation of an HTTP cookie /// - [Obsolete("The HttpCooking class will be removed in future versions")] + [Obsolete("The HttpCookie class will be removed in future versions")] public class HttpCookie { /// diff --git a/src/RestSharp/HttpResponse.cs b/src/RestSharp/HttpResponse.cs index 31def81cc..3b5a05893 100644 --- a/src/RestSharp/HttpResponse.cs +++ b/src/RestSharp/HttpResponse.cs @@ -24,7 +24,7 @@ namespace RestSharp [PublicAPI] public class HttpResponse : IHttpResponse { - string _content; + string? _content; public HttpResponse() { diff --git a/src/RestSharp/JsonRequest.cs b/src/RestSharp/JsonRequest.cs deleted file mode 100644 index f088b0c51..000000000 --- a/src/RestSharp/JsonRequest.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net; -using RestSharp.Extensions; - -namespace RestSharp -{ - public class JsonRequest : RestRequest - { - readonly List>> _changeResponse = new List>>(); - - readonly Dictionary> _customResponses = - new Dictionary>(); - - public JsonRequest(string resource, TRequest request) : base(resource) - { - AddJsonBody(request); - _changeResponse.Add(ApplyCustomResponse); - } - - public JsonRequest ResponseForStatusCode(HttpStatusCode statusCode, TResponse response) - => this.With(x => _customResponses.Add(statusCode, () => response)); - - public JsonRequest ResponseForStatusCode( - HttpStatusCode statusCode, - Func getResponse - ) - => this.With(x => _customResponses.Add(statusCode, getResponse)); - - public JsonRequest ChangeResponse(Action> change) - => this.With(x => x._changeResponse.Add(change)); - - void ApplyCustomResponse(IRestResponse response) - { - if (_customResponses.TryGetValue(response.StatusCode, out var getResponse)) - response.Data = getResponse(); - } - - internal IRestResponse UpdateResponse(IRestResponse response) - { - _changeResponse.ForEach(x => x(response)); - return response; - } - } -} \ No newline at end of file diff --git a/src/RestSharp/Legacy/RestClient.cs b/src/RestSharp/Legacy/RestClient.cs index 405a0eb10..5e1749a09 100644 --- a/src/RestSharp/Legacy/RestClient.cs +++ b/src/RestSharp/Legacy/RestClient.cs @@ -26,7 +26,7 @@ public partial class RestClient { static HttpWebRequest DoAsGetAsync(IHttp http, Action responseCb, string method) => http.AsGetAsync(responseCb, method); - static HttpWebRequest DoAsPostAsync(IHttp http, Action responseCb, string method) => http.AsPostAsync(responseCb, method); + static HttpWebRequest? DoAsPostAsync(IHttp http, Action responseCb, string method) => http.AsPostAsync(responseCb, method); /// /// Executes the request asynchronously, authenticating if needed diff --git a/src/RestSharp/NameValuePair.cs b/src/RestSharp/NameValuePair.cs index 5e66f6e9a..538bb3265 100644 --- a/src/RestSharp/NameValuePair.cs +++ b/src/RestSharp/NameValuePair.cs @@ -12,20 +12,20 @@ // See the License for the specific language governing permissions and // limitations under the License. -namespace RestSharp -{ - public class NameValuePair - { - public static NameValuePair Empty = new NameValuePair(null, null); +using JetBrains.Annotations; - public NameValuePair(string name, string value) - { +namespace RestSharp { + [PublicAPI] + public class NameValuePair { + public static NameValuePair Empty = new(null, null); + + public NameValuePair(string? name, string? value) { Name = name; Value = value; } - public string Name { get; } - public string Value { get; } + public string? Name { get; } + public string? Value { get; } public bool IsEmpty => Name == null; } diff --git a/src/RestSharp/RestClientJsonRequest.cs b/src/RestSharp/RestClientJsonRequest.cs deleted file mode 100644 index 9d7900e47..000000000 --- a/src/RestSharp/RestClientJsonRequest.cs +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -namespace RestSharp -{ - public static class RestClientJsonRequest - { - public static IRestResponse Get( - this IRestClient client, - JsonRequest request - ) where TResponse : new() - => request.UpdateResponse(client.Execute(request, Method.GET)); - - public static IRestResponse Post( - this IRestClient client, - JsonRequest request - ) where TResponse : new() - => request.UpdateResponse(client.Execute(request, Method.POST)); - - public static IRestResponse Put( - this IRestClient client, - JsonRequest request - ) where TResponse : new() - => request.UpdateResponse(client.Execute(request, Method.PUT)); - - public static IRestResponse Head( - this IRestClient client, - JsonRequest request - ) where TResponse : new() - => request.UpdateResponse(client.Execute(request, Method.HEAD)); - - public static IRestResponse Options( - this IRestClient client, - JsonRequest request - ) where TResponse : new() - => request.UpdateResponse(client.Execute(request, Method.OPTIONS)); - - public static IRestResponse Patch( - this IRestClient client, - JsonRequest request - ) where TResponse : new() - => request.UpdateResponse(client.Execute(request, Method.PATCH)); - - public static IRestResponse Delete( - this IRestClient client, - JsonRequest request - ) where TResponse : new() - => request.UpdateResponse(client.Execute(request, Method.DELETE)); - } -} \ No newline at end of file diff --git a/src/RestSharp/RestRequest.cs b/src/RestSharp/RestRequest.cs index ecdd7703d..eb38c6132 100644 --- a/src/RestSharp/RestRequest.cs +++ b/src/RestSharp/RestRequest.cs @@ -129,7 +129,7 @@ public RestRequest(Uri resource) : this(resource, Method.GET, DataFormat.Xml) { public IXmlSerializer XmlSerializer { get; set; } /// - public RequestBody Body { get; set; } + public RequestBody? Body { get; set; } /// public Action ResponseWriter @@ -463,10 +463,10 @@ public IRestRequest AddDecompressionMethod(DecompressionMethods decompressionMet public string RootElement { get; set; } /// - public Action OnBeforeDeserialization { get; set; } + public Action? OnBeforeDeserialization { get; set; } /// - public Action OnBeforeRequest { get; set; } + public Action? OnBeforeRequest { get; set; } /// [Obsolete("Add custom content handler instead. This property will be removed.")] @@ -477,7 +477,7 @@ public IRestRequest AddDecompressionMethod(DecompressionMethods decompressionMet public string XmlNamespace { get; set; } /// - public ICredentials Credentials { get; set; } + public ICredentials? Credentials { get; set; } /// public int Timeout { get; set; } diff --git a/src/RestSharp/Serializers/IDeserializer.cs b/src/RestSharp/Serializers/IDeserializer.cs index 51aa5bd9c..87ea77382 100644 --- a/src/RestSharp/Serializers/IDeserializer.cs +++ b/src/RestSharp/Serializers/IDeserializer.cs @@ -16,6 +16,6 @@ namespace RestSharp.Deserializers { public interface IDeserializer { - T Deserialize(IRestResponse response); + T? Deserialize(IRestResponse response); } } \ No newline at end of file diff --git a/src/RestSharp/Serializers/Xml/DotNetXmlSerializer.cs b/src/RestSharp/Serializers/Xml/DotNetXmlSerializer.cs index cc75e0d77..7e270cfaa 100644 --- a/src/RestSharp/Serializers/Xml/DotNetXmlSerializer.cs +++ b/src/RestSharp/Serializers/Xml/DotNetXmlSerializer.cs @@ -69,7 +69,7 @@ public string Serialize(object obj) /// /// Name of the root element to use when serializing /// - public string RootElement { get; set; } + public string? RootElement { get; set; } /// /// XML namespace to use when serializing diff --git a/src/RestSharp/Serializers/Xml/XmlDeserializer.cs b/src/RestSharp/Serializers/Xml/XmlDeserializer.cs index 8a5853638..a481d7035 100644 --- a/src/RestSharp/Serializers/Xml/XmlDeserializer.cs +++ b/src/RestSharp/Serializers/Xml/XmlDeserializer.cs @@ -32,13 +32,13 @@ public class XmlDeserializer : IXmlDeserializer public CultureInfo Culture { get; set; } - public string RootElement { get; set; } + public string? RootElement { get; set; } public string Namespace { get; set; } public string DateFormat { get; set; } - public virtual T Deserialize(IRestResponse response) + public virtual T? Deserialize(IRestResponse response) { if (string.IsNullOrEmpty(response.Content)) return default; @@ -46,7 +46,7 @@ public virtual T Deserialize(IRestResponse response) var doc = XDocument.Parse(response.Content); var root = doc.Root; - if (RootElement.HasValue() && doc.Root != null) + if (RootElement != null && doc.Root != null) root = doc.Root.DescendantsAndSelf(RootElement.AsNamespaced(Namespace)).SingleOrDefault(); // autodetect xml namespace @@ -54,12 +54,12 @@ public virtual T Deserialize(IRestResponse response) RemoveNamespace(doc); var x = Activator.CreateInstance(); - var objType = x.GetType(); + var objType = x!.GetType(); if (objType.IsSubclassOfRawGeneric(typeof(List<>))) - x = (T) HandleListDerivative(root, objType.Name, objType); + x = (T) HandleListDerivative(root!, objType.Name, objType); else - x = (T) Map(x, root); + x = (T) Map(x, root!); return x; } @@ -402,9 +402,9 @@ object HandleListDerivative(XElement root, string propName, Type type) return list; } - protected virtual object CreateAndMap(Type t, XElement element) + protected virtual object? CreateAndMap(Type t, XElement element) { - object item; + object? item; if (t == typeof(string)) { @@ -423,9 +423,9 @@ protected virtual object CreateAndMap(Type t, XElement element) return item; } - protected virtual object GetValueFromXml(XElement root, XName name, PropertyInfo prop, bool useExactName) + protected virtual object? GetValueFromXml(XElement? root, XName name, PropertyInfo prop, bool useExactName) { - object val = null; + object? val = null; if (root == null) return val; var element = GetElementByName(root, name); @@ -446,7 +446,7 @@ protected virtual object GetValueFromXml(XElement root, XName name, PropertyInfo return val; } - protected virtual XElement GetElementByName(XElement root, XName name) + protected virtual XElement? GetElementByName(XElement root, XName name) { var lowerName = name.LocalName.ToLower(Culture).AsNamespaced(name.NamespaceName); var camelName = name.LocalName.ToCamelCase(Culture).AsNamespaced(name.NamespaceName); @@ -462,7 +462,7 @@ protected virtual XElement GetElementByName(XElement root, XName name) // try looking for element that matches sanitized property name (Order by depth) var orderedDescendants = root.Descendants() - .OrderBy(d => d.Ancestors().Count()); + .OrderBy(d => d.Ancestors().Count()).ToList(); var element = orderedDescendants .FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName) ?? @@ -480,7 +480,7 @@ protected virtual XElement GetElementByName(XElement root, XName name) : element; } - protected virtual XAttribute GetAttributeByName(XElement root, XName name, bool useExactName) + protected virtual XAttribute? GetAttributeByName(XElement root, XName name, bool useExactName) { var names = useExactName ? null @@ -499,7 +499,7 @@ protected virtual XAttribute GetAttributeByName(XElement root, XName name, bool .FirstOrDefault( d => useExactName ? d.Name == name - : names.Contains(d.Name.LocalName.RemoveUnderscoresAndDashes()) + : names?.Contains(d.Name.LocalName.RemoveUnderscoresAndDashes()) == true ); } } diff --git a/src/RestSharp/Serializers/Xml/XmlRestSerializer.cs b/src/RestSharp/Serializers/Xml/XmlRestSerializer.cs index 769b053b2..90fa53977 100644 --- a/src/RestSharp/Serializers/Xml/XmlRestSerializer.cs +++ b/src/RestSharp/Serializers/Xml/XmlRestSerializer.cs @@ -25,19 +25,20 @@ public class XmlRestSerializer : IRestSerializer, IXmlSerializer, IXmlDeserializ XmlSerilizationOptions _options = XmlSerilizationOptions.Default; IXmlDeserializer _xmlDeserializer = new XmlDeserializer(); IXmlSerializer _xmlSerializer = new XmlSerializer(); - public string[] SupportedContentTypes { get; } = Serialization.ContentType.XmlAccept; + + public string[] SupportedContentTypes => Serialization.ContentType.XmlAccept; - public DataFormat DataFormat { get; } = DataFormat.Xml; + public DataFormat DataFormat => DataFormat.Xml; public string ContentType { get; set; } = Serialization.ContentType.Xml; - public string Serialize(object obj) => _xmlSerializer.Serialize(obj); + public string? Serialize(object? obj) => _xmlSerializer.Serialize(obj); - public T Deserialize(IRestResponse response) => _xmlDeserializer.Deserialize(response); + public T? Deserialize(IRestResponse response) => _xmlDeserializer.Deserialize(response); - public string Serialize(Parameter parameter) + public string? Serialize(Parameter parameter) { - if (!(parameter is XmlParameter xmlParameter)) + if (parameter is not XmlParameter xmlParameter) throw new InvalidOperationException("Supplied parameter is not an XML parameter"); var savedNamespace = _xmlSerializer.Namespace; @@ -50,7 +51,7 @@ public string Serialize(Parameter parameter) return result; } - public string RootElement + public string? RootElement { get => _options.RootElement; set diff --git a/src/RestSharp/Serializers/Xml/XmlSerializer.cs b/src/RestSharp/Serializers/Xml/XmlSerializer.cs index 56b926c80..f155c06fa 100644 --- a/src/RestSharp/Serializers/Xml/XmlSerializer.cs +++ b/src/RestSharp/Serializers/Xml/XmlSerializer.cs @@ -98,7 +98,7 @@ public string Serialize(object obj) /// /// Name of the root element to use when serializing /// - public string RootElement { get; set; } + public string? RootElement { get; set; } /// /// XML namespace to use when serializing