forked from Pathoschild/FluentHttpClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.cs
More file actions
104 lines (90 loc) · 4.41 KB
/
Response.cs
File metadata and controls
104 lines (90 loc) · 4.41 KB
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
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace Pathoschild.Http.Client.Internal
{
/// <summary>Asynchronously parses an HTTP response.</summary>
public sealed class Response : IResponse
{
/*********
** Fields
*********/
/// <summary>Whether the HTTP response was successful.</summary>
public bool IsSuccessStatusCode => this.Message.IsSuccessStatusCode;
/// <summary>The underlying HTTP response message.</summary>
public HttpResponseMessage Message { get; }
/// <summary>The formatters used for serializing and deserializing message bodies.</summary>
public MediaTypeFormatterCollection Formatters { get; }
/// <summary>The HTTP status code.</summary>
public HttpStatusCode Status => this.Message.StatusCode;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="message">The underlying HTTP request message.</param>
/// <param name="formatters">The formatters used for serializing and deserializing message bodies.</param>
public Response(HttpResponseMessage message, MediaTypeFormatterCollection formatters)
{
this.Message = message;
this.Formatters = formatters;
}
/// <summary>Asynchronously retrieve the response body as a deserialized model.</summary>
/// <typeparam name="T">The response model to deserialize into.</typeparam>
/// <exception cref="ApiException">An error occurred processing the response.</exception>
public Task<T> As<T>()
{
return this.Message.Content.ReadAsAsync<T>(this.Formatters);
}
/// <summary>Asynchronously retrieve the response body as a list of deserialized models.</summary>
/// <typeparam name="T">The response model to deserialize into.</typeparam>
/// <exception cref="ApiException">An error occurred processing the response.</exception>
public Task<T[]> AsArray<T>()
{
return this.As<T[]>();
}
/// <summary>Asynchronously retrieve the response body as an array of <see cref="byte"/>.</summary>
/// <returns>Returns the response body, or <c>null</c> if the response has no body.</returns>
/// <exception cref="ApiException">An error occurred processing the response.</exception>
public Task<byte[]> AsByteArray()
{
return this.Message.Content.ReadAsByteArrayAsync();
}
/// <summary>Asynchronously retrieve the response body as a <see cref="string"/>.</summary>
/// <returns>Returns the response body, or <c>null</c> if the response has no body.</returns>
/// <exception cref="ApiException">An error occurred processing the response.</exception>
public Task<string> AsString()
{
return this.Message.Content.ReadAsStringAsync();
}
/// <summary>Asynchronously retrieve the response body as a <see cref="Stream"/>.</summary>
/// <returns>Returns the response body, or <c>null</c> if the response has no body.</returns>
/// <exception cref="ApiException">An error occurred processing the response.</exception>
public async Task<Stream> AsStream()
{
Stream stream = await this.Message.Content.ReadAsStreamAsync().ConfigureAwait(false);
stream.Position = 0;
return stream;
}
/// <summary>Get a raw JSON representation of the response, which can also be accessed as a <c>dynamic</c> value.</summary>
public async Task<JToken> AsRawJson()
{
string content = await this.AsString();
return JToken.Parse(content);
}
/// <summary>Get a raw JSON object representation of the response, which can also be accessed as a <c>dynamic</c> value.</summary>
public async Task<JObject> AsRawJsonObject()
{
string content = await this.AsString();
return JObject.Parse(content);
}
/// <summary>Get a raw JSON array representation of the response, which can also be accessed as a <c>dynamic</c> value.</summary>
public async Task<JArray> AsRawJsonArray()
{
string content = await this.AsString();
return JArray.Parse(content);
}
}
}