Skip to content

Commit 0593de2

Browse files
committed
Move to Kubernetes.Fluent namespace
1 parent a5d691b commit 0593de2

File tree

3 files changed

+107
-102
lines changed

3 files changed

+107
-102
lines changed

src/KubernetesClient/Kubernetes.Fluent.cs renamed to src/KubernetesClient/Fluent/Kubernetes.Fluent.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,48 @@
22
using System.Net.Http;
33
using k8s.Models;
44

5-
namespace k8s
5+
namespace k8s.Fluent
66
{
7-
public partial class Kubernetes
7+
public static class KubernetesFluent
88
{
99
/// <summary>Creates a new Kubernetes object of the given type and sets its <see cref="IKubernetesObject.ApiVersion"/> and
1010
/// <see cref="IKubernetesObject.Kind"/>.
1111
/// </summary>
12-
public T New<T>() where T : IKubernetesObject, new() => Scheme.New<T>();
12+
public static T New<T>(this Kubernetes client) where T : IKubernetesObject, new() => client.Scheme.New<T>();
1313

1414
/// <summary>Creates a new Kubernetes object of the given type and sets its <see cref="IKubernetesObject.ApiVersion"/>,
1515
/// <see cref="IKubernetesObject.Kind"/>, and <see cref="V1ObjectMeta.Name"/>.
1616
/// </summary>
17-
public T New<T>(string name) where T : IKubernetesObject<V1ObjectMeta>, new() => Scheme.New<T>(name);
17+
public static T New<T>(this Kubernetes client, string name) where T : IKubernetesObject<V1ObjectMeta>, new() => client.Scheme.New<T>(name);
1818

1919
/// <summary>Creates a new Kubernetes object of the given type and sets its <see cref="IKubernetesObject.ApiVersion"/>,
2020
/// <see cref="IKubernetesObject.Kind"/>, <see cref="V1ObjectMeta.Namespace"/>, and <see cref="V1ObjectMeta.Name"/>.
2121
/// </summary>
22-
public T New<T>(string ns, string name) where T : IKubernetesObject<V1ObjectMeta>, new() => Scheme.New<T>(ns, name);
22+
public static T New<T>(this Kubernetes client, string ns, string name) where T : IKubernetesObject<V1ObjectMeta>, new() => client.Scheme.New<T>(ns, name);
2323

2424
/// <summary>Creates a new <see cref="KubernetesRequest"/> using the given <see cref="HttpMethod"/>
2525
/// (<see cref="HttpMethod.Get"/> by default).
2626
/// </summary>
27-
public KubernetesRequest Request(HttpMethod method = null) => new KubernetesRequest(this).Method(method);
27+
public static KubernetesRequest Request(this Kubernetes client, HttpMethod method = null) => new KubernetesRequest(client).Method(method);
2828

2929
/// <summary>Creates a new <see cref="KubernetesRequest"/> using the given <see cref="HttpMethod"/>
3030
/// and resource URI components.
3131
/// </summary>
32-
public KubernetesRequest Request(
32+
public static KubernetesRequest Request(this Kubernetes client,
3333
HttpMethod method, string type = null, string ns = null, string name = null, string group = null, string version = null) =>
34-
new KubernetesRequest(this).Method(method).Group(group).Version(version).Type(type).Namespace(ns).Name(name);
34+
new KubernetesRequest(client).Method(method).Group(group).Version(version).Type(type).Namespace(ns).Name(name);
3535

3636
/// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given type of object.</summary>
37-
public KubernetesRequest Request(Type type) => new KubernetesRequest(this).GVK(type);
37+
public static KubernetesRequest Request(this Kubernetes client, Type type) => new KubernetesRequest(client).GVK(type);
3838

3939
/// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given type of object with an optional name and namespace.</summary>
40-
public KubernetesRequest Request(HttpMethod method, Type type, string ns = null, string name = null) =>
41-
Request(method).GVK(type).Namespace(ns).Name(name);
40+
public static KubernetesRequest Request(this Kubernetes client, HttpMethod method, Type type, string ns = null, string name = null) =>
41+
Request(client, method).GVK(type).Namespace(ns).Name(name);
4242

4343
/// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given type of object with an optional name and namespace.</summary>
44-
public KubernetesRequest Request<T>(string ns = null, string name = null) => Request(null, typeof(T), ns, name);
44+
public static KubernetesRequest Request<T>(this Kubernetes client, string ns = null, string name = null) => Request(client, null, typeof(T), ns, name);
4545

4646
/// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given object.</summary>
47-
public KubernetesRequest Request(IKubernetesObject obj, bool setBody = true) => new KubernetesRequest(this).Set(obj, setBody);
47+
public static KubernetesRequest Request(this Kubernetes client, IKubernetesObject obj, bool setBody = true) => new KubernetesRequest(client).Set(obj, setBody);
4848
}
4949
}

src/KubernetesClient/KubernetesRequest.cs renamed to src/KubernetesClient/Fluent/KubernetesRequest.cs

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
using Microsoft.Rest;
1313
using Newtonsoft.Json;
1414

15-
namespace k8s
15+
namespace k8s.Fluent
1616
{
17-
#region KubernetesRequest
1817
/// <summary>Represents a single request to Kubernetes.</summary>
1918
public sealed class KubernetesRequest : ICloneable
2019
{
@@ -687,91 +686,4 @@ static string CheckHeaderName(string name)
687686

688687
static string NormalizeEmpty(string value) => string.IsNullOrEmpty(value) ? null : value; // normalizes empty strings to null
689688
}
690-
#endregion
691-
692-
#region KubernetesResponse
693-
/// <summary>Represents a response to a <see cref="KubernetesRequest"/>.</summary>
694-
public sealed class KubernetesResponse : IDisposable
695-
{
696-
/// <summary>Initializes a new <see cref="KubernetesResponse"/> from an <see cref="HttpResponseMessage"/>.</summary>
697-
public KubernetesResponse(HttpResponseMessage message) => Message = message ?? throw new ArgumentNullException(nameof(message));
698-
699-
/// <summary>Indicates whether the server returned an error response.</summary>
700-
public bool IsError => (int)StatusCode >= 400;
701-
702-
/// <summary>Indicates whether the server returned a 404 Not Found response.</summary>
703-
public bool IsNotFound => StatusCode == HttpStatusCode.NotFound;
704-
705-
/// <summary>Gets the underlying <see cref="HttpResponseMessage"/>.</summary>
706-
public HttpResponseMessage Message { get; }
707-
708-
/// <summary>Gets the <see cref="HttpStatusCode"/> of the response.</summary>
709-
public HttpStatusCode StatusCode => Message.StatusCode;
710-
711-
/// <inheritdoc/>
712-
public void Dispose() => Message.Dispose();
713-
714-
/// <summary>Returns the response body as a string.</summary>
715-
public async Task<string> GetBodyAsync()
716-
{
717-
if (body == null)
718-
{
719-
body = Message.Content != null ? await Message.Content.ReadAsStringAsync().ConfigureAwait(false) : string.Empty;
720-
}
721-
return body;
722-
}
723-
724-
/// <summary>Deserializes the response body from JSON as a value of the given type, or null if the response body is empty.</summary>
725-
/// <param name="type">The type of object to return</param>
726-
/// <param name="failIfEmpty">If false, an empty response body will be returned as null. If true, an exception will be thrown if
727-
/// the body is empty. The default is false.
728-
/// </param>
729-
public async Task<object> GetBodyAsync(Type type, bool failIfEmpty = false)
730-
{
731-
string body = await GetBodyAsync().ConfigureAwait(false);
732-
if (string.IsNullOrWhiteSpace(body))
733-
{
734-
if (!failIfEmpty) throw new InvalidOperationException("The response body was empty.");
735-
return null;
736-
}
737-
return JsonConvert.DeserializeObject(body, type, Kubernetes.DefaultJsonSettings);
738-
}
739-
740-
/// <summary>Deserializes the response body from JSON as a value of type <typeparamref name="T"/>, or the default value of
741-
/// type <typeparamref name="T"/> if the response body is empty.
742-
/// </summary>
743-
/// <param name="failIfEmpty">If false, an empty response body will be returned as the default value of type
744-
/// <typeparamref name="T"/>. If true, an exception will be thrown if the body is empty. The default is false.
745-
/// </param>
746-
public async Task<T> GetBodyAsync<T>(bool failIfEmpty = false)
747-
{
748-
string body = await GetBodyAsync().ConfigureAwait(false);
749-
if (string.IsNullOrWhiteSpace(body))
750-
{
751-
if (failIfEmpty) throw new InvalidOperationException("The response body was empty.");
752-
return default(T);
753-
}
754-
return JsonConvert.DeserializeObject<T>(body, Kubernetes.DefaultJsonSettings);
755-
}
756-
757-
/// <summary>Deserializes the response body as a <see cref="V1Status"/> object, or creates one from the status code if the
758-
/// response body is not a JSON object.
759-
/// </summary>
760-
public async Task<V1Status> GetStatusAsync()
761-
{
762-
try
763-
{
764-
var status = await GetBodyAsync<V1Status>().ConfigureAwait(false);
765-
if (status != null && (status.Status == "Success" || status.Status == "Failure")) return status;
766-
}
767-
catch (JsonException) { }
768-
return new V1Status()
769-
{
770-
Status = IsError ? "Failure" : "Success", Code = (int)StatusCode, Reason = StatusCode.ToString(), Message = body
771-
};
772-
}
773-
774-
string body;
775-
}
776-
#endregion
777689
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Net;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using k8s.Models;
6+
using Newtonsoft.Json;
7+
8+
namespace k8s.Fluent
9+
{
10+
/// <summary>Represents a response to a <see cref="KubernetesRequest"/>.</summary>
11+
public sealed class KubernetesResponse : IDisposable
12+
{
13+
/// <summary>Initializes a new <see cref="KubernetesResponse"/> from an <see cref="HttpResponseMessage"/>.</summary>
14+
public KubernetesResponse(HttpResponseMessage message) => Message = message ?? throw new ArgumentNullException(nameof(message));
15+
16+
/// <summary>Indicates whether the server returned an error response.</summary>
17+
public bool IsError => (int)StatusCode >= 400;
18+
19+
/// <summary>Indicates whether the server returned a 404 Not Found response.</summary>
20+
public bool IsNotFound => StatusCode == HttpStatusCode.NotFound;
21+
22+
/// <summary>Gets the underlying <see cref="HttpResponseMessage"/>.</summary>
23+
public HttpResponseMessage Message { get; }
24+
25+
/// <summary>Gets the <see cref="HttpStatusCode"/> of the response.</summary>
26+
public HttpStatusCode StatusCode => Message.StatusCode;
27+
28+
/// <inheritdoc/>
29+
public void Dispose() => Message.Dispose();
30+
31+
/// <summary>Returns the response body as a string.</summary>
32+
public async Task<string> GetBodyAsync()
33+
{
34+
if (body == null)
35+
{
36+
body = Message.Content != null ? await Message.Content.ReadAsStringAsync().ConfigureAwait(false) : string.Empty;
37+
}
38+
return body;
39+
}
40+
41+
/// <summary>Deserializes the response body from JSON as a value of the given type, or null if the response body is empty.</summary>
42+
/// <param name="type">The type of object to return</param>
43+
/// <param name="failIfEmpty">If false, an empty response body will be returned as null. If true, an exception will be thrown if
44+
/// the body is empty. The default is false.
45+
/// </param>
46+
public async Task<object> GetBodyAsync(Type type, bool failIfEmpty = false)
47+
{
48+
string body = await GetBodyAsync().ConfigureAwait(false);
49+
if (string.IsNullOrWhiteSpace(body))
50+
{
51+
if (!failIfEmpty) throw new InvalidOperationException("The response body was empty.");
52+
return null;
53+
}
54+
return JsonConvert.DeserializeObject(body, type, Kubernetes.DefaultJsonSettings);
55+
}
56+
57+
/// <summary>Deserializes the response body from JSON as a value of type <typeparamref name="T"/>, or the default value of
58+
/// type <typeparamref name="T"/> if the response body is empty.
59+
/// </summary>
60+
/// <param name="failIfEmpty">If false, an empty response body will be returned as the default value of type
61+
/// <typeparamref name="T"/>. If true, an exception will be thrown if the body is empty. The default is false.
62+
/// </param>
63+
public async Task<T> GetBodyAsync<T>(bool failIfEmpty = false)
64+
{
65+
string body = await GetBodyAsync().ConfigureAwait(false);
66+
if (string.IsNullOrWhiteSpace(body))
67+
{
68+
if (failIfEmpty) throw new InvalidOperationException("The response body was empty.");
69+
return default(T);
70+
}
71+
return JsonConvert.DeserializeObject<T>(body, Kubernetes.DefaultJsonSettings);
72+
}
73+
74+
/// <summary>Deserializes the response body as a <see cref="V1Status"/> object, or creates one from the status code if the
75+
/// response body is not a JSON object.
76+
/// </summary>
77+
public async Task<V1Status> GetStatusAsync()
78+
{
79+
try
80+
{
81+
var status = await GetBodyAsync<V1Status>().ConfigureAwait(false);
82+
if (status != null && (status.Status == "Success" || status.Status == "Failure")) return status;
83+
}
84+
catch (JsonException) { }
85+
return new V1Status()
86+
{
87+
Status = IsError ? "Failure" : "Success", Code = (int)StatusCode, Reason = StatusCode.ToString(), Message = body
88+
};
89+
}
90+
91+
string body;
92+
}
93+
}

0 commit comments

Comments
 (0)