Skip to content

Commit

Permalink
Removed ExternalInit reference in favour of inline code
Browse files Browse the repository at this point in the history
Reverted AddFile overloads without options. (should fix #1953)
  • Loading branch information
alexeyzimarev committed Nov 8, 2022
1 parent df07026 commit cbcc74b
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 25 deletions.
2 changes: 1 addition & 1 deletion RestSharp.sln.DotSettings
Expand Up @@ -85,7 +85,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=repcodes/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=upsert/@EntryIndexedValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue"> Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue"> 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.
Expand Down
3 changes: 0 additions & 3 deletions src/Directory.Build.props
Expand Up @@ -26,9 +26,6 @@
<None Include="$(RepoRoot)\restsharp.png" Pack="true" PackagePath="\"/>
<Using Include="JetBrains.Annotations"/>
</ItemGroup>
<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
<PackageReference Include="IsExternalInit" Version="1.0.2" PrivateAssets="All"/>
</ItemGroup>
<Target Name="CustomVersion" AfterTargets="MinVer">
<PropertyGroup>
<FileVersion>$(MinVerMajor).$(MinVerMinor).$(MinVerPatch)</FileVersion>
Expand Down
16 changes: 13 additions & 3 deletions src/RestSharp/Parameters/BodyParameter.cs
Expand Up @@ -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) { }

/// <summary>
/// Body parameter data type
/// </summary>
public DataFormat DataFormat { get; init; } = DataFormat.None;

/// <summary>
/// Custom content encoding
/// </summary>
Expand All @@ -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) { }
}

public JsonParameter(object value, string contentType = Serializers.ContentType.Json)
: this("", value, contentType) { }
}
9 changes: 9 additions & 0 deletions 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
47 changes: 38 additions & 9 deletions src/RestSharp/Request/RestRequestExtensions.cs
Expand Up @@ -261,6 +261,35 @@ public static RestRequest AddOrUpdateParameter(this RestRequest request, Paramet
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<Stream> getFile,
string fileName,
string? contentType = null
)
=> request.AddFile(FileParameter.Create(name, getFile, fileName, contentType));

/// <summary>
/// Adds a file parameter to the request body. The file will be read from disk as a stream.
/// </summary>
Expand All @@ -274,8 +303,8 @@ public static RestRequest AddOrUpdateParameter(this RestRequest request, Paramet
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));

Expand All @@ -294,8 +323,8 @@ public static RestRequest AddOrUpdateParameter(this RestRequest request, Paramet
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));

Expand All @@ -314,8 +343,8 @@ public static RestRequest AddOrUpdateParameter(this RestRequest request, Paramet
string name,
Func<Stream> getFile,
string fileName,
string? contentType = null,
FileParameterOptions? options = null
string? contentType,
FileParameterOptions? options
)
=> request.AddFile(FileParameter.Create(name, getFile, fileName, contentType, options));

Expand Down Expand Up @@ -368,7 +397,7 @@ public static RestRequest AddOrUpdateParameter(this RestRequest request, Paramet
/// <param name="contentType">Content type of the body</param>
/// <returns></returns>
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))));

/// <summary>
/// Adds a JSON body parameter to the request
Expand All @@ -379,7 +408,7 @@ public static RestRequest AddStringBody(this RestRequest request, string body, s
/// <returns></returns>
public static RestRequest AddJsonBody<T>(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));
}

/// <summary>
Expand All @@ -396,7 +425,7 @@ public static RestRequest AddXmlBody<T>(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));
}

/// <summary>
Expand Down
16 changes: 8 additions & 8 deletions src/RestSharp/RestClient.cs
Expand Up @@ -125,11 +125,12 @@ public partial class RestClient : IDisposable {

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) {
Expand All @@ -139,9 +140,8 @@ public partial class RestClient : IDisposable {
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 =
Expand Down Expand Up @@ -178,7 +178,7 @@ public partial class RestClient : IDisposable {
);

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));
}
Expand Down Expand Up @@ -237,4 +237,4 @@ internal void AssignAcceptedContentTypes()
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
2 changes: 1 addition & 1 deletion src/RestSharp/RestClientOptions.cs
Expand Up @@ -75,7 +75,7 @@ public class RestClientOptions {
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;

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

0 comments on commit cbcc74b

Please sign in to comment.