Skip to content

Commit fc995f9

Browse files
committed
Separating parameters, disable headers encoding
1 parent 4d76f55 commit fc995f9

24 files changed

+204
-123
lines changed

src/RestSharp/Authenticators/HttpBasicAuthenticator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ static string GetHeader(string username, string password, Encoding encoding)
3535

3636
// return ;
3737
protected override ValueTask<Parameter> GetAuthenticationParameter(string accessToken)
38-
=> new(new Parameter(KnownHeaders.Authorization, $"Basic {accessToken}", ParameterType.HttpHeader, false));
38+
=> new(new HeaderParameter(KnownHeaders.Authorization, $"Basic {accessToken}"));
3939
}

src/RestSharp/Authenticators/JwtAuthenticator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ public JwtAuthenticator(string accessToken) : base(GetToken(accessToken)) { }
3131
static string GetToken(string accessToken) => $"Bearer {Ensure.NotEmpty(accessToken, nameof(accessToken))}";
3232

3333
protected override ValueTask<Parameter> GetAuthenticationParameter(string accessToken)
34-
=> new(new Parameter(KnownHeaders.Authorization, accessToken, ParameterType.HttpHeader, false));
34+
=> new(new HeaderParameter(KnownHeaders.Authorization, accessToken));
3535
}

