-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[REF-3197] Browser init workflow (#3673)
- Loading branch information
Showing
5 changed files
with
127 additions
and
5 deletions.
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
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
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,48 @@ | ||
"""Utilities to handle redirection to browser UI.""" | ||
|
||
import time | ||
import uuid | ||
import webbrowser | ||
|
||
import httpx | ||
|
||
from .. import constants | ||
from . import console | ||
|
||
|
||
def open_browser_and_wait( | ||
target_url: str, poll_url: str, interval: int = 1 | ||
) -> httpx.Response: | ||
"""Open a browser window to target_url and request poll_url until it returns successfully. | ||
Args: | ||
target_url: The URL to open in the browser. | ||
poll_url: The URL to poll for success. | ||
interval: The interval in seconds to wait between polling. | ||
Returns: | ||
The response from the poll_url. | ||
""" | ||
if not webbrowser.open(target_url): | ||
console.warn( | ||
f"Unable to automatically open the browser. Please navigate to {target_url} in your browser." | ||
) | ||
console.info("Complete the workflow in the browser to continue.") | ||
while response := httpx.get(poll_url, follow_redirects=True): | ||
if response.is_success: | ||
break | ||
time.sleep(interval) | ||
return response | ||
|
||
|
||
def reflex_build_redirect() -> str: | ||
"""Open the browser window to reflex.build and wait for the user to select a generation. | ||
Returns: | ||
The selected generation hash. | ||
""" | ||
token = str(uuid.uuid4()) | ||
target_url = constants.Templates.REFLEX_BUILD_URL.format(reflex_init_token=token) | ||
poll_url = constants.Templates.REFLEX_BUILD_POLL_URL.format(reflex_init_token=token) | ||
response = open_browser_and_wait(target_url, poll_url) | ||
return response.json()["generation_hash"] |