move getheader to abstract (#807)

This commit is contained in:
Boshi Lian
2022-03-28 08:45:23 -07:00
committed by GitHub
parent 8e8619130a
commit dba7b9718e
2 changed files with 38 additions and 48 deletions

View File

@@ -2,6 +2,7 @@ using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using k8s.Autorest;
using k8s.Models;
using System.Net.Http.Headers;
@@ -61,12 +62,47 @@ namespace k8s
public virtual TimeSpan HttpClientTimeout { get; set; } = TimeSpan.FromSeconds(100);
protected virtual MediaTypeHeaderValue GetHeader(object body)
{
if (body == null)
{
throw new ArgumentNullException(nameof(body));
}
if (body is V1Patch patch)
{
return GetHeader(patch);
}
return MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
}
private MediaTypeHeaderValue GetHeader(V1Patch body)
{
if (body == null)
{
throw new ArgumentNullException(nameof(body));
}
switch (body.Type)
{
case V1Patch.PatchType.JsonPatch:
return MediaTypeHeaderValue.Parse("application/json-patch+json; charset=utf-8");
case V1Patch.PatchType.MergePatch:
return MediaTypeHeaderValue.Parse("application/merge-patch+json; charset=utf-8");
case V1Patch.PatchType.StrategicMergePatch:
return MediaTypeHeaderValue.Parse("application/strategic-merge-patch+json; charset=utf-8");
case V1Patch.PatchType.ApplyPatch:
return MediaTypeHeaderValue.Parse("application/apply-patch+yaml; charset=utf-8");
default:
throw new ArgumentOutOfRangeException(nameof(body.Type), "");
}
}
protected abstract Task<HttpOperationResponse<T>> CreateResultAsync<T>(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken);
protected abstract HttpRequestMessage CreateRequest(string relativeUri, string method, IDictionary<string, IList<string>> customHeaders);
protected abstract MediaTypeHeaderValue GetHeader(object body);
protected abstract Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken);
}
}

View File

@@ -1,46 +0,0 @@
using System.Net.Http.Headers;
using k8s.Models;
namespace k8s
{
public partial class Kubernetes
{
protected override MediaTypeHeaderValue GetHeader(object body)
{
if (body == null)
{
throw new ArgumentNullException(nameof(body));
}
if (body is V1Patch patch)
{
return GetHeader(patch);
}
return MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
}
private MediaTypeHeaderValue GetHeader(V1Patch body)
{
if (body == null)
{
throw new ArgumentNullException(nameof(body));
}
switch (body.Type)
{
case V1Patch.PatchType.JsonPatch:
return MediaTypeHeaderValue.Parse("application/json-patch+json; charset=utf-8");
case V1Patch.PatchType.MergePatch:
return MediaTypeHeaderValue.Parse("application/merge-patch+json; charset=utf-8");
case V1Patch.PatchType.StrategicMergePatch:
return MediaTypeHeaderValue.Parse("application/strategic-merge-patch+json; charset=utf-8");
case V1Patch.PatchType.ApplyPatch:
return MediaTypeHeaderValue.Parse("application/apply-patch+yaml; charset=utf-8");
default:
throw new ArgumentOutOfRangeException(nameof(body.Type), "");
}
}
}
}