Skip to content

Commit 7796fef

Browse files
authored
Merge pull request #640 from daviian/wait-long-running-operation
consider Http Status codes OK and Created when waiting for long running operations due to a change in the Api Management Rest API
2 parents b9c4361 + 4b42259 commit 7796fef

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

tools/code/common/Http.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ public static Request CreateRequest(this HttpPipeline pipeline, Uri uri, Request
253253
private static async ValueTask<Response> WaitForLongRunningOperation(this HttpPipeline pipeline, Response response, CancellationToken cancellationToken)
254254
{
255255
var updatedResponse = response;
256-
while ((updatedResponse.Status == ((int)HttpStatusCode.Accepted))
256+
while (((updatedResponse.Status is (int)HttpStatusCode.OK or (int)HttpStatusCode.Created && IsProvisioningInProgress(updatedResponse)) ||
257+
updatedResponse.Status == (int)HttpStatusCode.Accepted)
257258
&& updatedResponse.Headers.TryGetValue("Location", out var locationHeaderValue)
258259
&& Uri.TryCreate(locationHeaderValue, UriKind.Absolute, out var locationUri)
259260
&& locationUri is not null)
@@ -278,6 +279,23 @@ private static async ValueTask<Response> WaitForLongRunningOperation(this HttpPi
278279

279280
return updatedResponse;
280281
}
282+
283+
private static bool IsProvisioningInProgress(Response response)
284+
{
285+
try
286+
{
287+
return response.Content.ToObjectFromJson<JsonObject>()
288+
.TryGetJsonObjectProperty("properties")
289+
.Bind(json => json.TryGetStringProperty("ProvisioningState"))
290+
.ToOption()
291+
.Where(state => state.Equals("InProgress", StringComparison.OrdinalIgnoreCase))
292+
.IsSome;
293+
}
294+
catch (JsonException)
295+
{
296+
return false;
297+
}
298+
}
281299
}
282300

283301
public sealed class ILoggerHttpPipelinePolicy(ILogger logger) : HttpPipelinePolicy

0 commit comments

Comments
 (0)