-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathRestResponse.cs
85 lines (65 loc) · 2.38 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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Net;
namespace RESTClient {
public abstract class Response {
public bool IsError { get; set; }
public string ErrorMessage { get; set; }
public string Url { get; set; }
public HttpStatusCode StatusCode { get; set; }
public string Content { get; set; }
protected Response(HttpStatusCode statusCode) {
this.StatusCode = statusCode;
this.IsError = !((int)statusCode >= 200 && (int)statusCode < 300);
}
// success
protected Response(HttpStatusCode statusCode, string url, string text) {
this.IsError = false;
this.Url = url;
this.ErrorMessage = null;
this.StatusCode = statusCode;
this.Content = text;
}
// failure
protected Response(string error, HttpStatusCode statusCode, string url, string text) {
this.IsError = true;
this.Url = url;
this.ErrorMessage = error;
this.StatusCode = statusCode;
this.Content = text;
}
}
public sealed class RestResponse : Response {
// success
public RestResponse(HttpStatusCode statusCode, string url, string text) : base(statusCode, url, text) {
}
// failure
public RestResponse(string error, HttpStatusCode statusCode, string url, string text) : base(error, statusCode, url, text) {
}
}
public sealed class RestResponse<T> : Response, IRestResponse<T> {
public T Data { get; set; }
// success
public RestResponse(HttpStatusCode statusCode, string url, string text, T data) : base(statusCode, url, text) {
this.Data = data;
}
public RestResponse(HttpStatusCode statusCode, string url, string text) : base(statusCode, url, text) {
}
// failure
public RestResponse(string error, HttpStatusCode statusCode, string url, string text) : base(error, statusCode, url, text) {
}
}
/// <summary>
/// Parsed JSON result could either be an object or an array of objects
/// </summary>
internal sealed class RestResult<T> : Response {
public T AnObject { get; set; }
public T[] AnArrayOfObjects { get; set; }
public RestResult(HttpStatusCode statusCode) : base(statusCode) {
}
}
internal sealed class RestResult : Response {
public RestResult(HttpStatusCode statusCode) : base(statusCode) {
}
}
}