diff --git a/RestSharp.sln.DotSettings b/RestSharp.sln.DotSettings index 975adf68e..8ac5f1395 100644 --- a/RestSharp.sln.DotSettings +++ b/RestSharp.sln.DotSettings @@ -85,7 +85,7 @@ True True True - Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community + Copyright (c) .NET Foundation and Contributors 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/Directory.Build.props b/src/Directory.Build.props index 016a45495..c99c4fe4a 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -26,9 +26,6 @@ - - - $(MinVerMajor).$(MinVerMinor).$(MinVerPatch) diff --git a/src/RestSharp/Parameters/BodyParameter.cs b/src/RestSharp/Parameters/BodyParameter.cs index d2cf78531..6df933a22 100644 --- a/src/RestSharp/Parameters/BodyParameter.cs +++ b/src/RestSharp/Parameters/BodyParameter.cs @@ -21,15 +21,19 @@ public BodyParameter(string? name, object value, string contentType, DataFormat if (dataFormat == DataFormat.Binary && value is not byte[]) { throw new ArgumentException("Binary data format needs a byte array as value"); } + ContentType = contentType; DataFormat = dataFormat; } + public BodyParameter(object value, string contentType, DataFormat dataFormat = DataFormat.None) + : this("", value, contentType, dataFormat) { } + /// /// Body parameter data type /// public DataFormat DataFormat { get; init; } = DataFormat.None; - + /// /// Custom content encoding /// @@ -41,10 +45,16 @@ public XmlParameter(string name, object value, string? xmlNamespace = null, stri : base(name, value, contentType, DataFormat.Xml) => XmlNamespace = xmlNamespace; + public XmlParameter(object value, string? xmlNamespace = null, string contentType = Serializers.ContentType.Xml) + : this("", value, xmlNamespace, contentType) { } + public string? XmlNamespace { get; } } public record JsonParameter : BodyParameter { - public JsonParameter(string name, object value, string contentType = Serializers.ContentType.Json) + public JsonParameter(string name, object value, string contentType = Serializers.ContentType.Json) : base(name, value, contentType, DataFormat.Json) { } -} \ No newline at end of file + + public JsonParameter(object value, string contentType = Serializers.ContentType.Json) + : this("", value, contentType) { } +} diff --git a/src/RestSharp/Properties/IsExternalInit.cs b/src/RestSharp/Properties/IsExternalInit.cs new file mode 100644 index 000000000..deb2fb270 --- /dev/null +++ b/src/RestSharp/Properties/IsExternalInit.cs @@ -0,0 +1,9 @@ +#if NETSTANDARD +using System.ComponentModel; + +// ReSharper disable once CheckNamespace +namespace System.Runtime.CompilerServices; + +[EditorBrowsable(EditorBrowsableState.Never)] +internal class IsExternalInit{} +#endif diff --git a/src/RestSharp/Request/RestRequestExtensions.cs b/src/RestSharp/Request/RestRequestExtensions.cs index e7ccb2c01..80bfa5c13 100644 --- a/src/RestSharp/Request/RestRequestExtensions.cs +++ b/src/RestSharp/Request/RestRequestExtensions.cs @@ -261,6 +261,35 @@ public static RestRequest AddOrUpdateParameters(this RestRequest request, IEnume return request; } + // TODO: Three methods below added for binary compatibility with v108. Remove for the next major release. + // In addition, both contentType and options parameters should get default values. + + public static RestRequest AddFile( + this RestRequest request, + string name, + string path, + string? contentType = null + ) + => request.AddFile(FileParameter.FromFile(path, name, contentType)); + + public static RestRequest AddFile( + this RestRequest request, + string name, + byte[] bytes, + string filename, + string? contentType = null + ) + => request.AddFile(FileParameter.Create(name, bytes, filename, contentType)); + + public static RestRequest AddFile( + this RestRequest request, + string name, + Func getFile, + string fileName, + string? contentType = null + ) + => request.AddFile(FileParameter.Create(name, getFile, fileName, contentType)); + /// /// Adds a file parameter to the request body. The file will be read from disk as a stream. /// @@ -274,8 +303,8 @@ public static RestRequest AddFile( this RestRequest request, string name, string path, - string? contentType = null, - FileParameterOptions? options = null + string? contentType, + FileParameterOptions? options ) => request.AddFile(FileParameter.FromFile(path, name, contentType, options)); @@ -294,8 +323,8 @@ public static RestRequest AddFile( string name, byte[] bytes, string filename, - string? contentType = null, - FileParameterOptions? options = null + string? contentType, + FileParameterOptions? options ) => request.AddFile(FileParameter.Create(name, bytes, filename, contentType, options)); @@ -314,8 +343,8 @@ public static RestRequest AddFile( string name, Func getFile, string fileName, - string? contentType = null, - FileParameterOptions? options = null + string? contentType, + FileParameterOptions? options ) => request.AddFile(FileParameter.Create(name, getFile, fileName, contentType, options)); @@ -368,7 +397,7 @@ public static RestRequest AddStringBody(this RestRequest request, string body, D /// Content type of the body /// public static RestRequest AddStringBody(this RestRequest request, string body, string contentType) - => request.AddParameter(new BodyParameter("", body, Ensure.NotEmpty(contentType, nameof(contentType)))); + => request.AddParameter(new BodyParameter(body, Ensure.NotEmpty(contentType, nameof(contentType)))); /// /// Adds a JSON body parameter to the request @@ -379,7 +408,7 @@ public static RestRequest AddStringBody(this RestRequest request, string body, s /// public static RestRequest AddJsonBody(this RestRequest request, T obj, string contentType = ContentType.Json) where T : class { request.RequestFormat = DataFormat.Json; - return obj is string str ? request.AddStringBody(str, DataFormat.Json) : request.AddParameter(new JsonParameter("", obj, contentType)); + return obj is string str ? request.AddStringBody(str, DataFormat.Json) : request.AddParameter(new JsonParameter(obj, contentType)); } /// @@ -396,7 +425,7 @@ public static RestRequest AddXmlBody(this RestRequest request, T obj, string return obj is string str ? request.AddStringBody(str, DataFormat.Xml) - : request.AddParameter(new XmlParameter("", obj, xmlNamespace, contentType)); + : request.AddParameter(new XmlParameter(obj, xmlNamespace, contentType)); } /// diff --git a/src/RestSharp/RestClient.cs b/src/RestSharp/RestClient.cs index ec8603563..b12b00d72 100644 --- a/src/RestSharp/RestClient.cs +++ b/src/RestSharp/RestClient.cs @@ -125,11 +125,12 @@ public RestClient(HttpMessageHandler handler, bool disposeHandler = true) : this void ConfigureHttpClient(HttpClient httpClient) { if (Options.MaxTimeout > 0) httpClient.Timeout = TimeSpan.FromMilliseconds(Options.MaxTimeout); - if (httpClient.DefaultRequestHeaders.UserAgent.All(x => x.Product.Name != "RestSharp")) { + + if (Options.UserAgent != null && httpClient.DefaultRequestHeaders.UserAgent.All(x => x.Product?.Name != Options.UserAgent)) { httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(Options.UserAgent); } - if (Options.Expect100Continue != null) - httpClient.DefaultRequestHeaders.ExpectContinue = Options.Expect100Continue; + + if (Options.Expect100Continue != null) httpClient.DefaultRequestHeaders.ExpectContinue = Options.Expect100Continue; } void ConfigureHttpMessageHandler(HttpClientHandler handler) { @@ -139,9 +140,8 @@ void ConfigureHttpMessageHandler(HttpClientHandler handler) { handler.AutomaticDecompression = Options.AutomaticDecompression; handler.PreAuthenticate = Options.PreAuthenticate; handler.AllowAutoRedirect = Options.FollowRedirects; - - if (handler.SupportsProxy) - handler.Proxy = Options.Proxy; + + if (handler.SupportsProxy) handler.Proxy = Options.Proxy; if (Options.RemoteCertificateValidationCallback != null) handler.ServerCertificateCustomValidationCallback = @@ -178,7 +178,7 @@ public RestClient AddDefaultParameter(Parameter parameter) { ); if (!Options.AllowMultipleDefaultParametersWithSameName && - !MultiParameterTypes.Contains(parameter.Type) && + !MultiParameterTypes.Contains(parameter.Type) && DefaultParameters.Any(x => x.Name == parameter.Name)) { throw new ArgumentException("A default parameters with the same name has already been added", nameof(parameter)); } @@ -237,4 +237,4 @@ public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } -} \ No newline at end of file +} diff --git a/src/RestSharp/RestClientOptions.cs b/src/RestSharp/RestClientOptions.cs index 64f78ffa8..43fec3f3a 100644 --- a/src/RestSharp/RestClientOptions.cs +++ b/src/RestSharp/RestClientOptions.cs @@ -75,7 +75,7 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba public bool FollowRedirects { get; set; } = true; public bool? Expect100Continue { get; set; } = null; public CookieContainer? CookieContainer { get; set; } - public string UserAgent { get; set; } = DefaultUserAgent; + public string? UserAgent { get; set; } = DefaultUserAgent; /// /// Maximum request duration in milliseconds. When the request timeout is specified using ,