From 2db88c47ac27b724be7319f73fe562a0c359ca83 Mon Sep 17 00:00:00 2001 From: Alexey Zimarev Date: Wed, 15 Dec 2021 12:43:25 +0100 Subject: [PATCH 1/5] Make onBefore and onAfter request functions async so it's possible to read the content, etc --- src/RestSharp/Request/RestRequest.cs | 7 +++-- src/RestSharp/RestClient.Async.cs | 6 +++-- .../HttpHeadersTests.cs | 5 +++- .../MultipartFormDataTests.cs | 26 ++++++++++++++----- .../RequestBodyTests.cs | 16 +++++++++--- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/RestSharp/Request/RestRequest.cs b/src/RestSharp/Request/RestRequest.cs index 6217c4ed2..5cd918d48 100644 --- a/src/RestSharp/Request/RestRequest.cs +++ b/src/RestSharp/Request/RestRequest.cs @@ -1,5 +1,4 @@ -using System.Text.RegularExpressions; -using RestSharp.Extensions; +using RestSharp.Extensions; namespace RestSharp; @@ -130,12 +129,12 @@ public RestRequest(Uri resource, Method method = Method.Get, DataFormat dataForm /// /// When supplied, the function will be called before making a request /// - public Action? OnBeforeRequest { get; set; } + public Func? OnBeforeRequest { get; set; } /// /// When supplied, the function will be called after the request is complete /// - public Action? OnAfterRequest { get; set; } + public Func? OnAfterRequest { get; set; } internal void IncreaseNumAttempts() => Attempts++; diff --git a/src/RestSharp/RestClient.Async.cs b/src/RestSharp/RestClient.Async.cs index 508e1831b..db80efae4 100644 --- a/src/RestSharp/RestClient.Async.cs +++ b/src/RestSharp/RestClient.Async.cs @@ -52,11 +52,13 @@ async Task ExecuteInternal(RestRequest request, CancellationTo .AddAcceptHeader(this); message.AddHeaders(parameters.Parameters); - request.OnBeforeRequest?.Invoke(message); + if (request.OnBeforeRequest != null) + await request.OnBeforeRequest(message); var responseMessage = await HttpClient.SendAsync(message, ct); - request.OnAfterRequest?.Invoke(responseMessage); + if (request.OnAfterRequest != null) + await request.OnAfterRequest(responseMessage); return new InternalResponse(responseMessage, url, null, timeoutCts.Token); } diff --git a/test/RestSharp.IntegrationTests/HttpHeadersTests.cs b/test/RestSharp.IntegrationTests/HttpHeadersTests.cs index 4311ad2e3..e829f0d54 100644 --- a/test/RestSharp.IntegrationTests/HttpHeadersTests.cs +++ b/test/RestSharp.IntegrationTests/HttpHeadersTests.cs @@ -19,7 +19,10 @@ public async Task Ensure_headers_correctly_set_in_the_hook() { var client = new RestClient(server.Url); var request = new RestRequest(RequestHeadCapturer.Resource) { - OnBeforeRequest = http => http.Headers.Add(headerName, headerValue) + OnBeforeRequest = http => { + http.Headers.Add(headerName, headerValue); + return default; + } }; // Run diff --git a/test/RestSharp.IntegrationTests/MultipartFormDataTests.cs b/test/RestSharp.IntegrationTests/MultipartFormDataTests.cs index 6a5c79b0d..3ff81e742 100644 --- a/test/RestSharp.IntegrationTests/MultipartFormDataTests.cs +++ b/test/RestSharp.IntegrationTests/MultipartFormDataTests.cs @@ -1,5 +1,4 @@ using System.Net; -using System.Net.Http.Headers; using RestSharp.Tests.Shared.Fixtures; namespace RestSharp.IntegrationTests; @@ -80,7 +79,10 @@ public async Task MultipartFormData() { AddParameters(request); string boundary = null; - request.OnBeforeRequest += http => boundary = http.Content!.GetFormBoundary(); + request.OnBeforeRequest = http => { + boundary = http.Content!.GetFormBoundary(); + return default; + }; var response = await _client.ExecuteAsync(request); @@ -102,7 +104,10 @@ public async Task MultipartFormData_HasDefaultContentType() { request.AddParameter("controlName", "test", "application/json", ParameterType.RequestBody); string boundary = null; - request.OnBeforeRequest = http => boundary = http.Content!.GetFormBoundary(); + request.OnBeforeRequest = http => { + boundary = http.Content!.GetFormBoundary(); + return default; + }; var response = await _client.ExecuteAsync(request); @@ -128,7 +133,10 @@ public async Task MultipartFormData_WithCustomContentType() { request.AddParameter("controlName", "test", "application/json", ParameterType.RequestBody); string boundary = null; - request.OnBeforeRequest = http => boundary = http.Content!.GetFormBoundary(); + request.OnBeforeRequest = http => { + boundary = http.Content!.GetFormBoundary(); + return default; + }; var response = await _client.ExecuteAsync(request); @@ -151,7 +159,10 @@ public async Task MultipartFormData_WithParameterAndFile_Async() { request.AddParameter("controlName", "test", "application/json", ParameterType.RequestBody); string boundary = null; - request.OnBeforeRequest = http => boundary = http.Content!.GetFormBoundary(); + request.OnBeforeRequest = http => { + boundary = http.Content!.GetFormBoundary(); + return default; + }; var response = await _client.ExecuteAsync(request); @@ -168,7 +179,10 @@ public async Task MultipartFormDataAsync() { string boundary = null; - request.OnBeforeRequest = http => boundary = http.Content!.GetFormBoundary(); + request.OnBeforeRequest = http => { + boundary = http.Content!.GetFormBoundary(); + return default; + }; var response = await _client.ExecuteAsync(request); var expected = string.Format(Expected, boundary); diff --git a/test/RestSharp.IntegrationTests/RequestBodyTests.cs b/test/RestSharp.IntegrationTests/RequestBodyTests.cs index 4c408c461..a3cc75074 100644 --- a/test/RestSharp.IntegrationTests/RequestBodyTests.cs +++ b/test/RestSharp.IntegrationTests/RequestBodyTests.cs @@ -4,6 +4,7 @@ namespace RestSharp.IntegrationTests; public class RequestBodyTests : IClassFixture { + readonly ITestOutputHelper _output; readonly SimpleServer _server; const string NewLine = "\r\n"; @@ -11,11 +12,20 @@ public class RequestBodyTests : IClassFixture { const string TextPlainContentType = "text/plain"; const string ExpectedTextContentType = $"{TextPlainContentType}; charset=utf-8"; - public RequestBodyTests(RequestBodyFixture fixture) => _server = fixture.Server; + public RequestBodyTests(RequestBodyFixture fixture, ITestOutputHelper output) { + _output = output; + _server = fixture.Server; + } async Task AssertBody(Method method) { - var client = new RestClient(_server.Url); - var request = new RestRequest(RequestBodyCapturer.Resource, method); + var client = new RestClient(_server.Url); + + var request = new RestRequest(RequestBodyCapturer.Resource, method) { + OnBeforeRequest = async m => { + _output.WriteLine(m.ToString()); + _output.WriteLine(await m.Content!.ReadAsStringAsync()); + } + }; const string bodyData = "abc123 foo bar baz BING!"; From 2ea6528d2e21c6e7e6912b0b0ed7fd870dd569fb Mon Sep 17 00:00:00 2001 From: Alexey Zimarev Date: Wed, 15 Dec 2021 12:59:22 +0100 Subject: [PATCH 2/5] Added an option to configure the message handler --- src/RestSharp/RestClient.cs | 6 ++++-- src/RestSharp/RestClientOptions.cs | 4 +++- .../Fixtures/OutputLogger.cs | 11 +++++++++++ .../MultipartFormDataTests.cs | 10 ++++++++-- .../RestSharp.IntegrationTests.csproj | 1 + 5 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 test/RestSharp.IntegrationTests/Fixtures/OutputLogger.cs diff --git a/src/RestSharp/RestClient.cs b/src/RestSharp/RestClient.cs index 7f3198313..220a9912d 100644 --- a/src/RestSharp/RestClient.cs +++ b/src/RestSharp/RestClient.cs @@ -57,7 +57,7 @@ public RestClient(RestClientOptions options) { AutomaticDecompression = Options.AutomaticDecompression, PreAuthenticate = Options.PreAuthenticate, AllowAutoRedirect = Options.FollowRedirects, - Proxy = Options.Proxy, + Proxy = Options.Proxy }; if (Options.RemoteCertificateValidationCallback != null) @@ -70,7 +70,9 @@ public RestClient(RestClientOptions options) { if (Options.MaxRedirects.HasValue) handler.MaxAutomaticRedirections = Options.MaxRedirects.Value; - HttpClient = new HttpClient(handler); + var finalHandler = Options.ConfigureMessageHandler?.Invoke(handler) ?? handler; + + HttpClient = new HttpClient(finalHandler); if (Options.Timeout > 0) HttpClient.Timeout = TimeSpan.FromMilliseconds(Options.Timeout); diff --git a/src/RestSharp/RestClientOptions.cs b/src/RestSharp/RestClientOptions.cs index 1f4283b02..89cd47f25 100644 --- a/src/RestSharp/RestClientOptions.cs +++ b/src/RestSharp/RestClientOptions.cs @@ -38,7 +38,9 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba /// If null, default host value extracted from URI is used. /// public Uri? BaseUrl { get; set; } - + + public Func? ConfigureMessageHandler { get; set; } + /// /// In general you would not need to set this directly. Used by the NtlmAuthenticator. /// diff --git a/test/RestSharp.IntegrationTests/Fixtures/OutputLogger.cs b/test/RestSharp.IntegrationTests/Fixtures/OutputLogger.cs new file mode 100644 index 000000000..69f868862 --- /dev/null +++ b/test/RestSharp.IntegrationTests/Fixtures/OutputLogger.cs @@ -0,0 +1,11 @@ +using HttpTracer.Logger; + +namespace RestSharp.IntegrationTests.Fixtures; + +public class OutputLogger : ILogger{ + readonly ITestOutputHelper _output; + + public OutputLogger(ITestOutputHelper output) => _output = output; + + public void Log(string message) => _output.WriteLine(message); +} \ No newline at end of file diff --git a/test/RestSharp.IntegrationTests/MultipartFormDataTests.cs b/test/RestSharp.IntegrationTests/MultipartFormDataTests.cs index 3ff81e742..c398d3c3e 100644 --- a/test/RestSharp.IntegrationTests/MultipartFormDataTests.cs +++ b/test/RestSharp.IntegrationTests/MultipartFormDataTests.cs @@ -1,15 +1,21 @@ using System.Net; +using HttpTracer; +using RestSharp.IntegrationTests.Fixtures; using RestSharp.Tests.Shared.Fixtures; namespace RestSharp.IntegrationTests; -public class MultipartFormDataTests : IDisposable { +public sealed class MultipartFormDataTests : IDisposable { readonly ITestOutputHelper _output; public MultipartFormDataTests(ITestOutputHelper output) { _output = output; _server = SimpleServer.Create(RequestHandler.Handle); - _client = new RestClient(_server.Url); + + var options = new RestClientOptions(_server.Url) { + ConfigureMessageHandler = handler => new HttpTracerHandler(handler, new OutputLogger(output), HttpMessageParts.All) + }; + _client = new RestClient(options); } public void Dispose() => _server.Dispose(); diff --git a/test/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj b/test/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj index 514299d87..258a499a3 100644 --- a/test/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj +++ b/test/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj @@ -12,6 +12,7 @@ + From b88d58e2ab99aec8596cad04892a146aaffd0930 Mon Sep 17 00:00:00 2001 From: Alexey Zimarev Date: Fri, 17 Dec 2021 15:25:29 +0100 Subject: [PATCH 3/5] Per-request serializers --- RestSharp.sln.DotSettings | 1 + .../JsonNetSerializer.cs | 24 ++++- .../RestClientExtensions.cs | 14 +++ .../WriterBuffer.cs | 14 +++ .../DeserializeAsAttribute.cs | 16 ++- .../SerializeAsAttribute.cs | 16 ++- .../XmlAttributeDeserializer.cs | 16 ++- .../XmlDeserializer.cs | 16 ++- .../XmlExtensions.cs | 16 ++- .../XmlSerializer.cs | 18 +++- .../XmlSerializerClientExtensions.cs | 2 +- .../Authenticators/AuthenticatorBase.cs | 14 +++ .../Authenticators/HttpBasicAuthenticator.cs | 21 +++- .../Authenticators/IAuthenticator.cs | 16 ++- .../Authenticators/JwtAuthenticator.cs | 14 +++ src/RestSharp/Authenticators/OAuth/Enums.cs | 14 +++ .../OAuth/Extensions/OAuthExtensions.cs | 14 +++ .../OAuth/Extensions/StringExtensions.cs | 19 +++- .../OAuth/Extensions/TimeExtensions.cs | 14 +++ .../OAuth/OAuth1Authenticator.cs | 16 ++- .../Authenticators/OAuth/OAuthTools.cs | 42 ++++---- .../Authenticators/OAuth/OAuthWorkflow.cs | 14 +++ src/RestSharp/Authenticators/OAuth/WebPair.cs | 4 +- .../Authenticators/OAuth/WebPairCollection.cs | 14 +++ ...AuthorizationRequestHeaderAuthenticator.cs | 14 +++ .../OAuth2UriQueryParameterAuthenticator.cs | 21 +++- src/RestSharp/Ensure.cs | 14 ++- src/RestSharp/Enum.cs | 16 ++- .../Extensions/CollectionExtensions.cs | 14 +++ src/RestSharp/Extensions/MiscExtensions.cs | 14 ++- .../Extensions/ReflectionExtensions.cs | 16 ++- .../Extensions/ResponseStatusExtensions.cs | 16 ++- src/RestSharp/Extensions/StreamExtensions.cs | 17 --- src/RestSharp/Extensions/StringExtensions.cs | 16 ++- src/RestSharp/Extensions/WithExtensions.cs | 14 +++ src/RestSharp/KnownHeaders.cs | 2 +- src/RestSharp/NameValuePair.cs | 14 +++ src/RestSharp/Parameters/FileParameter.cs | 2 +- src/RestSharp/Parameters/Parameter.cs | 2 +- src/RestSharp/Request/BodyExtensions.cs | 2 +- .../Request/HttpContentExtensions.cs | 2 +- .../Request/HttpRequestMessageExtensions.cs | 2 +- .../Request/InvalidRequestException.cs | 2 +- src/RestSharp/Request/RequestContent.cs | 11 +- src/RestSharp/Request/RequestParameters.cs | 2 +- src/RestSharp/Request/RestRequest.cs | 16 ++- .../Request/RestRequestExtensions.cs | 14 +++ src/RestSharp/Request/RestXmlRequest.cs | 2 +- src/RestSharp/Response/RestResponse.cs | 16 ++- src/RestSharp/Response/RestResponseBase.cs | 3 +- src/RestSharp/RestClient.Async.cs | 16 ++- src/RestSharp/RestClient.cs | 27 ++++- src/RestSharp/RestClientExtensions.Json.cs | 2 +- src/RestSharp/RestClientExtensions.Params.cs | 2 +- src/RestSharp/RestClientExtensions.cs | 2 +- src/RestSharp/RestClientOptions.cs | 2 +- src/RestSharp/Serializers/ContentType.cs | 16 ++- .../Serializers/DeseralizationException.cs | 14 +++ src/RestSharp/Serializers/IDeserializer.cs | 16 ++- src/RestSharp/Serializers/IRestSerializer.cs | 19 +++- src/RestSharp/Serializers/ISerializer.cs | 16 ++- src/RestSharp/Serializers/IWithDateFormat.cs | 19 ++++ src/RestSharp/Serializers/IWithRootElement.cs | 22 +++- .../Serializers/Json/RestClientExtensions.cs | 14 +++ .../Json/SystemTextJsonSerializer.cs | 20 +++- src/RestSharp/Serializers/SerializerRecord.cs | 17 +++ .../Serializers/Xml/DotNetXmlDeserializer.cs | 16 ++- .../Serializers/Xml/DotNetXmlSerializer.cs | 16 ++- .../DotNetXmlSerializerClientExtensions.cs | 3 +- .../Serializers/Xml/IXmlDeserializer.cs | 20 +++- .../Serializers/Xml/IXmlSerializer.cs | 20 +++- .../Serializers/Xml/XmlRestSerializer.cs | 100 ++---------------- .../RootElementTests.cs | 12 +-- .../SampleDeserializers/CustomDeserializer.cs | 11 -- 74 files changed, 825 insertions(+), 230 deletions(-) delete mode 100644 src/RestSharp/Extensions/StreamExtensions.cs create mode 100644 src/RestSharp/Serializers/IWithDateFormat.cs create mode 100644 src/RestSharp/Serializers/SerializerRecord.cs delete mode 100644 test/RestSharp.IntegrationTests/SampleDeserializers/CustomDeserializer.cs diff --git a/RestSharp.sln.DotSettings b/RestSharp.sln.DotSettings index 4996b4954..3372f4ecd 100644 --- a/RestSharp.sln.DotSettings +++ b/RestSharp.sln.DotSettings @@ -98,4 +98,5 @@ 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. + True \ No newline at end of file diff --git a/src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs b/src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs index 067db9377..bcb09818e 100644 --- a/src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs +++ b/src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs @@ -1,8 +1,22 @@ +// Copyright © 2009-2021 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. + using Newtonsoft.Json.Serialization; -namespace RestSharp.Serializers.NewtonsoftJson; +namespace RestSharp.Serializers.NewtonsoftJson; -public class JsonNetSerializer : IRestSerializer { +public class JsonNetSerializer : IRestSerializer, ISerializer, IDeserializer { /// /// Default serialization settings: /// - Camel-case contract resolver @@ -37,7 +51,7 @@ public class JsonNetSerializer : IRestSerializer { public string? Serialize(object? obj) { if (obj == null) return null; - + using var writerBuffer = _writerBuffer ??= new WriterBuffer(_serializer); _serializer.Serialize(writerBuffer.GetJsonTextWriter(), obj, obj.GetType()); @@ -50,11 +64,15 @@ public class JsonNetSerializer : IRestSerializer { public T? Deserialize(RestResponse response) { if (response.Content == null) throw new DeserializationException(response, new InvalidOperationException("Response content is null")); + using var reader = new JsonTextReader(new StringReader(response.Content)) { CloseInput = true }; return _serializer.Deserialize(reader); } + public ISerializer Serializer => this; + public IDeserializer Deserializer => this; + public string[] SupportedContentTypes { get; } = { "application/json", "text/json", "text/x-json", "text/javascript", "*+json" }; diff --git a/src/RestSharp.Serializers.NewtonsoftJson/RestClientExtensions.cs b/src/RestSharp.Serializers.NewtonsoftJson/RestClientExtensions.cs index d9565a40e..6f2484ba6 100644 --- a/src/RestSharp.Serializers.NewtonsoftJson/RestClientExtensions.cs +++ b/src/RestSharp.Serializers.NewtonsoftJson/RestClientExtensions.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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.Serializers.NewtonsoftJson; [PublicAPI] diff --git a/src/RestSharp.Serializers.NewtonsoftJson/WriterBuffer.cs b/src/RestSharp.Serializers.NewtonsoftJson/WriterBuffer.cs index bff3d4c4e..520e4e56e 100644 --- a/src/RestSharp.Serializers.NewtonsoftJson/WriterBuffer.cs +++ b/src/RestSharp.Serializers.NewtonsoftJson/WriterBuffer.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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. + using System.Globalization; using System.Text; diff --git a/src/RestSharp.Serializers.Xml/DeserializeAsAttribute.cs b/src/RestSharp.Serializers.Xml/DeserializeAsAttribute.cs index 29d2d486b..23e1f16d2 100644 --- a/src/RestSharp.Serializers.Xml/DeserializeAsAttribute.cs +++ b/src/RestSharp.Serializers.Xml/DeserializeAsAttribute.cs @@ -1,4 +1,18 @@ -// ReSharper disable once CheckNamespace +// Copyright © 2009-2021 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. + +// ReSharper disable once CheckNamespace namespace RestSharp.Serializers; /// diff --git a/src/RestSharp.Serializers.Xml/SerializeAsAttribute.cs b/src/RestSharp.Serializers.Xml/SerializeAsAttribute.cs index 28b330784..fc7838df4 100644 --- a/src/RestSharp.Serializers.Xml/SerializeAsAttribute.cs +++ b/src/RestSharp.Serializers.Xml/SerializeAsAttribute.cs @@ -1,4 +1,18 @@ -using System.Globalization; +// Copyright © 2009-2021 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. + +using System.Globalization; using RestSharp.Extensions; // ReSharper disable once CheckNamespace diff --git a/src/RestSharp.Serializers.Xml/XmlAttributeDeserializer.cs b/src/RestSharp.Serializers.Xml/XmlAttributeDeserializer.cs index a314bdb25..2c25baaaf 100644 --- a/src/RestSharp.Serializers.Xml/XmlAttributeDeserializer.cs +++ b/src/RestSharp.Serializers.Xml/XmlAttributeDeserializer.cs @@ -1,4 +1,18 @@ -using System.Reflection; +// Copyright © 2009-2021 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. + +using System.Reflection; using System.Xml.Linq; using RestSharp.Extensions; diff --git a/src/RestSharp.Serializers.Xml/XmlDeserializer.cs b/src/RestSharp.Serializers.Xml/XmlDeserializer.cs index b59ff4b91..e0ef92e0b 100644 --- a/src/RestSharp.Serializers.Xml/XmlDeserializer.cs +++ b/src/RestSharp.Serializers.Xml/XmlDeserializer.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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. + using System.Collections; using System.ComponentModel; using System.Globalization; @@ -8,7 +22,7 @@ namespace RestSharp.Serializers.Xml; -public class XmlDeserializer : IXmlDeserializer { +public class XmlDeserializer : IXmlDeserializer, IWithRootElement, IWithDateFormat { public XmlDeserializer() => Culture = CultureInfo.InvariantCulture; public CultureInfo Culture { get; set; } diff --git a/src/RestSharp.Serializers.Xml/XmlExtensions.cs b/src/RestSharp.Serializers.Xml/XmlExtensions.cs index 1834a2165..c4c694645 100644 --- a/src/RestSharp.Serializers.Xml/XmlExtensions.cs +++ b/src/RestSharp.Serializers.Xml/XmlExtensions.cs @@ -1,4 +1,18 @@ -using System.Xml.Linq; +// Copyright © 2009-2021 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. + +using System.Xml.Linq; using RestSharp.Extensions; namespace RestSharp.Serializers.Xml; diff --git a/src/RestSharp.Serializers.Xml/XmlSerializer.cs b/src/RestSharp.Serializers.Xml/XmlSerializer.cs index fdf27ea2f..c5f6c29e7 100644 --- a/src/RestSharp.Serializers.Xml/XmlSerializer.cs +++ b/src/RestSharp.Serializers.Xml/XmlSerializer.cs @@ -1,4 +1,18 @@ -using System.Collections; +// Copyright © 2009-2021 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. + +using System.Collections; using System.Globalization; using System.Reflection; using System.Xml.Linq; @@ -9,7 +23,7 @@ namespace RestSharp.Serializers.Xml; /// /// Default XML Serializer /// -public class XmlSerializer : IXmlSerializer { +public class XmlSerializer : IXmlSerializer, IWithRootElement, IWithDateFormat { /// /// Default constructor, does not specify namespace /// diff --git a/src/RestSharp.Serializers.Xml/XmlSerializerClientExtensions.cs b/src/RestSharp.Serializers.Xml/XmlSerializerClientExtensions.cs index c0ec75bcf..4e4898f11 100644 --- a/src/RestSharp.Serializers.Xml/XmlSerializerClientExtensions.cs +++ b/src/RestSharp.Serializers.Xml/XmlSerializerClientExtensions.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/Authenticators/AuthenticatorBase.cs b/src/RestSharp/Authenticators/AuthenticatorBase.cs index fcee00edf..f0847c1a7 100644 --- a/src/RestSharp/Authenticators/AuthenticatorBase.cs +++ b/src/RestSharp/Authenticators/AuthenticatorBase.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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.Authenticators; public abstract class AuthenticatorBase : IAuthenticator { diff --git a/src/RestSharp/Authenticators/HttpBasicAuthenticator.cs b/src/RestSharp/Authenticators/HttpBasicAuthenticator.cs index ee88020ca..964acbba6 100644 --- a/src/RestSharp/Authenticators/HttpBasicAuthenticator.cs +++ b/src/RestSharp/Authenticators/HttpBasicAuthenticator.cs @@ -1,6 +1,20 @@ -using System.Text; +// Copyright © 2009-2021 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.Authenticators; +using System.Text; + +namespace RestSharp.Authenticators; /// /// Allows "basic access authentication" for HTTP requests. @@ -10,8 +24,7 @@ namespace RestSharp.Authenticators; /// UTF-8 is used by default but some servers might expect ISO-8859-1 encoding. /// [PublicAPI] -public class HttpBasicAuthenticator : AuthenticatorBase -{ +public class HttpBasicAuthenticator : AuthenticatorBase { public HttpBasicAuthenticator(string username, string password) : this(username, password, Encoding.UTF8) { } public HttpBasicAuthenticator(string username, string password, Encoding encoding) diff --git a/src/RestSharp/Authenticators/IAuthenticator.cs b/src/RestSharp/Authenticators/IAuthenticator.cs index d4e84e08f..5d8799893 100644 --- a/src/RestSharp/Authenticators/IAuthenticator.cs +++ b/src/RestSharp/Authenticators/IAuthenticator.cs @@ -1,4 +1,18 @@ -namespace RestSharp.Authenticators; +// Copyright © 2009-2021 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.Authenticators; public interface IAuthenticator { ValueTask Authenticate(RestClient client, RestRequest request); diff --git a/src/RestSharp/Authenticators/JwtAuthenticator.cs b/src/RestSharp/Authenticators/JwtAuthenticator.cs index 5005507ab..815973d9d 100644 --- a/src/RestSharp/Authenticators/JwtAuthenticator.cs +++ b/src/RestSharp/Authenticators/JwtAuthenticator.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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.Authenticators; /// diff --git a/src/RestSharp/Authenticators/OAuth/Enums.cs b/src/RestSharp/Authenticators/OAuth/Enums.cs index 31c675c03..16e4e9ea8 100644 --- a/src/RestSharp/Authenticators/OAuth/Enums.cs +++ b/src/RestSharp/Authenticators/OAuth/Enums.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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.Authenticators.OAuth; public enum OAuthSignatureMethod { HmacSha1, HmacSha256, PlainText, RsaSha1 } diff --git a/src/RestSharp/Authenticators/OAuth/Extensions/OAuthExtensions.cs b/src/RestSharp/Authenticators/OAuth/Extensions/OAuthExtensions.cs index 1b537f807..37cd5434a 100644 --- a/src/RestSharp/Authenticators/OAuth/Extensions/OAuthExtensions.cs +++ b/src/RestSharp/Authenticators/OAuth/Extensions/OAuthExtensions.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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. + using System.Security.Cryptography; using System.Text; diff --git a/src/RestSharp/Authenticators/OAuth/Extensions/StringExtensions.cs b/src/RestSharp/Authenticators/OAuth/Extensions/StringExtensions.cs index ee6f28897..c7be008e6 100644 --- a/src/RestSharp/Authenticators/OAuth/Extensions/StringExtensions.cs +++ b/src/RestSharp/Authenticators/OAuth/Extensions/StringExtensions.cs @@ -1,9 +1,22 @@ +// Copyright © 2009-2021 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. + using System.Text; -namespace RestSharp.Authenticators.OAuth.Extensions; +namespace RestSharp.Authenticators.OAuth.Extensions; -static class StringExtensions -{ +static class StringExtensions { public static bool EqualsIgnoreCase(this string left, string right) => string.Equals(left, right, StringComparison.InvariantCultureIgnoreCase); public static string Then(this string input, string value) => string.Concat(input, value); diff --git a/src/RestSharp/Authenticators/OAuth/Extensions/TimeExtensions.cs b/src/RestSharp/Authenticators/OAuth/Extensions/TimeExtensions.cs index b67df54c2..a98c57cd5 100644 --- a/src/RestSharp/Authenticators/OAuth/Extensions/TimeExtensions.cs +++ b/src/RestSharp/Authenticators/OAuth/Extensions/TimeExtensions.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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.Authenticators.OAuth.Extensions; static class TimeExtensions { diff --git a/src/RestSharp/Authenticators/OAuth/OAuth1Authenticator.cs b/src/RestSharp/Authenticators/OAuth/OAuth1Authenticator.cs index 09eb301bb..8a6706912 100644 --- a/src/RestSharp/Authenticators/OAuth/OAuth1Authenticator.cs +++ b/src/RestSharp/Authenticators/OAuth/OAuth1Authenticator.cs @@ -1,4 +1,18 @@ -using RestSharp.Authenticators.OAuth; +// Copyright © 2009-2021 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. + +using RestSharp.Authenticators.OAuth; using RestSharp.Extensions; using System.Web; diff --git a/src/RestSharp/Authenticators/OAuth/OAuthTools.cs b/src/RestSharp/Authenticators/OAuth/OAuthTools.cs index 10dffeef6..cb7f55984 100644 --- a/src/RestSharp/Authenticators/OAuth/OAuthTools.cs +++ b/src/RestSharp/Authenticators/OAuth/OAuthTools.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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. + using System.Security.Cryptography; using System.Text; using RestSharp.Authenticators.OAuth.Extensions; @@ -8,25 +22,15 @@ namespace RestSharp.Authenticators.OAuth; static class OAuthTools { const string AlphaNumeric = Upper + Lower + Digit; - - const string Digit = "1234567890"; - - const string Lower = "abcdefghijklmnopqrstuvwxyz"; - - const string Unreserved = AlphaNumeric + "-._~"; - - const string Upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - - static readonly Random Random; - - static readonly object RandomLock = new(); - - static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create(); - - /// - /// All text parameters are UTF-8 encoded (per section 5.1). - /// - static readonly Encoding Encoding = Encoding.UTF8; + const string Digit = "1234567890"; + const string Lower = "abcdefghijklmnopqrstuvwxyz"; + const string Unreserved = AlphaNumeric + "-._~"; + const string Upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + static readonly Random Random; + static readonly object RandomLock = new(); + static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create(); + static readonly Encoding Encoding = Encoding.UTF8; /// /// The set of characters that are unreserved in RFC 2396 but are NOT unreserved in RFC 3986. diff --git a/src/RestSharp/Authenticators/OAuth/OAuthWorkflow.cs b/src/RestSharp/Authenticators/OAuth/OAuthWorkflow.cs index 420709c4d..97e61e839 100644 --- a/src/RestSharp/Authenticators/OAuth/OAuthWorkflow.cs +++ b/src/RestSharp/Authenticators/OAuth/OAuthWorkflow.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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. + using System.Web; using RestSharp.Authenticators.OAuth.Extensions; using RestSharp.Extensions; diff --git a/src/RestSharp/Authenticators/OAuth/WebPair.cs b/src/RestSharp/Authenticators/OAuth/WebPair.cs index a71155860..b1aedd53c 100644 --- a/src/RestSharp/Authenticators/OAuth/WebPair.cs +++ b/src/RestSharp/Authenticators/OAuth/WebPair.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System.Collections.Generic; - namespace RestSharp.Authenticators.OAuth; class WebPair { diff --git a/src/RestSharp/Authenticators/OAuth/WebPairCollection.cs b/src/RestSharp/Authenticators/OAuth/WebPairCollection.cs index 5e1e7d05f..398774efc 100644 --- a/src/RestSharp/Authenticators/OAuth/WebPairCollection.cs +++ b/src/RestSharp/Authenticators/OAuth/WebPairCollection.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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. + using System.Collections; namespace RestSharp.Authenticators.OAuth; diff --git a/src/RestSharp/Authenticators/OAuth2/OAuth2AuthorizationRequestHeaderAuthenticator.cs b/src/RestSharp/Authenticators/OAuth2/OAuth2AuthorizationRequestHeaderAuthenticator.cs index fc4cadbc2..d05c3269a 100644 --- a/src/RestSharp/Authenticators/OAuth2/OAuth2AuthorizationRequestHeaderAuthenticator.cs +++ b/src/RestSharp/Authenticators/OAuth2/OAuth2AuthorizationRequestHeaderAuthenticator.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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.Authenticators.OAuth2; /// diff --git a/src/RestSharp/Authenticators/OAuth2/OAuth2UriQueryParameterAuthenticator.cs b/src/RestSharp/Authenticators/OAuth2/OAuth2UriQueryParameterAuthenticator.cs index 5cfebba73..b9f407630 100644 --- a/src/RestSharp/Authenticators/OAuth2/OAuth2UriQueryParameterAuthenticator.cs +++ b/src/RestSharp/Authenticators/OAuth2/OAuth2UriQueryParameterAuthenticator.cs @@ -1,4 +1,18 @@ -namespace RestSharp.Authenticators.OAuth2; +// Copyright © 2009-2021 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.Authenticators.OAuth2; /// /// The OAuth 2 authenticator using URI query parameter. @@ -6,14 +20,13 @@ namespace RestSharp.Authenticators.OAuth2; /// /// Based on http://tools.ietf.org/html/draft-ietf-oauth-v2-10#section-5.1.2 /// -public class OAuth2UriQueryParameterAuthenticator : AuthenticatorBase -{ +public class OAuth2UriQueryParameterAuthenticator : AuthenticatorBase { /// /// Initializes a new instance of the class. /// /// The access token. public OAuth2UriQueryParameterAuthenticator(string accessToken) : base(accessToken) { } - protected override ValueTask GetAuthenticationParameter(string accessToken) + protected override ValueTask GetAuthenticationParameter(string accessToken) => new(new Parameter("oauth_token", accessToken, ParameterType.GetOrPost)); } \ No newline at end of file diff --git a/src/RestSharp/Ensure.cs b/src/RestSharp/Ensure.cs index 40fbd441e..277a82169 100644 --- a/src/RestSharp/Ensure.cs +++ b/src/RestSharp/Ensure.cs @@ -1,4 +1,16 @@ -using System.Runtime.CompilerServices; +// Copyright © 2009-2021 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; diff --git a/src/RestSharp/Enum.cs b/src/RestSharp/Enum.cs index 8669e12b9..6d21aee70 100644 --- a/src/RestSharp/Enum.cs +++ b/src/RestSharp/Enum.cs @@ -1,4 +1,18 @@ -namespace RestSharp; +// Copyright © 2009-2021 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; /// /// Types of parameters that can be added to requests diff --git a/src/RestSharp/Extensions/CollectionExtensions.cs b/src/RestSharp/Extensions/CollectionExtensions.cs index 97e875fb2..9e3f8df00 100644 --- a/src/RestSharp/Extensions/CollectionExtensions.cs +++ b/src/RestSharp/Extensions/CollectionExtensions.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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.Extensions; static class CollectionExtensions { diff --git a/src/RestSharp/Extensions/MiscExtensions.cs b/src/RestSharp/Extensions/MiscExtensions.cs index c596e5316..a6e94ff22 100644 --- a/src/RestSharp/Extensions/MiscExtensions.cs +++ b/src/RestSharp/Extensions/MiscExtensions.cs @@ -1,4 +1,16 @@ -using System; +// Copyright © 2009-2021 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.Extensions; diff --git a/src/RestSharp/Extensions/ReflectionExtensions.cs b/src/RestSharp/Extensions/ReflectionExtensions.cs index 6d71f898c..af24183e9 100644 --- a/src/RestSharp/Extensions/ReflectionExtensions.cs +++ b/src/RestSharp/Extensions/ReflectionExtensions.cs @@ -1,4 +1,18 @@ -using System.Globalization; +// Copyright © 2009-2021 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. + +using System.Globalization; using System.Reflection; namespace RestSharp.Extensions; diff --git a/src/RestSharp/Extensions/ResponseStatusExtensions.cs b/src/RestSharp/Extensions/ResponseStatusExtensions.cs index 27f91cdcb..e6a8992b9 100644 --- a/src/RestSharp/Extensions/ResponseStatusExtensions.cs +++ b/src/RestSharp/Extensions/ResponseStatusExtensions.cs @@ -1,4 +1,18 @@ -using System.Net; +// Copyright © 2009-2021 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. + +using System.Net; using static System.Net.WebExceptionStatus; namespace RestSharp.Extensions; diff --git a/src/RestSharp/Extensions/StreamExtensions.cs b/src/RestSharp/Extensions/StreamExtensions.cs deleted file mode 100644 index 1332dc1f5..000000000 --- a/src/RestSharp/Extensions/StreamExtensions.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Text; - -namespace RestSharp.Extensions; - -static class StreamExtensions { - public static void WriteString(this Stream stream, string value, Encoding encoding) { - var bytes = encoding.GetBytes(value); - - stream.Write(bytes, 0, bytes.Length); - } - - public static Task WriteStringAsync(this Stream stream, string value, Encoding encoding, CancellationToken cancellationToken) { - var bytes = encoding.GetBytes(value); - - return stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken); - } -} \ No newline at end of file diff --git a/src/RestSharp/Extensions/StringExtensions.cs b/src/RestSharp/Extensions/StringExtensions.cs index 8eec2a512..962beead3 100644 --- a/src/RestSharp/Extensions/StringExtensions.cs +++ b/src/RestSharp/Extensions/StringExtensions.cs @@ -1,4 +1,18 @@ -using System.Globalization; +// Copyright © 2009-2021 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. + +using System.Globalization; using System.Text; using System.Text.RegularExpressions; using System.Web; diff --git a/src/RestSharp/Extensions/WithExtensions.cs b/src/RestSharp/Extensions/WithExtensions.cs index e3f751b69..6efe56a1c 100644 --- a/src/RestSharp/Extensions/WithExtensions.cs +++ b/src/RestSharp/Extensions/WithExtensions.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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.Extensions; static class WithExtensions { diff --git a/src/RestSharp/KnownHeaders.cs b/src/RestSharp/KnownHeaders.cs index 9c9ff2e44..dbcc61224 100644 --- a/src/RestSharp/KnownHeaders.cs +++ b/src/RestSharp/KnownHeaders.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/NameValuePair.cs b/src/RestSharp/NameValuePair.cs index c2fd56a41..ae52c1054 100644 --- a/src/RestSharp/NameValuePair.cs +++ b/src/RestSharp/NameValuePair.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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; [PublicAPI] diff --git a/src/RestSharp/Parameters/FileParameter.cs b/src/RestSharp/Parameters/FileParameter.cs index 54e817742..2a337d0ec 100644 --- a/src/RestSharp/Parameters/FileParameter.cs +++ b/src/RestSharp/Parameters/FileParameter.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/Parameters/Parameter.cs b/src/RestSharp/Parameters/Parameter.cs index d11403830..9de5c8941 100644 --- a/src/RestSharp/Parameters/Parameter.cs +++ b/src/RestSharp/Parameters/Parameter.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/Request/BodyExtensions.cs b/src/RestSharp/Request/BodyExtensions.cs index da3278c66..38b149611 100644 --- a/src/RestSharp/Request/BodyExtensions.cs +++ b/src/RestSharp/Request/BodyExtensions.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/Request/HttpContentExtensions.cs b/src/RestSharp/Request/HttpContentExtensions.cs index a12edf8c6..0a9d7f6e2 100644 --- a/src/RestSharp/Request/HttpContentExtensions.cs +++ b/src/RestSharp/Request/HttpContentExtensions.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/Request/HttpRequestMessageExtensions.cs b/src/RestSharp/Request/HttpRequestMessageExtensions.cs index a2cce28f4..43d9b6b95 100644 --- a/src/RestSharp/Request/HttpRequestMessageExtensions.cs +++ b/src/RestSharp/Request/HttpRequestMessageExtensions.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/Request/InvalidRequestException.cs b/src/RestSharp/Request/InvalidRequestException.cs index 5c3294a52..27b5bd805 100644 --- a/src/RestSharp/Request/InvalidRequestException.cs +++ b/src/RestSharp/Request/InvalidRequestException.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/Request/RequestContent.cs b/src/RestSharp/Request/RequestContent.cs index 6906b3f9d..9fff38914 100644 --- a/src/RestSharp/Request/RequestContent.cs +++ b/src/RestSharp/Request/RequestContent.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. @@ -11,7 +11,6 @@ // 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. -// using System.Net.Http.Headers; using System.Runtime.Serialization; @@ -67,11 +66,13 @@ HttpContent Serialize(Parameter body) { }; HttpContent GetSerialized() { - if (!_client.Serializers.TryGetValue(body.DataFormat, out var serializer)) + if (!_client.Serializers.TryGetValue(body.DataFormat, out var serializerRecord)) throw new InvalidDataContractException( $"Can't find serializer for content type {body.DataFormat}" ); + var serializer = serializerRecord.GetSerializer(); + var content = serializer.Serialize(body); if (content == null) @@ -80,7 +81,7 @@ HttpContent GetSerialized() { return new StringContent( content, _client.Options.Encoding, - body.ContentType ?? serializer.ContentType + body.ContentType ?? serializer.Serializer.ContentType ); } } @@ -131,7 +132,7 @@ void AddPostParameters() { var formContent = new FormUrlEncodedContent( _request.Parameters .Where(x => x.Type == ParameterType.GetOrPost) - .Select(x => new KeyValuePair(x.Name!, x.Value!.ToString()!))! + .Select(x => new KeyValuePair(x.Name!, x.Value!.ToString()!)) ); Content = formContent; } diff --git a/src/RestSharp/Request/RequestParameters.cs b/src/RestSharp/Request/RequestParameters.cs index 38ad229da..45e8802a6 100644 --- a/src/RestSharp/Request/RequestParameters.cs +++ b/src/RestSharp/Request/RequestParameters.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/Request/RestRequest.cs b/src/RestSharp/Request/RestRequest.cs index 5cd918d48..d211353fd 100644 --- a/src/RestSharp/Request/RestRequest.cs +++ b/src/RestSharp/Request/RestRequest.cs @@ -1,4 +1,18 @@ -using RestSharp.Extensions; +// Copyright © 2009-2021 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. + +using RestSharp.Extensions; namespace RestSharp; diff --git a/src/RestSharp/Request/RestRequestExtensions.cs b/src/RestSharp/Request/RestRequestExtensions.cs index fa2ab1fb1..a3c749c1f 100644 --- a/src/RestSharp/Request/RestRequestExtensions.cs +++ b/src/RestSharp/Request/RestRequestExtensions.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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. + using System.Text.RegularExpressions; using RestSharp.Extensions; diff --git a/src/RestSharp/Request/RestXmlRequest.cs b/src/RestSharp/Request/RestXmlRequest.cs index ba01b37bf..740acb454 100644 --- a/src/RestSharp/Request/RestXmlRequest.cs +++ b/src/RestSharp/Request/RestXmlRequest.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/Response/RestResponse.cs b/src/RestSharp/Response/RestResponse.cs index 2c2af7e4a..dafd9b3d8 100644 --- a/src/RestSharp/Response/RestResponse.cs +++ b/src/RestSharp/Response/RestResponse.cs @@ -1,4 +1,18 @@ -using System.Diagnostics; +// Copyright © 2009-2021 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. + +using System.Diagnostics; using System.Net; using System.Net.Http.Headers; using System.Text; diff --git a/src/RestSharp/Response/RestResponseBase.cs b/src/RestSharp/Response/RestResponseBase.cs index 8526334fa..be9a7f5cd 100644 --- a/src/RestSharp/Response/RestResponseBase.cs +++ b/src/RestSharp/Response/RestResponseBase.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. @@ -11,7 +11,6 @@ // 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. -// using System.Diagnostics; using System.Net; diff --git a/src/RestSharp/RestClient.Async.cs b/src/RestSharp/RestClient.Async.cs index db80efae4..2e2cd1027 100644 --- a/src/RestSharp/RestClient.Async.cs +++ b/src/RestSharp/RestClient.Async.cs @@ -1,4 +1,18 @@ -using RestSharp.Extensions; +// Copyright © 2009-2021 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. + +using RestSharp.Extensions; namespace RestSharp; diff --git a/src/RestSharp/RestClient.cs b/src/RestSharp/RestClient.cs index 220a9912d..41ecb678d 100644 --- a/src/RestSharp/RestClient.cs +++ b/src/RestSharp/RestClient.cs @@ -1,4 +1,18 @@ -using System.Net; +// Copyright © 2009-2021 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. + +using System.Net; using System.Text; using RestSharp.Authenticators; using RestSharp.Extensions; @@ -93,7 +107,7 @@ public RestClient(Uri baseUrl) : this(new RestClientOptions { BaseUrl = baseUrl /// public RestClient(string baseUrl) : this(new Uri(Ensure.NotEmptyString(baseUrl, nameof(baseUrl)))) { } - internal Dictionary Serializers { get; } = new(); + internal Dictionary Serializers { get; } = new(); Func Encode { get; set; } = s => s.UrlEncode(); @@ -184,7 +198,7 @@ internal void AssignAcceptedContentTypes() /// Function that returns the serializer instance public RestClient UseSerializer(Func serializerFactory) { var instance = serializerFactory(); - Serializers[instance.DataFormat] = instance; + Serializers[instance.DataFormat] = new SerializerRecord(instance.DataFormat, instance.SupportedContentTypes, serializerFactory); AssignAcceptedContentTypes(); return this; } @@ -314,8 +328,10 @@ internal RestResponse Deserialize(RestRequest request, RestResponse raw) { // Only continue if there is a handler defined else there is no way to deserialize the data. // This can happen when a request returns for example a 404 page instead of the requested JSON/XML resource if (handler is IXmlDeserializer xml && request is RestXmlRequest xmlRequest) { - if (xmlRequest.DateFormat.IsNotEmpty()) xml.DateFormat = xmlRequest.DateFormat!; if (xmlRequest.XmlNamespace.IsNotEmpty()) xml.Namespace = xmlRequest.XmlNamespace!; + + if (xml is IWithDateFormat withDateFormat && xmlRequest.DateFormat.IsNotEmpty()) + withDateFormat.DateFormat = xmlRequest.DateFormat!; } if (handler is IWithRootElement deserializer && !request.RootElement.IsEmpty()) deserializer.RootElement = request.RootElement; @@ -346,7 +362,8 @@ internal RestResponse Deserialize(RestRequest request, RestResponse raw) { if (contentType.IsEmpty()) return null; var serializer = Serializers.FirstOrDefault(x => x.Value.SupportedContentTypes.Contains(contentType)); - return serializer.Value ?? (Serializers.ContainsKey(requestFormat) ? Serializers[requestFormat] : null); + var factory = serializer.Value ?? (Serializers.ContainsKey(requestFormat) ? Serializers[requestFormat] : null); + return factory?.GetSerializer().Deserializer; string? DetectContentType() => response.Content!.StartsWith("<") ? ContentType.Xml : response.Content.StartsWith("{") ? ContentType.Json : null; diff --git a/src/RestSharp/RestClientExtensions.Json.cs b/src/RestSharp/RestClientExtensions.Json.cs index 0ddd62c68..2b0783853 100644 --- a/src/RestSharp/RestClientExtensions.Json.cs +++ b/src/RestSharp/RestClientExtensions.Json.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/RestClientExtensions.Params.cs b/src/RestSharp/RestClientExtensions.Params.cs index b6bf02dfb..f5cff7a25 100644 --- a/src/RestSharp/RestClientExtensions.Params.cs +++ b/src/RestSharp/RestClientExtensions.Params.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/RestClientExtensions.cs b/src/RestSharp/RestClientExtensions.cs index 085a24d6a..448384cb1 100644 --- a/src/RestSharp/RestClientExtensions.cs +++ b/src/RestSharp/RestClientExtensions.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/RestClientOptions.cs b/src/RestSharp/RestClientOptions.cs index 89cd47f25..69378ed9f 100644 --- a/src/RestSharp/RestClientOptions.cs +++ b/src/RestSharp/RestClientOptions.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. diff --git a/src/RestSharp/Serializers/ContentType.cs b/src/RestSharp/Serializers/ContentType.cs index 5678f8dd2..d5f4ddf81 100644 --- a/src/RestSharp/Serializers/ContentType.cs +++ b/src/RestSharp/Serializers/ContentType.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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.Serializers; public static class ContentType { @@ -10,7 +24,7 @@ public static class ContentType { public static readonly Dictionary FromDataFormat = new() { { DataFormat.Xml, Xml }, - { DataFormat.Json, Json } + { DataFormat.Json, Json }, }; public static readonly string[] JsonAccept = { diff --git a/src/RestSharp/Serializers/DeseralizationException.cs b/src/RestSharp/Serializers/DeseralizationException.cs index a279995c9..155b677c3 100644 --- a/src/RestSharp/Serializers/DeseralizationException.cs +++ b/src/RestSharp/Serializers/DeseralizationException.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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. + // ReSharper disable once CheckNamespace namespace RestSharp; diff --git a/src/RestSharp/Serializers/IDeserializer.cs b/src/RestSharp/Serializers/IDeserializer.cs index 4b33a4df7..b76e7b0ce 100644 --- a/src/RestSharp/Serializers/IDeserializer.cs +++ b/src/RestSharp/Serializers/IDeserializer.cs @@ -1,4 +1,18 @@ -namespace RestSharp.Serializers; +// Copyright © 2009-2021 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.Serializers; public interface IDeserializer { T? Deserialize(RestResponse response); diff --git a/src/RestSharp/Serializers/IRestSerializer.cs b/src/RestSharp/Serializers/IRestSerializer.cs index 048870fed..2a8e7f3d5 100644 --- a/src/RestSharp/Serializers/IRestSerializer.cs +++ b/src/RestSharp/Serializers/IRestSerializer.cs @@ -1,6 +1,23 @@ +// Copyright © 2009-2021 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.Serializers; -public interface IRestSerializer : ISerializer, IDeserializer { +public interface IRestSerializer { + ISerializer Serializer { get; } + IDeserializer Deserializer { get; } + string[] SupportedContentTypes { get; } DataFormat DataFormat { get; } diff --git a/src/RestSharp/Serializers/ISerializer.cs b/src/RestSharp/Serializers/ISerializer.cs index fcde9bd84..79fabfa5c 100644 --- a/src/RestSharp/Serializers/ISerializer.cs +++ b/src/RestSharp/Serializers/ISerializer.cs @@ -1,4 +1,18 @@ -namespace RestSharp.Serializers; +// Copyright © 2009-2021 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.Serializers; public interface ISerializer { string ContentType { get; set; } diff --git a/src/RestSharp/Serializers/IWithDateFormat.cs b/src/RestSharp/Serializers/IWithDateFormat.cs new file mode 100644 index 000000000..35d9a266a --- /dev/null +++ b/src/RestSharp/Serializers/IWithDateFormat.cs @@ -0,0 +1,19 @@ +// Copyright © 2009-2021 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.Serializers; + +public interface IWithDateFormat { + string? DateFormat { get; set; } +} \ No newline at end of file diff --git a/src/RestSharp/Serializers/IWithRootElement.cs b/src/RestSharp/Serializers/IWithRootElement.cs index 80d141e36..22bcea51f 100644 --- a/src/RestSharp/Serializers/IWithRootElement.cs +++ b/src/RestSharp/Serializers/IWithRootElement.cs @@ -1,5 +1,19 @@ -namespace RestSharp.Serializers { - public interface IWithRootElement { - string? RootElement { get; set; } - } +// Copyright © 2009-2021 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.Serializers; + +public interface IWithRootElement { + string? RootElement { get; set; } } \ No newline at end of file diff --git a/src/RestSharp/Serializers/Json/RestClientExtensions.cs b/src/RestSharp/Serializers/Json/RestClientExtensions.cs index 4ae094d0f..00ed71f31 100644 --- a/src/RestSharp/Serializers/Json/RestClientExtensions.cs +++ b/src/RestSharp/Serializers/Json/RestClientExtensions.cs @@ -1,3 +1,17 @@ +// Copyright © 2009-2021 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. + using System.Text.Json; namespace RestSharp.Serializers.Json; diff --git a/src/RestSharp/Serializers/Json/SystemTextJsonSerializer.cs b/src/RestSharp/Serializers/Json/SystemTextJsonSerializer.cs index ef57e2d6c..2fb5c3aa9 100644 --- a/src/RestSharp/Serializers/Json/SystemTextJsonSerializer.cs +++ b/src/RestSharp/Serializers/Json/SystemTextJsonSerializer.cs @@ -1,8 +1,22 @@ +// Copyright © 2009-2021 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. + using System.Text.Json; -namespace RestSharp.Serializers.Json; +namespace RestSharp.Serializers.Json; -public class SystemTextJsonSerializer : IRestSerializer { +public class SystemTextJsonSerializer : IRestSerializer, ISerializer, IDeserializer { readonly JsonSerializerOptions _options; /// @@ -22,6 +36,8 @@ public class SystemTextJsonSerializer : IRestSerializer { public T? Deserialize(RestResponse response) => JsonSerializer.Deserialize(response.Content!, _options); + public ISerializer Serializer => this; + public IDeserializer Deserializer => this; public string[] SupportedContentTypes { get; } = { "application/json", "text/json", "text/x-json", "text/javascript", "*+json" }; diff --git a/src/RestSharp/Serializers/SerializerRecord.cs b/src/RestSharp/Serializers/SerializerRecord.cs new file mode 100644 index 000000000..6fc5182fa --- /dev/null +++ b/src/RestSharp/Serializers/SerializerRecord.cs @@ -0,0 +1,17 @@ +// Copyright © 2009-2021 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.Serializers; + +public record SerializerRecord(DataFormat DataFormat, string[] SupportedContentTypes, Func GetSerializer); \ No newline at end of file diff --git a/src/RestSharp/Serializers/Xml/DotNetXmlDeserializer.cs b/src/RestSharp/Serializers/Xml/DotNetXmlDeserializer.cs index aade10166..e04dce2ba 100644 --- a/src/RestSharp/Serializers/Xml/DotNetXmlDeserializer.cs +++ b/src/RestSharp/Serializers/Xml/DotNetXmlDeserializer.cs @@ -1,4 +1,18 @@ -using System.Text; +// Copyright © 2009-2021 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. + +using System.Text; namespace RestSharp.Serializers.Xml; diff --git a/src/RestSharp/Serializers/Xml/DotNetXmlSerializer.cs b/src/RestSharp/Serializers/Xml/DotNetXmlSerializer.cs index c2aa9d768..4f0e4529f 100644 --- a/src/RestSharp/Serializers/Xml/DotNetXmlSerializer.cs +++ b/src/RestSharp/Serializers/Xml/DotNetXmlSerializer.cs @@ -1,4 +1,18 @@ -using System.Text; +// Copyright © 2009-2021 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. + +using System.Text; using System.Xml.Serialization; namespace RestSharp.Serializers.Xml; diff --git a/src/RestSharp/Serializers/Xml/DotNetXmlSerializerClientExtensions.cs b/src/RestSharp/Serializers/Xml/DotNetXmlSerializerClientExtensions.cs index dffac4320..ea9117002 100644 --- a/src/RestSharp/Serializers/Xml/DotNetXmlSerializerClientExtensions.cs +++ b/src/RestSharp/Serializers/Xml/DotNetXmlSerializerClientExtensions.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. @@ -16,6 +16,7 @@ namespace RestSharp.Serializers.Xml; +[PublicAPI] public static class DotNetXmlSerializerClientExtensions { public static RestClient UseDotNetXmlSerializer( this RestClient restClient, diff --git a/src/RestSharp/Serializers/Xml/IXmlDeserializer.cs b/src/RestSharp/Serializers/Xml/IXmlDeserializer.cs index a85ea9970..5f2d8c59c 100644 --- a/src/RestSharp/Serializers/Xml/IXmlDeserializer.cs +++ b/src/RestSharp/Serializers/Xml/IXmlDeserializer.cs @@ -1,7 +1,19 @@ -namespace RestSharp.Serializers.Xml; +// Copyright © 2009-2021 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. -public interface IXmlDeserializer : IDeserializer, IWithRootElement { - string? Namespace { get; set; } +namespace RestSharp.Serializers.Xml; - string? DateFormat { get; set; } +public interface IXmlDeserializer : IDeserializer { + string? Namespace { get; set; } } \ No newline at end of file diff --git a/src/RestSharp/Serializers/Xml/IXmlSerializer.cs b/src/RestSharp/Serializers/Xml/IXmlSerializer.cs index 27d845c52..62771df10 100644 --- a/src/RestSharp/Serializers/Xml/IXmlSerializer.cs +++ b/src/RestSharp/Serializers/Xml/IXmlSerializer.cs @@ -1,7 +1,19 @@ -namespace RestSharp.Serializers.Xml; +// Copyright © 2009-2021 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. -public interface IXmlSerializer : ISerializer, IWithRootElement { - string? Namespace { get; set; } +namespace RestSharp.Serializers.Xml; - string? DateFormat { get; set; } +public interface IXmlSerializer : ISerializer { + string? Namespace { get; set; } } \ No newline at end of file diff --git a/src/RestSharp/Serializers/Xml/XmlRestSerializer.cs b/src/RestSharp/Serializers/Xml/XmlRestSerializer.cs index 9bc55c6fa..41b3b4dba 100644 --- a/src/RestSharp/Serializers/Xml/XmlRestSerializer.cs +++ b/src/RestSharp/Serializers/Xml/XmlRestSerializer.cs @@ -1,4 +1,4 @@ -// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community +// Copyright © 2009-2021 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. @@ -12,14 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System.Globalization; - namespace RestSharp.Serializers.Xml; -public class XmlRestSerializer : IRestSerializer, IXmlSerializer, IXmlDeserializer { - XmlSerializationOptions _options = XmlSerializationOptions.Default; - IXmlDeserializer _xmlDeserializer; - IXmlSerializer _xmlSerializer; +public class XmlRestSerializer : IRestSerializer { + IXmlDeserializer _xmlDeserializer; + IXmlSerializer _xmlSerializer; public XmlRestSerializer() : this(new DotNetXmlSerializer(), new DotNetXmlDeserializer()) { } @@ -28,16 +25,12 @@ public XmlRestSerializer(IXmlSerializer xmlSerializer, IXmlDeserializer xmlDeser _xmlSerializer = xmlSerializer; } - public string[] SupportedContentTypes => Serializers.ContentType.XmlAccept; + public ISerializer Serializer => _xmlSerializer; + public IDeserializer Deserializer => _xmlDeserializer; + public string[] SupportedContentTypes => ContentType.XmlAccept; public DataFormat DataFormat => DataFormat.Xml; - public string ContentType { get; set; } = Serializers.ContentType.Xml; - - public string? Serialize(object? obj) => _xmlSerializer.Serialize(Ensure.NotNull(obj, nameof(obj))); - - public T? Deserialize(RestResponse response) => _xmlDeserializer.Deserialize(response); - public string? Serialize(Parameter parameter) { if (parameter is not XmlParameter xmlParameter) throw new ArgumentException("Supplied parameter is not an XML parameter", nameof(parameter)); @@ -55,92 +48,13 @@ public XmlRestSerializer(IXmlSerializer xmlSerializer, IXmlDeserializer xmlDeser return result; } - public string? RootElement { - get => _options.RootElement; - set { - _options.RootElement = value; - _xmlSerializer.RootElement = value; - _xmlDeserializer.RootElement = value; - } - } - - public string? Namespace { - get => _options.Namespace; - set { - var ns = Ensure.NotEmptyString(value, nameof(Namespace)); - _options.Namespace = ns; - _xmlSerializer.Namespace = ns; - _xmlDeserializer.Namespace = ns; - } - } - - public string? DateFormat { - get => _options.DateFormat; - set { - var dateFormat = Ensure.NotEmptyString(value, nameof(DataFormat)); - _options.DateFormat = dateFormat; - _xmlSerializer.DateFormat = dateFormat; - _xmlDeserializer.DateFormat = dateFormat; - } - } - - public XmlRestSerializer WithOptions(XmlSerializationOptions options) { - _options = options; - return this; - } - - public XmlRestSerializer WithXmlSerializer(XmlSerializationOptions? options = null) where T : IXmlSerializer, new() { - if (options != null) _options = options; - - return WithXmlSerializer( - new T { - Namespace = _options.Namespace, - DateFormat = _options.DateFormat, - RootElement = _options.RootElement - } - ); - } - public XmlRestSerializer WithXmlSerializer(IXmlSerializer xmlSerializer) { _xmlSerializer = xmlSerializer; return this; } - public XmlRestSerializer WithXmlDeserializer(XmlSerializationOptions? options = null) where T : IXmlDeserializer, new() { - if (options != null) _options = options; - - return WithXmlDeserializer( - new T { - Namespace = _options.Namespace, - DateFormat = _options.DateFormat, - RootElement = _options.RootElement - } - ); - } - public XmlRestSerializer WithXmlDeserializer(IXmlDeserializer xmlDeserializer) { _xmlDeserializer = xmlDeserializer; return this; } -} - -public class XmlSerializationOptions { - /// - /// Name of the root element to use when serializing - /// - public string? RootElement { get; set; } - - /// - /// XML namespace to use when serializing - /// - public string? Namespace { get; set; } - - /// - /// Format string to use when serializing dates - /// - public string? DateFormat { get; set; } - - public CultureInfo? Culture { get; set; } - - public static XmlSerializationOptions Default => new() { Culture = CultureInfo.InvariantCulture }; } \ No newline at end of file diff --git a/test/RestSharp.IntegrationTests/RootElementTests.cs b/test/RestSharp.IntegrationTests/RootElementTests.cs index e1e52b283..89a644553 100644 --- a/test/RestSharp.IntegrationTests/RootElementTests.cs +++ b/test/RestSharp.IntegrationTests/RootElementTests.cs @@ -1,5 +1,4 @@ using System.Net; -using RestSharp.IntegrationTests.SampleDeserializers; using RestSharp.Serializers.Xml; using RestSharp.Tests.Shared.Extensions; using RestSharp.Tests.Shared.Fixtures; @@ -11,17 +10,14 @@ public class RootElementTests { public async Task Copy_RootElement_From_Request_To_IWithRootElement_Deserializer() { using var server = HttpServerFixture.StartServer("success", Handle); - var client = new RestClient(server.Url); + var client = new RestClient(server.Url).UseXmlSerializer(); var request = new RestRequest("success") { RootElement = "Success" }; - var deserializer = new CustomDeserializer(); - var restSerializer = new XmlRestSerializer(new XmlSerializer(), deserializer); - client.UseSerializer(() => restSerializer); - - await client.ExecuteAsync(request); + var response = await client.ExecuteAsync(request); - Assert.Equal(request.RootElement, deserializer.RootElement); + response.Data.Should().NotBeNull(); + response.Data!.Message.Should().Be("Works!"); static void Handle(HttpListenerRequest req, HttpListenerResponse response) { response.StatusCode = 200; diff --git a/test/RestSharp.IntegrationTests/SampleDeserializers/CustomDeserializer.cs b/test/RestSharp.IntegrationTests/SampleDeserializers/CustomDeserializer.cs deleted file mode 100644 index bdac358ce..000000000 --- a/test/RestSharp.IntegrationTests/SampleDeserializers/CustomDeserializer.cs +++ /dev/null @@ -1,11 +0,0 @@ -using RestSharp.Serializers.Xml; - -namespace RestSharp.IntegrationTests.SampleDeserializers; - -class CustomDeserializer : IXmlDeserializer { - public T Deserialize(RestResponse response) => default; - - public string RootElement { get; set; } - public string Namespace { get; set; } - public string DateFormat { get; set; } -} \ No newline at end of file From da55ae7fc2e2fb77bf9ea6e5819266ee4213190e Mon Sep 17 00:00:00 2001 From: Alexey Zimarev Date: Fri, 17 Dec 2021 15:28:28 +0100 Subject: [PATCH 4/5] Bump IsExternalInit --- src/Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 8655fbb63..9d59e2572 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -31,7 +31,7 @@ - + From 3d5c59d819a0483a711ad4627ce3a549eba17378 Mon Sep 17 00:00:00 2001 From: Alexey Zimarev Date: Fri, 17 Dec 2021 15:29:56 +0100 Subject: [PATCH 5/5] Make timeout tests more predictable --- test/RestSharp.IntegrationTests/AsyncTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/RestSharp.IntegrationTests/AsyncTests.cs b/test/RestSharp.IntegrationTests/AsyncTests.cs index fb5c8c839..efd01d3ea 100644 --- a/test/RestSharp.IntegrationTests/AsyncTests.cs +++ b/test/RestSharp.IntegrationTests/AsyncTests.cs @@ -88,7 +88,7 @@ public async Task Can_Timeout_GET_Async() { var request = new RestRequest("timeout", Method.Get).AddBody("Body_Content"); // Half the value of ResponseHandler.Timeout - request.Timeout = 500; + request.Timeout = 200; var response = await client.ExecuteAsync(request); @@ -103,7 +103,7 @@ public async Task Can_Timeout_PUT_Async() { var request = new RestRequest("timeout", Method.Put).AddBody("Body_Content"); // Half the value of ResponseHandler.Timeout - request.Timeout = 500; + request.Timeout = 200; var response = await client.ExecuteAsync(request);