Skip to content

Commit

Permalink
Merge pull request #640 from daviian/wait-long-running-operation
Browse files Browse the repository at this point in the history
consider Http Status codes OK and Created when waiting for long running operations due to a change in the Api Management Rest API
  • Loading branch information
guythetechie authored Sep 6, 2024
2 parents b9c4361 + 4b42259 commit 7796fef
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tools/code/common/Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ public static Request CreateRequest(this HttpPipeline pipeline, Uri uri, Request
private static async ValueTask<Response> WaitForLongRunningOperation(this HttpPipeline pipeline, Response response, CancellationToken cancellationToken)
{
var updatedResponse = response;
while ((updatedResponse.Status == ((int)HttpStatusCode.Accepted))
while (((updatedResponse.Status is (int)HttpStatusCode.OK or (int)HttpStatusCode.Created && IsProvisioningInProgress(updatedResponse)) ||
updatedResponse.Status == (int)HttpStatusCode.Accepted)
&& updatedResponse.Headers.TryGetValue("Location", out var locationHeaderValue)
&& Uri.TryCreate(locationHeaderValue, UriKind.Absolute, out var locationUri)
&& locationUri is not null)
Expand All @@ -278,6 +279,23 @@ private static async ValueTask<Response> WaitForLongRunningOperation(this HttpPi

return updatedResponse;
}

private static bool IsProvisioningInProgress(Response response)
{
try
{
return response.Content.ToObjectFromJson<JsonObject>()
.TryGetJsonObjectProperty("properties")
.Bind(json => json.TryGetStringProperty("ProvisioningState"))
.ToOption()
.Where(state => state.Equals("InProgress", StringComparison.OrdinalIgnoreCase))
.IsSome;
}
catch (JsonException)
{
return false;
}
}
}

public sealed class ILoggerHttpPipelinePolicy(ILogger logger) : HttpPipelinePolicy
Expand Down

0 comments on commit 7796fef

Please sign in to comment.