Skip to content

Commit cdc020d

Browse files
committed
Merge branch 'dev' into v109
# Conflicts: # src/RestSharp/Request/RestRequestExtensions.cs
2 parents 90e70fc + f7d3984 commit cdc020d

File tree

11 files changed

+36
-28
lines changed

11 files changed

+36
-28
lines changed

docs/error-handling.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ Below you can find how different extensions deal with errors. Note that function
3636
| `ExecuteGetAsync` | No |
3737
| `ExecuteGetAsync<T>` | No |
3838
| `ExecutePostAsync` | No |
39-
| `ExecutePutAsync` | No |
40-
| `ExecuteGetAsync<T>` | No |
4139
| `ExecutePostAsync<T>` | No |
40+
| `ExecutePutAsync` | No |
4241
| `ExecutePutAsync<T>` | No |
4342
| `GetAsync` | Yes |
4443
| `GetAsync<T>` | Yes |

docs/usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ record TokenResponse {
3333

3434
Next, we create the authenticator itself. It needs the API key and API key secret to call the token endpoint using basic HTTP authentication. In addition, we can extend the list of parameters with the base URL to convert it to a more generic OAuth2 authenticator.
3535

36-
The easiest way to create an authenticator is to inherit from the `AuthanticatorBase` base class:
36+
The easiest way to create an authenticator is to inherit from the `AuthenticatorBase` base class:
3737

3838
```csharp
3939
public class TwitterAuthenticator : AuthenticatorBase {

docs/v107/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ mockHttp.When("http://localhost/api/user/*")
216216
.Respond("application/json", "{'name' : 'Test McGee'}"); // Respond with JSON
217217
218218
// Instantiate the client normally, but replace the message handler
219-
var client = new RestClient(...) { ConfigureMessageHandler = _ => mockHttp };
219+
var client = new RestClient(new RestClientOptions { ConfigureMessageHandler = _ => mockHttp });
220220

221221
var request = new RestRequest("http://localhost/api/user/1234");
222222
var response = await client.GetAsync(request);

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<ItemGroup>
2222
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
2323
<PackageReference Include="MinVer" Version="4.2.0" PrivateAssets="All"/>
24-
<PackageReference Include="JetBrains.Annotations" Version="2022.1.0" PrivateAssets="All"/>
24+
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" PrivateAssets="All"/>
2525
</ItemGroup>
2626
<ItemGroup>
2727
<None Include="$(RepoRoot)\restsharp.png" Pack="true" PackagePath="\"/>

src/RestSharp.Serializers.NewtonsoftJson/RestSharp.Serializers.NewtonsoftJson.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup>
3-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
3+
<PackageReference Include="Newtonsoft.Json" Version="13.0.2"/>
44
</ItemGroup>
55
<ItemGroup>
66
<ProjectReference Include="..\RestSharp\RestSharp.csproj"/>

src/RestSharp/Parameters/ObjectParser.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
namespace RestSharp;
1919

2020
static class ObjectParser {
21-
public static IEnumerable<(string Name, string? Value)> GetProperties(this object obj, params string[] includedProperties) {
21+
public static IEnumerable<ParsedParameter> GetProperties(this object obj, params string[] includedProperties) {
2222
// automatically create parameters from object props
2323
var type = obj.GetType();
2424
var props = type.GetProperties();
2525

26-
var properties = new List<(string Name, string? Value)>();
26+
var properties = new List<ParsedParameter>();
2727

2828
foreach (var prop in props.Where(x => IsAllowedProperty(x.Name))) {
2929
var val = prop.GetValue(obj, null);
@@ -38,14 +38,14 @@ static class ObjectParser {
3838

3939
string? ParseValue(string? format, object? value) => format == null ? value?.ToString() : string.Format($"{{0:{format}}}", value);
4040

41-
IEnumerable<(string, string?)> GetArray(PropertyInfo propertyInfo, object? value) {
41+
IEnumerable<ParsedParameter> GetArray(PropertyInfo propertyInfo, object? value) {
4242
var elementType = propertyInfo.PropertyType.GetElementType();
4343
var array = (Array)value!;
4444

4545
var attribute = propertyInfo.GetCustomAttribute<RequestPropertyAttribute>();
46-
var name = attribute?.Name ?? propertyInfo.Name;
47-
46+
var name = attribute?.Name ?? propertyInfo.Name;
4847
var queryType = attribute?.ArrayQueryType ?? RequestArrayQueryType.CommaSeparated;
48+
var encode = attribute?.Encode ?? true;
4949

5050
if (array.Length > 0 && elementType != null) {
5151
// convert the array to an array of strings
@@ -54,21 +54,20 @@ static class ObjectParser {
5454
.Select(item => ParseValue(attribute?.Format, item));
5555

5656
return queryType switch {
57-
RequestArrayQueryType.CommaSeparated => new (string, string?)[] { (name, string.Join(",", values)) },
58-
RequestArrayQueryType.ArrayParameters => values.Select(x => ($"{name}[]", x)),
57+
RequestArrayQueryType.CommaSeparated => new[] { new ParsedParameter(name, string.Join(",", values), encode) },
58+
RequestArrayQueryType.ArrayParameters => values.Select(x => new ParsedParameter($"{name}[]", x, encode)),
5959
_ => throw new ArgumentOutOfRangeException()
6060
};
61-
6261
}
6362

64-
return new (string, string?)[] { (name, null) };
63+
return new ParsedParameter[] { new(name, null, encode) };
6564
}
6665

67-
(string, string?) GetValue(PropertyInfo propertyInfo, object? value) {
66+
ParsedParameter GetValue(PropertyInfo propertyInfo, object? value) {
6867
var attribute = propertyInfo.GetCustomAttribute<RequestPropertyAttribute>();
6968
var name = attribute?.Name ?? propertyInfo.Name;
7069
var val = ParseValue(attribute?.Format, value);
71-
return (name, val);
70+
return new ParsedParameter(name, val, attribute?.Encode ?? true);
7271
}
7372

7473
bool IsAllowedProperty(string propertyName)
@@ -78,11 +77,14 @@ bool IsAllowedProperty(string propertyName)
7877
}
7978
}
8079

80+
record ParsedParameter(string Name, string? Value, bool Encode);
81+
8182
[AttributeUsage(AttributeTargets.Property)]
8283
public class RequestPropertyAttribute : Attribute {
8384
public string? Name { get; set; }
8485
public string? Format { get; set; }
8586
public RequestArrayQueryType ArrayQueryType { get; set; } = RequestArrayQueryType.CommaSeparated;
87+
public bool Encode { get; set; } = true;
8688
}
8789

8890
public enum RequestArrayQueryType { CommaSeparated, ArrayParameters }

src/RestSharp/Request/RestRequestExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,19 +284,19 @@ public static RestRequest AddFile(
284284
/// <param name="request">Request instance</param>
285285
/// <param name="name">Parameter name</param>
286286
/// <param name="bytes">File content as bytes</param>
287-
/// <param name="filename">File name</param>
287+
/// <param name="fileName">File name</param>
288288
/// <param name="contentType">Optional: content type. Default is "application/octet-stream"</param>
289289
/// <param name="options">File parameter header options</param>
290290
/// <returns></returns>
291291
public static RestRequest AddFile(
292292
this RestRequest request,
293293
string name,
294294
byte[] bytes,
295-
string filename,
295+
string fileName,
296296
ContentType? contentType = null,
297297
FileParameterOptions? options = null
298298
)
299-
=> request.AddFile(FileParameter.Create(name, bytes, filename, contentType, options));
299+
=> request.AddFile(FileParameter.Create(name, bytes, fileName, contentType, options));
300300

301301
/// <summary>
302302
/// Adds a file attachment to the request, where the file content will be retrieved from a given stream
@@ -408,8 +408,8 @@ public static RestRequest AddXmlBody<T>(this RestRequest request, T obj, Content
408408
public static RestRequest AddObject<T>(this RestRequest request, T obj, params string[] includedProperties) where T : class {
409409
var props = obj.GetProperties(includedProperties);
410410

411-
foreach (var (name, value) in props) {
412-
request.AddParameter(name, value);
411+
foreach (var prop in props) {
412+
request.AddParameter(prop.Name, prop.Value, prop.Encode);
413413
}
414414

415415
return request;

src/RestSharp/Response/RestResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ async Task<RestResponse> GetDefaultResponse() {
9191
ContentType = httpResponse.Content.Headers.ContentType?.MediaType,
9292
ResponseStatus = calculateResponseStatus(httpResponse),
9393
ErrorException = httpResponse.MaybeException(),
94-
ResponseUri = httpResponse.RequestMessage!.RequestUri,
94+
ResponseUri = httpResponse.RequestMessage?.RequestUri,
9595
Server = httpResponse.Headers.Server.ToString(),
9696
StatusCode = httpResponse.StatusCode,
9797
StatusDescription = httpResponse.ReasonPhrase,

src/RestSharp/RestClient.Async.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ static RestResponse GetErrorResponse(RestRequest request, Exception exception, C
8181
async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationToken cancellationToken) {
8282
Ensure.NotNull(request, nameof(request));
8383

84+
// Make sure we are not disposed of when someone tries to call us!
85+
if (_disposed) {
86+
throw new ObjectDisposedException(nameof(RestClient));
87+
}
88+
8489
using var requestContent = new RequestContent(this, request);
8590

8691
var authenticator = request.Authenticator ?? Options.Authenticator;

src/RestSharp/RestClient.Extensions.Json.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ public static partial class RestClientExtensions {
4747
object parameters,
4848
CancellationToken cancellationToken = default
4949
) {
50-
var props = parameters.GetProperties();
50+
var props = parameters.GetProperties();
5151
var request = new RestRequest(resource);
5252

53-
foreach (var (name, value) in props) {
54-
Parameter parameter = resource.Contains($"{name}") ? new UrlSegmentParameter(name, value!) : new QueryParameter(name, value);
53+
foreach (var prop in props) {
54+
Parameter parameter = resource.Contains($"{prop.Name}")
55+
? new UrlSegmentParameter(prop.Name, prop.Value!, prop.Encode)
56+
: new QueryParameter(prop.Name, prop.Value, prop.Encode);
5557
request.AddParameter(parameter);
5658
}
5759

@@ -141,4 +143,4 @@ public static async Task<HttpStatusCode> PutJsonAsync<TRequest>(
141143
var response = await client.PutAsync(restRequest, cancellationToken).ConfigureAwait(false);
142144
return response.StatusCode;
143145
}
144-
}
146+
}

0 commit comments

Comments
 (0)