Skip to content

Commit 79bf45c

Browse files
committed
Enhance AbstractKubernetes SendRequest extensibility
1 parent a763810 commit 79bf45c

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

src/KubernetesClient.Basic/AbstractKubernetes.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,6 @@ public override string ToString()
5454
}
5555
}
5656

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-
}
69-
7057
public virtual TimeSpan HttpClientTimeout { get; set; } = TimeSpan.FromSeconds(100);
7158

7259
protected virtual MediaTypeHeaderValue GetHeader(object body)
@@ -108,7 +95,7 @@ private MediaTypeHeaderValue GetHeader(V1Patch body)
10895

10996
protected abstract Task<HttpOperationResponse<T>> CreateResultAsync<T>(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken);
11097

111-
protected abstract HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders);
98+
protected abstract Task<HttpResponseMessage> SendRequest<T>(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders, T body, CancellationToken cancellationToken);
11299

113100
protected abstract Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken);
114101
}

src/KubernetesClient/Kubernetes.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected override async Task<HttpOperationResponse<T>> CreateResultAsync<T>(Htt
9999
return result;
100100
}
101101

102-
protected override HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders)
102+
protected override Task<HttpResponseMessage> SendRequest<T>(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders, T body, CancellationToken cancellationToken)
103103
{
104104
var httpRequest = new HttpRequestMessage();
105105
httpRequest.Method = method;
@@ -117,7 +117,15 @@ protected override HttpRequestMessage CreateRequest(string relativeUri, HttpMeth
117117
}
118118
}
119119

120-
return httpRequest;
120+
if (body != null)
121+
{
122+
var requestContent = KubernetesJson.Serialize(body);
123+
httpRequest.Content = new StringContent(requestContent, System.Text.Encoding.UTF8);
124+
httpRequest.Content.Headers.ContentType = GetHeader(body);
125+
return SendRequestRaw(requestContent, httpRequest, cancellationToken);
126+
}
127+
128+
return SendRequestRaw("", httpRequest, cancellationToken);
121129
}
122130

123131
protected override async Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken)

src/LibKubernetesGenerator/templates/Operations.cs.template

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,20 @@ public partial class AbstractKubernetes : I{{name}}Operations
5252
{{/IfListNotEmpty operation.parameters}}
5353

5454
// Create HTTP transport
55-
var httpRequest = CreateRequest(url, HttpMethods.{{Method}}, customHeaders);
5655
{{#IfParamContains operation "body"}}
57-
var httpResponse = await SendRequest(body, httpRequest, cancellationToken);
56+
var httpResponse = await SendRequest(url, HttpMethods.{{Method}}, customHeaders, body, cancellationToken);
5857
{{/IfParamContains operation "body"}}
5958
{{#IfParamDoesNotContain operation "body"}}
60-
var httpResponse = await SendRequestRaw("", httpRequest, cancellationToken);
59+
var httpResponse = await SendRequest<object>(url, HttpMethods.{{Method}}, customHeaders, null, cancellationToken);
6160
{{/IfParamDoesNotContain operation "body"}}
62-
// Create Result
61+
// Create Result
62+
var httpRequest = httpResponse.RequestMessage;
6363
{{#IfReturnType operation "void"}}
6464
HttpOperationResponse result = new HttpOperationResponse() { Request = httpRequest, Response = httpResponse };
6565
{{/IfReturnType operation "void"}}
6666
{{#IfReturnType operation "obj"}}
67-
var result = await CreateResultAsync{{GetReturnType operation "<>"}}(httpRequest,
67+
var result = await CreateResultAsync{{GetReturnType operation "<>"}}(
68+
httpRequest,
6869
httpResponse,
6970
{{#IfParamContains operation "watch"}}
7071
watch,

0 commit comments

Comments
 (0)