Skip to content

Commit cf00214

Browse files
committed
Simplify ReplaceAsync
1 parent 6e265bc commit cf00214

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/KubernetesClient/Fluent/KubernetesRequest.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -402,25 +402,22 @@ public async Task<T> ReplaceAsync<T>(
402402
{
403403
if (modify == null) throw new ArgumentNullException(nameof(modify));
404404
if (_watchVersion != null) throw new InvalidOperationException("Watches cannot be updated.");
405-
KubernetesRequest putReq = null;
406-
while (true)
405+
KubernetesRequest req = Clone();
406+
while(true)
407407
{
408-
if (obj == null) // if we need to load the resource...
408+
if(obj == null) // if we need to load the resource...
409409
{
410-
cancelToken.ThrowIfCancellationRequested();
411-
HttpRequestMessage getMsg = await CreateRequestMessage(cancelToken).ConfigureAwait(false); // load it with a GET request
412-
getMsg.Method = HttpMethod.Get;
413-
obj = await ExecuteMessageAsync<T>(getMsg, failIfMissing, cancelToken).ConfigureAwait(false);
410+
cancelToken.ThrowIfCancellationRequested(); // load it with a GET request
411+
obj = await req.Get().Body(null).ExecuteAsync<T>(failIfMissing, cancelToken).ConfigureAwait(false);
414412
}
415413
cancelToken.ThrowIfCancellationRequested();
416-
// if the resource is missing or no changes are needed, return it as-is
417-
if (obj == null || !await modify(obj, cancelToken).ConfigureAwait(false)) return obj;
418-
if (putReq == null) putReq = Clone().Put();
419-
KubernetesResponse resp = await putReq.Body(obj).ExecuteAsync(cancelToken).ConfigureAwait(false); // otherwise, update it
420-
if (resp.StatusCode != HttpStatusCode.Conflict) // if there was no conflict, return the result
414+
// if the resource is missing or no changes are needed, return it as-is. otherwise, update it with a PUT request
415+
if(obj == null || !await modify(obj, cancelToken).ConfigureAwait(false)) return obj;
416+
KubernetesResponse resp = await req.Put().Body(obj).ExecuteAsync(cancelToken).ConfigureAwait(false);
417+
if(resp.StatusCode != HttpStatusCode.Conflict) // if there was no conflict, return the result
421418
{
422-
if (resp.IsNotFound && !failIfMissing) return null;
423-
else if (resp.IsError) throw new KubernetesException(await resp.GetStatusAsync().ConfigureAwait(false));
419+
if(resp.IsNotFound && !failIfMissing) return null;
420+
else if(resp.IsError) throw new KubernetesException(await resp.GetStatusAsync().ConfigureAwait(false));
424421
else return await resp.GetBodyAsync<T>().ConfigureAwait(false);
425422
}
426423
obj = null; // otherwise, there was a conflict, so reload the item

0 commit comments

Comments
 (0)