Skip to content

Commit 95f1324

Browse files
committed
Fixing bugs after exception handling changed
1 parent f6fefcb commit 95f1324

File tree

20 files changed

+101
-68
lines changed

20 files changed

+101
-68
lines changed

docs/support/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Here are contribution guidelines:
6262
- No licence header for tested.
6363
- Code must build for .NET Standard 2.0, .NET 5, and .NET 6.
6464
- Test must run on .NET 6.
65-
- Use `autocrlf=true` (`git config --global core.autocrlf true` [http://help.github.com/dealing-with-lineendings/])
65+
- Use `autocrlf=true` (`git config --global core.autocrlf true`)
6666

6767
### Sponsor
6868

src/RestSharp.Serializers.Xml/XmlDeserializer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ public class XmlDeserializer : IXmlDeserializer, IWithRootElement, IWithDateForm
3737
if (string.IsNullOrEmpty(response.Content))
3838
return default;
3939

40-
var doc = XDocument.Parse(response.Content);
41-
var root = doc.Root;
40+
var doc = XDocument.Parse(response.Content);
41+
var root = doc.Root;
42+
var rootElement = response.RootElement ?? RootElement;
4243

43-
if (RootElement != null && doc.Root != null)
44-
root = doc.Root.DescendantsAndSelf(RootElement.AsNamespaced(Namespace)).SingleOrDefault();
44+
if (rootElement != null && doc.Root != null)
45+
root = doc.Root.DescendantsAndSelf(rootElement.AsNamespaced(Namespace)).SingleOrDefault();
4546

4647
// autodetect xml namespace
4748
if (Namespace.IsEmpty())

src/RestSharp/Parameters/FileParameter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public record FileParameter {
4343
Name = name;
4444
FileName = fileName;
4545
GetFile = getFile;
46-
ContentType = contentType ?? "application/octet-stream";
46+
ContentType = contentType ?? Serializers.ContentType.Binary;
4747
}
4848

4949
/// <summary>
@@ -80,7 +80,7 @@ public static FileParameter Create(
8080
string fileName,
8181
string? contentType = null
8282
)
83-
=> new(name, fileName, getFile, contentType ?? Serializers.ContentType.File);
83+
=> new(name, fileName, getFile, contentType ?? Serializers.ContentType.Binary);
8484

8585
public static FileParameter FromFile(string fullPath, string? name = null, string? contentType = null) {
8686
if (!File.Exists(Ensure.NotEmptyString(fullPath, nameof(fullPath))))

src/RestSharp/Request/RestRequestExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,16 @@ public static RestRequest AddFile(
304304
public static RestRequest AddBody(this RestRequest request, object obj, string? contentType = null) {
305305
if (contentType == null) {
306306
return request.RequestFormat switch {
307-
DataFormat.Json => request.AddJsonBody(obj, contentType ?? ContentType.Json),
308-
DataFormat.Xml => request.AddXmlBody(obj, contentType ?? ContentType.Xml),
309-
_ => request.AddParameter(new BodyParameter("", obj.ToString()!, contentType ?? ContentType.Plain))
307+
DataFormat.Json => request.AddJsonBody(obj, contentType ?? ContentType.Json),
308+
DataFormat.Xml => request.AddXmlBody(obj, contentType ?? ContentType.Xml),
309+
DataFormat.Binary => request.AddParameter(new BodyParameter("", obj, contentType ?? ContentType.Binary)),
310+
_ => request.AddParameter(new BodyParameter("", obj.ToString()!, contentType ?? ContentType.Plain))
310311
};
311312
}
312313

313314
return
314315
obj is string str ? request.AddParameter(new BodyParameter("", str, contentType)) :
316+
obj is byte[] bytes ? request.AddParameter(new BodyParameter("", bytes, contentType)) :
315317
contentType.Contains("xml") ? request.AddXmlBody(obj, contentType) :
316318
contentType.Contains("json") ? request.AddJsonBody(obj, contentType) :
317319
throw new ArgumentException("Non-string body found with unsupported content type", nameof(obj));

src/RestSharp/Response/RestResponse.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public static RestResponse<T> FromResponse(RestResponse response)
5050
Server = response.Server,
5151
StatusCode = response.StatusCode,
5252
StatusDescription = response.StatusDescription,
53-
Request = response.Request
53+
Request = response.Request,
54+
RootElement = response.RootElement
5455
};
5556
}
5657

@@ -92,13 +93,18 @@ async Task<RestResponse> GetDefaultResponse() {
9293
Request = request,
9394
Headers = httpResponse.Headers.GetHeaderParameters(),
9495
ContentHeaders = httpResponse.Content.Headers.GetHeaderParameters(),
95-
Cookies = cookieCollection
96+
Cookies = cookieCollection,
97+
RootElement = request.RootElement
9698
};
9799

98100
Exception? MaybeException()
99101
=> httpResponse.IsSuccessStatusCode
100102
? null
103+
#if NETSTANDARD
101104
: new HttpRequestException($"Request failed with status code {httpResponse.StatusCode}");
105+
#else
106+
: new HttpRequestException($"Request failed with status code {httpResponse.StatusCode}", null, httpResponse.StatusCode);
107+
#endif
102108

103109
Task<Stream?> ReadResponse() => httpResponse.ReadResponse(cancellationToken);
104110

src/RestSharp/Response/RestResponseBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ public abstract class RestResponseBase {
120120
/// HTTP protocol version of the request
121121
/// </summary>
122122
public Version? Version { get; set; }
123+
124+
/// <summary>
125+
/// Root element of the serialized response content, only works if deserializer supports it
126+
/// </summary>
127+
public string? RootElement { get; set; }
123128

124129
/// <summary>
125130
/// Assists with debugging responses by displaying in the debugger output

src/RestSharp/RestClient.Async.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,13 @@ static RestResponse AddError(RestResponse response, Exception exception, Cancell
134134
return response;
135135
}
136136

137-
static RestResponse ThrowIfError(RestResponse response) {
137+
internal static RestResponse ThrowIfError(RestResponse response) {
138138
var exception = response.GetException();
139-
return exception != null ? throw exception : response;
140-
}
139+
if (exception != null) throw exception;
141140

141+
return response;
142+
}
143+
142144
static HttpMethod AsHttpMethod(Method method)
143145
=> method switch {
144146
Method.Get => HttpMethod.Get,

src/RestSharp/RestClient.Serialization.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ internal RestResponse<T> Deserialize<T>(RestRequest request, RestResponse raw) {
5454
// Only attempt to deserialize if the request has not errored due
5555
// to a transport or framework exception. HTTP errors should attempt to
5656
// be deserialized
57-
if (response.ErrorException == null) {
57+
if (response.Content != null) {
5858
var handler = GetContentDeserializer(raw, request.RequestFormat);
5959

6060
// Only continue if there is a handler defined else there is no way to deserialize the data.

src/RestSharp/RestClient.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ namespace RestSharp;
2525
/// <summary>
2626
/// Client to translate RestRequests into Http requests and process response result
2727
/// </summary>
28-
public partial class RestClient {
28+
public partial class RestClient : IDisposable {
2929
public CookieContainer CookieContainer { get; }
3030

31-
public string[] AcceptedContentTypes { get; private set; } = null!;
31+
/// <summary>
32+
/// Content types that will be sent in the Accept header. The list is populated from the known serializers.
33+
/// If you need to send something else by default, set this property to a different value.
34+
/// </summary>
35+
public string[] AcceptedContentTypes { get; set; } = null!;
3236

3337
HttpClient HttpClient { get; }
3438

@@ -168,7 +172,9 @@ void DoBuildUriValidations(RestRequest request) {
168172
if (Options.BaseUrl == null && !request.Resource.ToLowerInvariant().StartsWith("http"))
169173
throw new ArgumentOutOfRangeException(
170174
nameof(request),
171-
"Request resource doesn't contain a valid scheme for an empty client base URL"
175+
"Request resource doesn't contain a valid scheme for an empty base URL of the client"
172176
);
173177
}
178+
179+
public void Dispose() => HttpClient.Dispose();
174180
}

src/RestSharp/RestClientExtensions.Json.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static async Task<HttpStatusCode> PostJsonAsync<TRequest>(
108108
TRequest request,
109109
CancellationToken cancellationToken = default
110110
) where TRequest : class {
111-
var restRequest = new RestRequest().AddJsonBody(request);
111+
var restRequest = new RestRequest(resource).AddJsonBody(request);
112112
return client.PutAsync<TResponse>(restRequest, cancellationToken);
113113
}
114114

0 commit comments

Comments
 (0)