Skip to content

Commit cbcc74b

Browse files
committed
Removed ExternalInit reference in favour of inline code
Reverted AddFile overloads without options. (should fix #1953)
1 parent df07026 commit cbcc74b

7 files changed

+70
-25
lines changed

RestSharp.sln.DotSettings

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
<s:Boolean x:Key="/Default/UserDictionary/Words/=repcodes/@EntryIndexedValue">True</s:Boolean>
8686
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean>
8787
<s:Boolean x:Key="/Default/UserDictionary/Words/=upsert/@EntryIndexedValue">True</s:Boolean>
88-
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue"> Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community
88+
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue"> Copyright (c) .NET Foundation and Contributors
8989

9090
Licensed under the Apache License, Version 2.0 (the "License");
9191
you may not use this file except in compliance with the License.

src/Directory.Build.props

-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
<None Include="$(RepoRoot)\restsharp.png" Pack="true" PackagePath="\"/>
2727
<Using Include="JetBrains.Annotations"/>
2828
</ItemGroup>
29-
<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
30-
<PackageReference Include="IsExternalInit" Version="1.0.2" PrivateAssets="All"/>
31-
</ItemGroup>
3229
<Target Name="CustomVersion" AfterTargets="MinVer">
3330
<PropertyGroup>
3431
<FileVersion>$(MinVerMajor).$(MinVerMinor).$(MinVerPatch)</FileVersion>

src/RestSharp/Parameters/BodyParameter.cs

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ public BodyParameter(string? name, object value, string contentType, DataFormat
2121
if (dataFormat == DataFormat.Binary && value is not byte[]) {
2222
throw new ArgumentException("Binary data format needs a byte array as value");
2323
}
24+
2425
ContentType = contentType;
2526
DataFormat = dataFormat;
2627
}
2728

29+
public BodyParameter(object value, string contentType, DataFormat dataFormat = DataFormat.None)
30+
: this("", value, contentType, dataFormat) { }
31+
2832
/// <summary>
2933
/// Body parameter data type
3034
/// </summary>
3135
public DataFormat DataFormat { get; init; } = DataFormat.None;
32-
36+
3337
/// <summary>
3438
/// Custom content encoding
3539
/// </summary>
@@ -41,10 +45,16 @@ public XmlParameter(string name, object value, string? xmlNamespace = null, stri
4145
: base(name, value, contentType, DataFormat.Xml)
4246
=> XmlNamespace = xmlNamespace;
4347

48+
public XmlParameter(object value, string? xmlNamespace = null, string contentType = Serializers.ContentType.Xml)
49+
: this("", value, xmlNamespace, contentType) { }
50+
4451
public string? XmlNamespace { get; }
4552
}
4653

4754
public record JsonParameter : BodyParameter {
48-
public JsonParameter(string name, object value, string contentType = Serializers.ContentType.Json)
55+
public JsonParameter(string name, object value, string contentType = Serializers.ContentType.Json)
4956
: base(name, value, contentType, DataFormat.Json) { }
50-
}
57+
58+
public JsonParameter(object value, string contentType = Serializers.ContentType.Json)
59+
: this("", value, contentType) { }
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#if NETSTANDARD
2+
using System.ComponentModel;
3+
4+
// ReSharper disable once CheckNamespace
5+
namespace System.Runtime.CompilerServices;
6+
7+
[EditorBrowsable(EditorBrowsableState.Never)]
8+
internal class IsExternalInit{}
9+
#endif

src/RestSharp/Request/RestRequestExtensions.cs

+38-9
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,35 @@ public static RestRequest AddOrUpdateParameters(this RestRequest request, IEnume
261261
return request;
262262
}
263263

264+
// TODO: Three methods below added for binary compatibility with v108. Remove for the next major release.
265+
// In addition, both contentType and options parameters should get default values.
266+
267+
public static RestRequest AddFile(
268+
this RestRequest request,
269+
string name,
270+
string path,
271+
string? contentType = null
272+
)
273+
=> request.AddFile(FileParameter.FromFile(path, name, contentType));
274+
275+
public static RestRequest AddFile(
276+
this RestRequest request,
277+
string name,
278+
byte[] bytes,
279+
string filename,
280+
string? contentType = null
281+
)
282+
=> request.AddFile(FileParameter.Create(name, bytes, filename, contentType));
283+
284+
public static RestRequest AddFile(
285+
this RestRequest request,
286+
string name,
287+
Func<Stream> getFile,
288+
string fileName,
289+
string? contentType = null
290+
)
291+
=> request.AddFile(FileParameter.Create(name, getFile, fileName, contentType));
292+
264293
/// <summary>
265294
/// Adds a file parameter to the request body. The file will be read from disk as a stream.
266295
/// </summary>
@@ -274,8 +303,8 @@ public static RestRequest AddFile(
274303
this RestRequest request,
275304
string name,
276305
string path,
277-
string? contentType = null,
278-
FileParameterOptions? options = null
306+
string? contentType,
307+
FileParameterOptions? options
279308
)
280309
=> request.AddFile(FileParameter.FromFile(path, name, contentType, options));
281310

@@ -294,8 +323,8 @@ public static RestRequest AddFile(
294323
string name,
295324
byte[] bytes,
296325
string filename,
297-
string? contentType = null,
298-
FileParameterOptions? options = null
326+
string? contentType,
327+
FileParameterOptions? options
299328
)
300329
=> request.AddFile(FileParameter.Create(name, bytes, filename, contentType, options));
301330

@@ -314,8 +343,8 @@ public static RestRequest AddFile(
314343
string name,
315344
Func<Stream> getFile,
316345
string fileName,
317-
string? contentType = null,
318-
FileParameterOptions? options = null
346+
string? contentType,
347+
FileParameterOptions? options
319348
)
320349
=> request.AddFile(FileParameter.Create(name, getFile, fileName, contentType, options));
321350

@@ -368,7 +397,7 @@ public static RestRequest AddStringBody(this RestRequest request, string body, D
368397
/// <param name="contentType">Content type of the body</param>
369398
/// <returns></returns>
370399
public static RestRequest AddStringBody(this RestRequest request, string body, string contentType)
371-
=> request.AddParameter(new BodyParameter("", body, Ensure.NotEmpty(contentType, nameof(contentType))));
400+
=> request.AddParameter(new BodyParameter(body, Ensure.NotEmpty(contentType, nameof(contentType))));
372401

373402
/// <summary>
374403
/// Adds a JSON body parameter to the request
@@ -379,7 +408,7 @@ public static RestRequest AddStringBody(this RestRequest request, string body, s
379408
/// <returns></returns>
380409
public static RestRequest AddJsonBody<T>(this RestRequest request, T obj, string contentType = ContentType.Json) where T : class {
381410
request.RequestFormat = DataFormat.Json;
382-
return obj is string str ? request.AddStringBody(str, DataFormat.Json) : request.AddParameter(new JsonParameter("", obj, contentType));
411+
return obj is string str ? request.AddStringBody(str, DataFormat.Json) : request.AddParameter(new JsonParameter(obj, contentType));
383412
}
384413

385414
/// <summary>
@@ -396,7 +425,7 @@ public static RestRequest AddXmlBody<T>(this RestRequest request, T obj, string
396425

397426
return obj is string str
398427
? request.AddStringBody(str, DataFormat.Xml)
399-
: request.AddParameter(new XmlParameter("", obj, xmlNamespace, contentType));
428+
: request.AddParameter(new XmlParameter(obj, xmlNamespace, contentType));
400429
}
401430

402431
/// <summary>

src/RestSharp/RestClient.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ public RestClient(HttpMessageHandler handler, bool disposeHandler = true) : this
125125

126126
void ConfigureHttpClient(HttpClient httpClient) {
127127
if (Options.MaxTimeout > 0) httpClient.Timeout = TimeSpan.FromMilliseconds(Options.MaxTimeout);
128-
if (httpClient.DefaultRequestHeaders.UserAgent.All(x => x.Product.Name != "RestSharp")) {
128+
129+
if (Options.UserAgent != null && httpClient.DefaultRequestHeaders.UserAgent.All(x => x.Product?.Name != Options.UserAgent)) {
129130
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(Options.UserAgent);
130131
}
131-
if (Options.Expect100Continue != null)
132-
httpClient.DefaultRequestHeaders.ExpectContinue = Options.Expect100Continue;
132+
133+
if (Options.Expect100Continue != null) httpClient.DefaultRequestHeaders.ExpectContinue = Options.Expect100Continue;
133134
}
134135

135136
void ConfigureHttpMessageHandler(HttpClientHandler handler) {
@@ -139,9 +140,8 @@ void ConfigureHttpMessageHandler(HttpClientHandler handler) {
139140
handler.AutomaticDecompression = Options.AutomaticDecompression;
140141
handler.PreAuthenticate = Options.PreAuthenticate;
141142
handler.AllowAutoRedirect = Options.FollowRedirects;
142-
143-
if (handler.SupportsProxy)
144-
handler.Proxy = Options.Proxy;
143+
144+
if (handler.SupportsProxy) handler.Proxy = Options.Proxy;
145145

146146
if (Options.RemoteCertificateValidationCallback != null)
147147
handler.ServerCertificateCustomValidationCallback =
@@ -178,7 +178,7 @@ public RestClient AddDefaultParameter(Parameter parameter) {
178178
);
179179

180180
if (!Options.AllowMultipleDefaultParametersWithSameName &&
181-
!MultiParameterTypes.Contains(parameter.Type) &&
181+
!MultiParameterTypes.Contains(parameter.Type) &&
182182
DefaultParameters.Any(x => x.Name == parameter.Name)) {
183183
throw new ArgumentException("A default parameters with the same name has already been added", nameof(parameter));
184184
}
@@ -237,4 +237,4 @@ public void Dispose() {
237237
Dispose(true);
238238
GC.SuppressFinalize(this);
239239
}
240-
}
240+
}

src/RestSharp/RestClientOptions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba
7575
public bool FollowRedirects { get; set; } = true;
7676
public bool? Expect100Continue { get; set; } = null;
7777
public CookieContainer? CookieContainer { get; set; }
78-
public string UserAgent { get; set; } = DefaultUserAgent;
78+
public string? UserAgent { get; set; } = DefaultUserAgent;
7979

8080
/// <summary>
8181
/// Maximum request duration in milliseconds. When the request timeout is specified using <seealso cref="RestRequest.Timeout"/>,

0 commit comments

Comments
 (0)