|
2 | 2 | using System.Threading;
|
3 | 3 | using System.Threading.Tasks;
|
4 | 4 | using k8s.Autorest;
|
| 5 | +using k8s.Models; |
5 | 6 | using System.Net.Http.Headers;
|
6 | 7 |
|
7 | 8 |
|
@@ -61,12 +62,47 @@ private Task<HttpResponseMessage> SendRequest<T>(T body, HttpRequestMessage http
|
61 | 62 |
|
62 | 63 | public virtual TimeSpan HttpClientTimeout { get; set; } = TimeSpan.FromSeconds(100);
|
63 | 64 |
|
| 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 | + |
64 | 102 | protected abstract Task<HttpOperationResponse<T>> CreateResultAsync<T>(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken);
|
65 | 103 |
|
66 | 104 | protected abstract HttpRequestMessage CreateRequest(string relativeUri, string method, IDictionary<string, IList<string>> customHeaders);
|
67 | 105 |
|
68 |
| - protected abstract MediaTypeHeaderValue GetHeader(object body); |
69 |
| - |
70 | 106 | protected abstract Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken);
|
71 | 107 | }
|
72 | 108 | }
|
0 commit comments