Skip to content

Commit cec5f5d

Browse files
authored
use explicitly impl instead of new (#853)
* use explicitly impl instead of new * Apply suggestions from code review
1 parent bdcfbfa commit cec5f5d

File tree

7 files changed

+73
-115
lines changed

7 files changed

+73
-115
lines changed

src/KubernetesClient.Basic/AbstractKubernetes.cs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,71 @@ namespace k8s;
55

66
public abstract partial class AbstractKubernetes
77
{
8+
private static class HttpMethods
9+
{
10+
public static readonly HttpMethod Delete = HttpMethod.Delete;
11+
public static readonly HttpMethod Get = HttpMethod.Get;
12+
public static readonly HttpMethod Head = HttpMethod.Head;
13+
public static readonly HttpMethod Options = HttpMethod.Options;
14+
public static readonly HttpMethod Post = HttpMethod.Post;
15+
public static readonly HttpMethod Put = HttpMethod.Put;
16+
public static readonly HttpMethod Trace = HttpMethod.Trace;
17+
public static readonly HttpMethod Patch = new HttpMethod("Patch");
18+
}
19+
20+
private sealed class QueryBuilder
21+
{
22+
private List<string> parameters = new List<string>();
23+
24+
public void Append(string key, params object[] values)
25+
{
26+
foreach (var value in values)
27+
{
28+
switch (value)
29+
{
30+
case int intval:
31+
parameters.Add($"{key}={intval}");
32+
break;
33+
case string strval:
34+
parameters.Add($"{key}={Uri.EscapeDataString(strval)}");
35+
break;
36+
case bool boolval:
37+
parameters.Add($"{key}={(boolval ? "true" : "false")}");
38+
break;
39+
default:
40+
// null
41+
break;
42+
}
43+
}
44+
}
45+
46+
public override string ToString()
47+
{
48+
if (parameters.Count > 0)
49+
{
50+
return $"?{string.Join("&", parameters)}";
51+
}
852

53+
return "";
54+
}
55+
}
56+
57+
private Task<HttpResponseMessage> SendRequest<T>(T body, HttpRequestMessage httpRequest, CancellationToken cancellationToken)
58+
{
59+
if (body != null)
60+
{
61+
var requestContent = KubernetesJson.Serialize(body);
62+
httpRequest.Content = new StringContent(requestContent, System.Text.Encoding.UTF8);
63+
httpRequest.Content.Headers.ContentType = GetHeader(body);
64+
return SendRequestRaw(requestContent, httpRequest, cancellationToken);
65+
}
66+
67+
return SendRequestRaw("", httpRequest, cancellationToken);
68+
}
969

1070
public virtual TimeSpan HttpClientTimeout { get; set; } = TimeSpan.FromSeconds(100);
1171

12-
protected internal virtual MediaTypeHeaderValue GetHeader(object body)
72+
protected virtual MediaTypeHeaderValue GetHeader(object body)
1373
{
1474
if (body == null)
1575
{
@@ -46,9 +106,9 @@ private MediaTypeHeaderValue GetHeader(V1Patch body)
46106
}
47107
}
48108

49-
protected internal abstract Task<HttpOperationResponse<T>> CreateResultAsync<T>(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken);
109+
protected abstract Task<HttpOperationResponse<T>> CreateResultAsync<T>(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken);
50110

51-
protected internal abstract HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders);
111+
protected abstract HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders);
52112

53-
protected internal abstract Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken);
113+
protected abstract Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken);
54114
}

src/KubernetesClient.Basic/HttpMethods.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/KubernetesClient.Basic/Operations.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/KubernetesClient.Basic/QueryBuilder.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/KubernetesClient/Kubernetes.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private void Initialize()
5555
BaseUri = new Uri("http://localhost");
5656
}
5757

58-
protected internal override async Task<HttpOperationResponse<T>> CreateResultAsync<T>(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken)
58+
protected override async Task<HttpOperationResponse<T>> CreateResultAsync<T>(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken)
5959
{
6060
if (httpRequest == null)
6161
{
@@ -99,7 +99,7 @@ protected internal override async Task<HttpOperationResponse<T>> CreateResultAsy
9999
return result;
100100
}
101101

102-
protected internal override HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders)
102+
protected override HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders)
103103
{
104104
var httpRequest = new HttpRequestMessage();
105105
httpRequest.Method = method;
@@ -120,7 +120,7 @@ protected internal override HttpRequestMessage CreateRequest(string relativeUri,
120120
return httpRequest;
121121
}
122122

123-
protected internal override async Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken)
123+
protected override async Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken)
124124
{
125125
if (httpRequest == null)
126126
{

src/LibKubernetesGenerator/templates/AbstractKubernetes.cs.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ namespace k8s;
1111
public abstract partial class AbstractKubernetes
1212
{
1313
{{#.}}
14-
public I{{.}}Operations {{.}} => new {{.}}Operations(this);
14+
public I{{.}}Operations {{.}} => this;
1515
{{/.}}
1616
}

src/LibKubernetesGenerator/templates/Operations.cs.template

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,16 @@
66

77
namespace k8s;
88

9-
internal partial class {{name}}Operations : Operations, I{{name}}Operations
9+
public partial class AbstractKubernetes : I{{name}}Operations
1010
{
11-
12-
public {{name}}Operations(AbstractKubernetes kubernetes) : base(kubernetes)
13-
{
14-
}
15-
16-
1711
{{#apis}}
1812
/// <inheritdoc/>
19-
public async Task<HttpOperationResponse{{GetReturnType operation "<>"}}> {{GetMethodName operation "WithHttpMessagesAsync"}}(
13+
async Task<HttpOperationResponse{{GetReturnType operation "<>"}}> I{{name}}Operations.{{GetMethodName operation "WithHttpMessagesAsync"}}(
2014
{{#operation.parameters}}
21-
{{GetDotNetType .}} {{GetDotNetName . "true"}},
15+
{{GetDotNetType .}} {{GetDotNetName .}},
2216
{{/operation.parameters}}
23-
IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders = null,
24-
CancellationToken cancellationToken = default(CancellationToken))
17+
IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders,
18+
CancellationToken cancellationToken)
2519
{
2620
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
2721
cts.CancelAfter(HttpClientTimeout);

0 commit comments

Comments
 (0)