Skip to content

Commit

Permalink
tested
Browse files Browse the repository at this point in the history
  • Loading branch information
martinxu9 committed Mar 19, 2024
1 parent ad181a9 commit 2ebbcac
Showing 1 changed file with 66 additions and 11 deletions.
77 changes: 66 additions & 11 deletions reflex/custom_components/custom_components.py
Expand Up @@ -26,6 +26,12 @@
f"{config.cp_backend_url}/custom-components/gallery"
)

GET_CUSTOM_COMPONENTS_GALLERY_BY_NAME_ENDPOINT = (
f"{config.cp_backend_url}/custom-components/gallery"
)

POST_CUSTOM_COMPONENTS_GALLERY_TIMEOUT = 15


@contextmanager
def set_directory(working_directory: str):
Expand Down Expand Up @@ -726,6 +732,32 @@ def _collect_details_for_gallery():
)
raise typer.Exit(code=1)
console.print(f"Custom component package name: {package_name}")
# Check the backend services if the user is allowed to update information of this package is already shared.
expected_status_code = False
try:
console.debug(
f"Checking if user has permission to modify information for {package_name} if already exists."
)
response = httpx.get(
f"{GET_CUSTOM_COMPONENTS_GALLERY_BY_NAME_ENDPOINT}/{package_name}",
headers={"Authorization": f"Bearer {access_token}"},
)
if response.status_code == httpx.codes.FORBIDDEN:
console.error(
f"{package_name} is owned by another user. Unable to update the information for it."
)
raise typer.Exit(code=1)
elif response.status_code == httpx.codes.NOT_FOUND:
console.debug(f"{package_name} is not found. This is a new share.")
expected_status_code = True

response.raise_for_status()
console.debug(f"{package_name} is found. This is an update.")
except httpx.HTTPError as he:
if not expected_status_code:
console.error(f"Unable to complete request due to {he}.")
raise typer.Exit(code=1) from he

params["package_name"] = package_name

description = None
Expand All @@ -750,21 +782,29 @@ def _collect_details_for_gallery():
if (image_file := _get_file_from_prompt_in_loop("image")) is not None:
files.append(("files", ("image", image_file)))

source = (
console.ask(
"Full URL of the source code, e.g. `https://github.com/my-repo` (enter to skip)"
source = None
while True:
source = (
console.ask(
"Full URL of the source code, e.g. `https://github.com/my-repo` (enter to skip)"
)
or None
)
or None
)
if _validate_url_with_protocol_prefix(source):
break
if source:
params["source"] = source

demo_url = (
console.ask(
"Full URL of deployed demo app, e.g. `https://my-app.reflex.run` (enter to skip)"
demo_url = None
while True:
demo_url = (
console.ask(
"Full URL of deployed demo app, e.g. `https://my-app.reflex.run` (enter to skip)"
)
or None
)
or None
)
if _validate_url_with_protocol_prefix(demo_url):
break
if demo_url:
params["demo_url"] = demo_url

Expand All @@ -776,13 +816,28 @@ def _collect_details_for_gallery():
headers={"Authorization": f"Bearer {access_token}"},
data=params,
files=files,
timeout=POST_CUSTOM_COMPONENTS_GALLERY_TIMEOUT,
)
response.raise_for_status()

except httpx.HTTPError as he:
console.error(f"Unable to complete request due to {he}.")
raise typer.Exit(code=1) from he

console.print("Custom component information successfully shared!")


def _validate_url_with_protocol_prefix(url: str | None) -> bool:
"""Validate the URL with protocol prefix. Empty string is acceptable.
Args:
url: the URL string to check.
Returns:
Whether the entered URL is acceptable.
"""
return not url or (url.startswith("http://") or url.startswith("https://"))


def _get_file_from_prompt_in_loop(file_type: Literal["image", "gif"]):
image_file = None
Expand All @@ -793,7 +848,7 @@ def _get_file_from_prompt_in_loop(file_type: Literal["image", "gif"]):
if not image_filepath:
break
try:
with open(image_filepath, "r") as f:
with open(image_filepath, "rb") as f:
image_file = f.read()
except OSError as ose:
console.error(f"Unable to read the {file_type} file due to {ose}")
Expand Down

0 comments on commit 2ebbcac

Please sign in to comment.