src/RestSharp/Authenticators/OAuth/OAuth1Authenticator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ void AddOAuthData(RestClient client, RestRequest request, OAuthWorkflow workflow
248248
request.AddOrUpdateParameters(oauthParameters);
249249

250250
IEnumerable<Parameter> CreateHeaderParameters()
251-
=> new[] { new Parameter(KnownHeaders.Authorization, GetAuthorizationHeader(), ParameterType.HttpHeader) };
251+
=> new[] { new HeaderParameter(KnownHeaders.Authorization, GetAuthorizationHeader()) };
252252

253253
IEnumerable<Parameter> CreateUrlParameters()
254-
=> oauth.Parameters.Select(p => new Parameter(p.Name, HttpUtility.UrlDecode(p.Value), ParameterType.GetOrPost));
254+
=> oauth.Parameters.Select(p => new GetOrPostParameter(p.Name, HttpUtility.UrlDecode(p.Value)));
255255

256256
string GetAuthorizationHeader() {
257257
var oathParameters =

src/RestSharp/Authenticators/OAuth2/OAuth2AuthorizationRequestHeaderAuthenticator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ public OAuth2AuthorizationRequestHeaderAuthenticator(string accessToken) : this(
3737
public OAuth2AuthorizationRequestHeaderAuthenticator(string accessToken, string tokenType) : base(accessToken) => _tokenType = tokenType;
3838

3939
protected override ValueTask<Parameter> GetAuthenticationParameter(string accessToken)
40-
=> new(new Parameter(KnownHeaders.Authorization, $"{_tokenType} {accessToken}", ParameterType.HttpHeader, false));
40+
=> new(new HeaderParameter(KnownHeaders.Authorization, $"{_tokenType} {accessToken}"));
4141
}

src/RestSharp/Authenticators/OAuth2/OAuth2UriQueryParameterAuthenticator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ public class OAuth2UriQueryParameterAuthenticator : AuthenticatorBase {
2828
public OAuth2UriQueryParameterAuthenticator(string accessToken) : base(accessToken) { }
2929

3030
protected override ValueTask<Parameter> GetAuthenticationParameter(string accessToken)
31-
=> new(new Parameter("oauth_token", accessToken, ParameterType.GetOrPost));
31+
=> new(new GetOrPostParameter("oauth_token", accessToken));
3232
}

src/RestSharp/Enum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public enum ParameterType {
2121
/// <summary>
2222
/// Cookie parameter
2323
/// </summary>
24-
Cookie, GetOrPost, UrlSegment, HttpHeader, RequestBody, QueryString, QueryStringWithoutEncode
24+
GetOrPost, UrlSegment, HttpHeader, RequestBody, QueryString
2525
}
2626

2727
/// <summary>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
namespace RestSharp;
17+
18+
public record BodyParameter : Parameter {
19+
public BodyParameter(string? name, object value, string contentType, DataFormat dataFormat = DataFormat.None)
20+
: base(name, Ensure.NotNull(value, nameof(value)), ParameterType.RequestBody) {
21+
ContentType = contentType;
22+
DataFormat = dataFormat;
23+
}
24+
25+
/// <summary>
26+
/// Body parameter data type
27+
/// </summary>
28+
public DataFormat DataFormat { get; init; } = DataFormat.None;
29+
}
30+
31+
public record XmlParameter : BodyParameter {
32+
public XmlParameter(string name, object value, string? xmlNamespace = null, string contentType = Serializers.ContentType.Xml)
33+
: base(name, value, contentType, DataFormat.Xml)
34+
=> XmlNamespace = xmlNamespace;
35+
36+
public string? XmlNamespace { get; }
37+
}
38+
39+
public record JsonParameter : BodyParameter {
40+
public JsonParameter(string name, object value, string contentType = Serializers.ContentType.Json)
41+
: base(name, value, contentType, DataFormat.Json) { }
42+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
namespace RestSharp;
17+
18+
public record GetOrPostParameter : NamedParameter {
19+
public GetOrPostParameter(string name, object? value, bool encode = true) : base(name, value, ParameterType.GetOrPost, encode) { }
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
namespace RestSharp;
17+
18+
public record HeaderParameter : Parameter {
19+
public HeaderParameter(string? name, object? value, bool encode = false) : base(name, value, ParameterType.HttpHeader, encode) { }
20+
}

src/RestSharp/Parameters/Parameter.cs

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,77 +12,62 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
namespace RestSharp;
15+
namespace RestSharp;
1616

1717
/// <summary>
1818
/// Parameter container for REST requests
1919
/// </summary>
2020
public record Parameter {
21-
public Parameter(string? name, object? value, ParameterType type, bool encode = true) {
22-
if (type != ParameterType.RequestBody)
23-
Ensure.NotEmpty(name, nameof(name));
24-
else {
25-
Ensure.NotNull(value, nameof(value));
26-
}
27-
21+
protected Parameter(string? name, object? value, ParameterType type, bool encode = true) {
2822
Name = name;
29-
Value = type != ParameterType.UrlSegment ? value : value?.ToString()?.Replace("%2F", "/").Replace("%2f", "/");
30-
Type = type == ParameterType.QueryStringWithoutEncode ? ParameterType.QueryString : type;
31-
Encode = type != ParameterType.QueryStringWithoutEncode && encode;
23+
Value = value;
24+
Type = type;
25+
Encode = encode;
3226
}
3327

34-
public Parameter(string name, object value, string contentType, ParameterType type, bool encode = true) : this(name, value, type, encode)
35-
=> ContentType = contentType;
28+
// Parameter(string name, object value, string contentType, ParameterType type, bool encode = true) : this(name, value, type, encode)
29+
// => ContentType = contentType;
3630

3731
/// <summary>
3832
/// Name of the parameter
3933
/// </summary>
40-
public string? Name { get; set; }
34+
public string? Name { get; }
4135

4236
/// <summary>
4337
/// Value of the parameter
4438
/// </summary>
45-
public object? Value { get; set; }
39+
public object? Value { get; }
4640

4741
/// <summary>
4842
/// Type of the parameter
4943
/// </summary>
50-
public ParameterType Type { get; set; }
44+
public ParameterType Type { get; }
5145

52-
/// <summary>
53-
/// Body parameter data type
54-
/// </summary>
55-
public DataFormat DataFormat { get; set; } = DataFormat.None;
46+
internal bool Encode { get; }
5647

5748
/// <summary>
5849
/// MIME content type of the parameter
5950
/// </summary>
60-
public string? ContentType { get; set; }
61-
62-
internal bool Encode { get; }
51+
public string? ContentType { get; protected init; }
6352

6453
/// <summary>
6554
/// Return a human-readable representation of this parameter
6655
/// </summary>
6756
/// <returns>String</returns>
6857
public override string ToString() => $"{Name}={Value}";
69-
}
7058

71-
public record XmlParameter : Parameter {
72-
public XmlParameter(string name, object value, string? xmlNamespace = null, string contentType = Serializers.ContentType.Xml)
73-
: base(name, value, ParameterType.RequestBody) {
74-
XmlNamespace = xmlNamespace;
75-
DataFormat = DataFormat.Xml;
76-
ContentType = contentType;
77-
}
78-
79-
public string? XmlNamespace { get; }
59+
public static Parameter CreateParameter(string? name, object value, ParameterType type, bool encode = true)
60+
=> type switch {
61+
ParameterType.GetOrPost => new GetOrPostParameter(name!, value, encode),
62+
ParameterType.UrlSegment => new UrlSegmentParameter(name!, value, encode),
63+
ParameterType.HttpHeader => new HeaderParameter(name, value, encode),
64+
ParameterType.RequestBody => new BodyParameter(name, value, Serializers.ContentType.Plain),
65+
ParameterType.QueryString => new QueryParameter(name!, value, encode),
66+
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
67+
};
8068
}
8169

82-
public record JsonParameter : Parameter {
83-
public JsonParameter(string name, object value, string contentType = Serializers.ContentType.Json)
84-
: base(name, value, ParameterType.RequestBody) {
85-
DataFormat = DataFormat.Json;
86-
ContentType = contentType;
87-
}
70+
public record NamedParameter : Parameter {
71+
protected NamedParameter(string name, object? value, ParameterType type, bool encode = true)
72+
: base(Ensure.NotEmptyString(name, nameof(name)), value, type, encode) { }
8873
}

0 commit comments

Comments
 (0)