-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HttpLongRunningOperationResponse #46
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
import { HttpOperationResponse, RequestOptionsBase } from "ms-rest-js"; | ||
import { AzureServiceClient } from "./azureServiceClient"; | ||
import { createLROPollStrategyFromInitialResponse, createLROPollStrategyFromMemento, LROMemento, LROPollStrategy } from "./lroPollStrategy"; | ||
import { LongRunningOperationStates } from "./util/constants"; | ||
|
||
/** | ||
* An HTTP operation response that provides special methods for interacting with LROs (long running | ||
* operations). | ||
*/ | ||
export class HttpLongRunningOperationResponse { | ||
/** | ||
* Create a new HttpLongRunningOperationResponse. | ||
* @param _lroPollStrategy The LROPollStrategy that this HttpLongRunningOperationResponse will | ||
* use to interact with the LRO. | ||
*/ | ||
constructor(private readonly _lroPollStrategy: LROPollStrategy | undefined, private readonly _initialResponse: HttpOperationResponse) { | ||
} | ||
|
||
/** | ||
* Get the current status of the LRO. | ||
* @returns The current status of the LRO. | ||
*/ | ||
public getOperationStatus(): LongRunningOperationStates { | ||
const lroPollStrategy: LROPollStrategy | undefined = this._lroPollStrategy; | ||
return !lroPollStrategy ? "Succeeded" : lroPollStrategy.getOperationStatus(); | ||
} | ||
|
||
/** | ||
* Send a single poll request and return the LRO's state. | ||
* @returns The LRO's state. | ||
*/ | ||
public poll(): Promise<LongRunningOperationStates> { | ||
let result: Promise<LongRunningOperationStates>; | ||
const lroPollStrategy: LROPollStrategy | undefined = this._lroPollStrategy; | ||
if (!lroPollStrategy) { | ||
result = Promise.resolve<LongRunningOperationStates>("Succeeded"); | ||
} else { | ||
result = lroPollStrategy.sendPollRequest().then(() => { | ||
return lroPollStrategy.getOperationStatus(); | ||
}); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Send poll requests that check the LRO's status until it is determined that the LRO is finished. | ||
* @returns Whether or not the LRO succeeded. | ||
*/ | ||
public async pollUntilFinished(): Promise<HttpOperationResponse> { | ||
let result: Promise<HttpOperationResponse>; | ||
const lroPollStrategy: LROPollStrategy | undefined = this._lroPollStrategy; | ||
if (!lroPollStrategy) { | ||
result = Promise.resolve(this._initialResponse); | ||
} else { | ||
result = lroPollStrategy.pollUntilFinished().then((succeeded: boolean) => { | ||
if (succeeded) { | ||
return lroPollStrategy.getOperationResponse(); | ||
} else { | ||
throw lroPollStrategy.getRestError(); | ||
} | ||
}); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Get an LROMemento object that can be used to poll this LRO in a different context (such as on a | ||
* different process or a different machine). If the LRO couldn't produce an LRO polling strategy, | ||
* then this will return undefined. | ||
*/ | ||
public getMemento(): LROMemento | undefined { | ||
const lroPollStrategy: LROPollStrategy | undefined = this._lroPollStrategy; | ||
return !lroPollStrategy ? undefined : lroPollStrategy.getMemento(); | ||
} | ||
} | ||
|
||
export function createHttpLongRunningOperationResponseFromInitialResponse(azureServiceClient: AzureServiceClient, initialResponse: HttpOperationResponse, options?: RequestOptionsBase): HttpLongRunningOperationResponse { | ||
const lroPollStrategy: LROPollStrategy | undefined = createLROPollStrategyFromInitialResponse(initialResponse, azureServiceClient, options); | ||
return new HttpLongRunningOperationResponse(lroPollStrategy, initialResponse); | ||
} | ||
|
||
export function createHttpLongRunningOperationResponseFromMemento(azureServiceClient: AzureServiceClient, lroMemento: LROMemento): HttpLongRunningOperationResponse { | ||
const lroPollStrategy: LROPollStrategy | undefined = createLROPollStrategyFromMemento(azureServiceClient, lroMemento); | ||
return new HttpLongRunningOperationResponse(lroPollStrategy, lroMemento.initialResponse); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What makes
export
necessary for these 2 functions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two functions serve as the constructor functions for HttpLongRunningOperationResponse objects. They're both used by the AzureServiceClient class.