/
RestResponse.cs
215 lines (189 loc) · 8.01 KB
/
RestResponse.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using RestSharp.Extensions;
namespace RestSharp
{
/// <summary>
/// Base class for common properties shared by RestResponse and RestResponse[[T]]
/// </summary>
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + "()}")]
public abstract class RestResponseBase
{
string _content;
/// <summary>
/// Default constructor
/// </summary>
protected RestResponseBase()
{
ResponseStatus = ResponseStatus.None;
Headers = new List<Parameter>();
Cookies = new List<RestResponseCookie>();
}
/// <summary>
/// The RestRequest that was made to get this RestResponse
/// </summary>
/// <remarks>
/// Mainly for debugging if ResponseStatus is not OK
/// </remarks>
public IRestRequest Request { get; set; }
/// <summary>
/// MIME content type of response
/// </summary>
public string ContentType { get; set; }
/// <summary>
/// Length in bytes of the response content
/// </summary>
public long ContentLength { get; set; }
/// <summary>
/// Encoding of the response content
/// </summary>
public string ContentEncoding { get; set; }
/// <summary>
/// String representation of response content
/// </summary>
public string Content
{
get => _content ??= RawBytes.AsString(ContentEncoding);
set => _content = value;
}
/// <summary>
/// HTTP response status code
/// </summary>
public HttpStatusCode StatusCode { get; set; }
/// <summary>
/// Whether or not the response status code indicates success
/// </summary>
public bool IsSuccessful => (int) StatusCode >= 200 &&
(int) StatusCode <= 299 &&
ResponseStatus == ResponseStatus.Completed;
/// <summary>
/// Description of HTTP status returned
/// </summary>
public string StatusDescription { get; set; }
/// <summary>
/// Response content
/// </summary>
public byte[] RawBytes { get; set; }
/// <summary>
/// The URL that actually responded to the content (different from request if redirected)
/// </summary>
public Uri ResponseUri { get; set; }
/// <summary>
/// HttpWebResponse.Server
/// </summary>
public string Server { get; set; }
/// <summary>
/// Cookies returned by server with the response
/// </summary>
public IList<RestResponseCookie> Cookies { get; protected internal set; }
/// <summary>
/// Headers returned by server with the response
/// </summary>
public IList<Parameter> Headers { get; protected internal set; }
/// <summary>
/// Status of the request. Will return Error for transport errors.
/// HTTP errors will still return ResponseStatus.Completed, check StatusCode instead
/// </summary>
public ResponseStatus ResponseStatus { get; set; }
/// <summary>
/// Transport or other non-HTTP error generated while attempting request
/// </summary>
public string ErrorMessage { get; set; }
/// <summary>
/// The exception thrown during the request, if any
/// </summary>
public Exception? ErrorException { get; set; }
/// <summary>
/// The HTTP protocol version (1.0, 1.1, etc)
/// </summary>
/// <remarks>Only set when underlying framework supports it.</remarks>
public Version ProtocolVersion { get; set; }
/// <summary>
/// Assists with debugging responses by displaying in the debugger output
/// </summary>
/// <returns></returns>
protected string DebuggerDisplay() => $"StatusCode: {StatusCode}, Content-Type: {ContentType}, Content-Length: {ContentLength})";
}
/// <summary>
/// Container for data sent back from API including deserialized data
/// </summary>
/// <typeparam name="T">Type of data to deserialize to</typeparam>
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + "()}")]
public class RestResponse<T> : RestResponseBase, IRestResponse<T>
{
/// <summary>
/// Deserialized entity data
/// </summary>
public T Data { get; set; } = default!;
public static explicit operator RestResponse<T>(RestResponse response)
=> new RestResponse<T>
{
ContentEncoding = response.ContentEncoding,
ContentLength = response.ContentLength,
ContentType = response.ContentType,
Cookies = response.Cookies,
ErrorMessage = response.ErrorMessage,
ErrorException = response.ErrorException,
Headers = response.Headers,
RawBytes = response.RawBytes,
ResponseStatus = response.ResponseStatus,
ResponseUri = response.ResponseUri,
ProtocolVersion = response.ProtocolVersion,
Server = response.Server,
StatusCode = response.StatusCode,
StatusDescription = response.StatusDescription,
Request = response.Request
};
}
/// <summary>
/// Container for data sent back from API
/// </summary>
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + "()}")]
public class RestResponse : RestResponseBase, IRestResponse
{
RestResponse SetHeaders(IEnumerable<HttpHeader> headers)
=> this.With(
x => x.Headers = headers.Select(p => new Parameter(p.Name, p.Value, ParameterType.HttpHeader)).ToList()
);
RestResponse SetCookies(IEnumerable<HttpCookie> cookies)
=> this.With(
x => x.Cookies = cookies.Select(RestResponseCookie.FromHttpCookie).ToList()
);
internal static RestResponse FromHttpResponse(IHttpResponse httpResponse, IRestRequest request)
=> new RestResponse
{
Content = httpResponse.Content,
ContentEncoding = httpResponse.ContentEncoding,
ContentLength = httpResponse.ContentLength,
ContentType = httpResponse.ContentType,
ErrorException = httpResponse.ErrorException,
ErrorMessage = httpResponse.ErrorMessage,
RawBytes = httpResponse.RawBytes,
ResponseStatus = httpResponse.ResponseStatus,
ResponseUri = httpResponse.ResponseUri,
ProtocolVersion = httpResponse.ProtocolVersion,
Server = httpResponse.Server,
StatusCode = httpResponse.StatusCode,
StatusDescription = httpResponse.StatusDescription,
Request = request
}
.SetHeaders(httpResponse.Headers)
.SetCookies(httpResponse.Cookies);
}
}