From c282cd5911aef8de001193511094df564a3d6bff Mon Sep 17 00:00:00 2001 From: guptadev21 Date: Fri, 18 Oct 2024 15:38:07 +0530 Subject: [PATCH] chore: update --- riocli/device/report.py | 53 +++++++++++++++++++++++++++++++++++++++-- riocli/device/util.py | 7 +++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/riocli/device/report.py b/riocli/device/report.py index af2cda1d..0d2eb2e9 100644 --- a/riocli/device/report.py +++ b/riocli/device/report.py @@ -12,9 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. +from datetime import datetime, timedelta + import click from click_help_colors import HelpColorsCommand +from rapyuta_io.clients import SharedURL +from riocli.config import new_client from riocli.constants import Colors, Symbols from riocli.device.util import name_to_guid, report_device_api_call from riocli.utils.spinner import with_spinner @@ -29,11 +33,31 @@ @click.option( "--force", "-f", "--silent", "force", is_flag=True, help="Skip confirmation" ) +@click.option( + "--share", + "-s", + is_flag=True, + help="Share the device logs with Rapyuta support", + default=False, +) +@click.option( + "--expiry", + "-e", + help="Expiry time for the shared URL in days", + default=7, + required=False, + show_default=True, +) @click.argument("device-name", type=str) @name_to_guid @with_spinner(text="Upload debugging files...") def report_device( - device_name: str, device_guid: str, force: bool, spinner=None + device_name: str, + device_guid: str, + force: bool, + share: bool, + expiry: int, + spinner=None, ) -> None: """ Report a device and get its debug logs in uploads section. @@ -45,7 +69,16 @@ def report_device( abort=True, ) try: - report_device_api_call(device_guid=device_guid) + # Upload the debug logs + response = report_device_api_call(device_guid=device_guid) + + if share: + spinner.text = click.style("Creating shared URL...", fg=Colors.YELLOW) + public_url = shared_url_debug_logs( + device_guid, response["response"]["data"]["request_uuid"], expiry + ) + click.secho(public_url.url, fg=Colors.GREEN) + spinner.text = click.style("Device reported successfully.", fg=Colors.GREEN) spinner.green.ok(Symbols.SUCCESS) @@ -54,3 +87,19 @@ def report_device( "Failed to report device: {}".format(e), fg=Colors.RED ) spinner.red.fail(Symbols.ERROR) + + +@with_spinner(text="Creating shared URL...") +def shared_url_debug_logs(device_guid: str, request_id: str, expiry: int, spinner=None): + try: + client = new_client() + device = client.get_device(device_id=device_guid) + expiry_time = datetime.now() + timedelta(days=expiry) + + # Create the shared URL + public_url = device.create_shared_url( + SharedURL(request_id, expiry_time=expiry_time) + ) + return public_url + except Exception as e: + raise Exception(f"Failed to create shared URL: {e}") diff --git a/riocli/device/util.py b/riocli/device/util.py index 39e5b7e9..f5d7917a 100644 --- a/riocli/device/util.py +++ b/riocli/device/util.py @@ -90,7 +90,12 @@ def report_device_api_call(device_guid: str) -> dict: RestClient(url).method(HttpMethod.POST).headers(headers).execute(payload={}) ) - return handle_server_errors(response) + result = handle_server_errors(response) + + if result: + return result + + return response.json() def get_device_name(client: Client, guid: str) -> str: