From fae3edcc3355ea293f064051d8867431eb2e3e34 Mon Sep 17 00:00:00 2001 From: guptadev21 Date: Fri, 18 Oct 2024 16:15:17 +0530 Subject: [PATCH] chore: refactor --- riocli/device/report.py | 49 ++++++++++++++++++++--------------------- riocli/device/util.py | 23 +++++++++++++++---- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/riocli/device/report.py b/riocli/device/report.py index 0d2eb2e9..d6c73a1f 100644 --- a/riocli/device/report.py +++ b/riocli/device/report.py @@ -11,16 +11,11 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 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.device.util import name_to_guid, shared_url_debug_logs, upload_debug_logs from riocli.utils.spinner import with_spinner @@ -37,7 +32,7 @@ "--share", "-s", is_flag=True, - help="Share the device logs with Rapyuta support", + help="Generate a public URL for sharing", default=False, ) @click.option( @@ -60,7 +55,27 @@ def report_device( spinner=None, ) -> None: """ - Report a device and get its debug logs in uploads section. + This function facilitates the reporting of a device by uploading its debug logs. + It also provides an option to share the logs with Rapyuta support by generating + a public URL with a specified expiry time. + + Usage Examples: + + Report a device with confirmation prompt. + + $ rio device report DEVICE_NAME + + Report a device without confirmation prompt. + + $ rio device report -f DEVICE_NAME + + Report a device and generate a public URL for sharing. + + $ rio device report -s DEVICE_NAME + + Report a device with a custom expiry time for the shared URL. + + $ rio device report -s -e 10 DEVICE_NAME """ if not force: with spinner.hidden(): @@ -70,7 +85,7 @@ def report_device( ) try: # Upload the debug logs - response = report_device_api_call(device_guid=device_guid) + response = upload_debug_logs(device_guid=device_guid) if share: spinner.text = click.style("Creating shared URL...", fg=Colors.YELLOW) @@ -87,19 +102,3 @@ 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 f5d7917a..5cd6f280 100644 --- a/riocli/device/util.py +++ b/riocli/device/util.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import datetime import functools import json import re @@ -21,9 +22,8 @@ import click from munch import Munch from rapyuta_io import Client -from rapyuta_io.clients import LogUploads -from rapyuta_io.clients.device import Device -from rapyuta_io.clients.device import DeviceStatus +from rapyuta_io.clients import LogUploads, SharedURL +from rapyuta_io.clients.device import Device, DeviceStatus from rapyuta_io.utils import RestClient from rapyuta_io.utils.rest_client import HttpMethod @@ -75,7 +75,7 @@ def decorated(**kwargs: typing.Any): return decorated -def report_device_api_call(device_guid: str) -> dict: +def upload_debug_logs(device_guid: str) -> dict: config = Configuration() coreapi_host = config.data.get( "core_api_host", "https://gaapiserver.apps.okd4v2.prod.rapyuta.io" @@ -98,6 +98,21 @@ def report_device_api_call(device_guid: str) -> dict: return response.json() +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() + datetime.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}") + + def get_device_name(client: Client, guid: str) -> str: device = client.get_device(device_id=guid) return device.name