Skip to content

Commit dba7b97

Browse files
authored
move getheader to abstract (#807)
1 parent 8e86191 commit dba7b97

File tree

2 files changed

+38
-48
lines changed

2 files changed

+38
-48
lines changed

src/KubernetesClient.Basic/AbstractKubernetes.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading;
33
using System.Threading.Tasks;
44
using k8s.Autorest;
5+
using k8s.Models;
56
using System.Net.Http.Headers;
67

78

@@ -61,12 +62,47 @@ private Task<HttpResponseMessage> SendRequest<T>(T body, HttpRequestMessage http
6162

6263
public virtual TimeSpan HttpClientTimeout { get; set; } = TimeSpan.FromSeconds(100);
6364

65+
protected virtual MediaTypeHeaderValue GetHeader(object body)
66+
{
67+
if (body == null)
68+
{
69+
throw new ArgumentNullException(nameof(body));
70+
}
71+
72+
if (body is V1Patch patch)
73+
{
74+
return GetHeader(patch);
75+
}
76+
77+
return MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
78+
}
79+
80+
private MediaTypeHeaderValue GetHeader(V1Patch body)
81+
{
82+
if (body == null)
83+
{
84+
throw new ArgumentNullException(nameof(body));
85+
}
86+
87+
switch (body.Type)
88+
{
89+
case V1Patch.PatchType.JsonPatch:
90+
return MediaTypeHeaderValue.Parse("application/json-patch+json; charset=utf-8");
91+
case V1Patch.PatchType.MergePatch:
92+
return MediaTypeHeaderValue.Parse("application/merge-patch+json; charset=utf-8");
93+
case V1Patch.PatchType.StrategicMergePatch:
94+
return MediaTypeHeaderValue.Parse("application/strategic-merge-patch+json; charset=utf-8");
95+
case V1Patch.PatchType.ApplyPatch:
96+
return MediaTypeHeaderValue.Parse("application/apply-patch+yaml; charset=utf-8");
97+
default:
98+
throw new ArgumentOutOfRangeException(nameof(body.Type), "");
99+
}
100+
}
101+
64102
protected abstract Task<HttpOperationResponse<T>> CreateResultAsync<T>(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken);
65103

66104
protected abstract HttpRequestMessage CreateRequest(string relativeUri, string method, IDictionary<string, IList<string>> customHeaders);
67105

68-
protected abstract MediaTypeHeaderValue GetHeader(object body);
69-
70106
protected abstract Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken);
71107
}
72108
}

src/KubernetesClient/Kubernetes.Header.cs

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

0 commit comments

Comments
 (0)