-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKubernetesResponse.cs
More file actions
95 lines (84 loc) · 3.83 KB
/
KubernetesResponse.cs
File metadata and controls
95 lines (84 loc) · 3.83 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
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using k8s.Models;
using Newtonsoft.Json;
namespace k8s.Fluent
{
/// <summary>Represents a response to a <see cref="KubernetesRequest"/>.</summary>
public sealed class KubernetesResponse : IDisposable
{
/// <summary>Initializes a new <see cref="KubernetesResponse"/> from an <see cref="HttpResponseMessage"/>.</summary>
public KubernetesResponse(HttpResponseMessage message) => Message = message ?? throw new ArgumentNullException(nameof(message));
/// <summary>Indicates whether the server returned an error response.</summary>
public bool IsError => (int)StatusCode >= 400;
/// <summary>Indicates whether the server returned a 404 Not Found response.</summary>
public bool IsNotFound => StatusCode == HttpStatusCode.NotFound;
/// <summary>Gets the underlying <see cref="HttpResponseMessage"/>.</summary>
public HttpResponseMessage Message { get; }
/// <summary>Gets the <see cref="HttpStatusCode"/> of the response.</summary>
public HttpStatusCode StatusCode => Message.StatusCode;
/// <inheritdoc/>
public void Dispose() => Message.Dispose();
/// <summary>Returns the response body as a string.</summary>
public async Task<string> GetBodyAsync()
{
if(body == null)
{
body = Message.Content != null ? await Message.Content.ReadAsStringAsync().ConfigureAwait(false) : string.Empty;
}
return body;
}
/// <summary>Deserializes the response body from JSON as a value of the given type, or null if the response body is empty.</summary>
/// <param name="type">The type of object to return</param>
/// <param name="throwIfEmpty">If false, an empty response body will be returned as null. If true, an exception will be thrown if
/// the body is empty. The default is false.
/// </param>
/// <exception cref="InvalidOperationException">Thrown if <paramref name="throwIfEmpty"/> is true and the response body was empty</exception>
public async Task<object> GetBodyAsync(Type type, bool throwIfEmpty = false)
{
string body = await GetBodyAsync().ConfigureAwait(false);
if(string.IsNullOrWhiteSpace(body))
{
if(!throwIfEmpty) throw new InvalidOperationException("The response body was empty.");
return null;
}
return FluentExtensions.Deserialize(body, type);
}
/// <summary>Deserializes the response body from JSON as a value of type <typeparamref name="T"/>, or the default value of
/// type <typeparamref name="T"/> if the response body is empty.
/// </summary>
/// <param name="throwIfEmpty">If false, an empty response body will be returned as the default value of type
/// <typeparamref name="T"/>. If true, an exception will be thrown if the body is empty. The default is false.
/// </param>
/// <exception cref="InvalidOperationException">Thrown if <paramref name="throwIfEmpty"/> is true and the response body was empty</exception>
public async Task<T> GetBodyAsync<T>(bool throwIfEmpty = false)
{
string body = await GetBodyAsync().ConfigureAwait(false);
if(string.IsNullOrWhiteSpace(body))
{
if(throwIfEmpty) throw new InvalidOperationException("The response body was empty.");
return default(T);
}
return (T)FluentExtensions.Deserialize(body, typeof(T));
}
/// <summary>Deserializes the response body as a <see cref="V1Status"/> object, or creates one from the status code if the
/// response body is not a JSON object.
/// </summary>
public async Task<V1Status> GetStatusAsync()
{
try
{
var status = await GetBodyAsync<V1Status>().ConfigureAwait(false);
if(status != null && (status.Status == "Success" || status.Status == "Failure")) return status;
}
catch(JsonException) { }
return new V1Status()
{
Status = IsError ? "Failure" : "Success", Code = (int)StatusCode, Reason = StatusCode.ToString(), Message = body
};
}
string body;
}
}