From 1b88548928bb85810940f3ce80279aafbe445f2a Mon Sep 17 00:00:00 2001 From: aytekin Date: Mon, 3 Jun 2024 15:27:09 -0500 Subject: [PATCH 1/2] feat(fix): Refactor `get_connections` function to improve type hinting and parameter handling --- smartcar/smartcar.py | 48 +++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/smartcar/smartcar.py b/smartcar/smartcar.py index c8582a89..4daf03f7 100644 --- a/smartcar/smartcar.py +++ b/smartcar/smartcar.py @@ -3,7 +3,7 @@ import hashlib import os import re -from typing import List, Union +from typing import NamedTuple, List, Dict, Optional, Union from warnings import warn import smartcar.config as config @@ -265,40 +265,38 @@ def get_management_token(amt: str, username: str = "default") -> str: def get_connections( - amt: str, filter: dict = {}, paging: dict = {} + amt: str, filter: Optional[Dict[str, str]] = None, paging: Optional[Dict[str, Optional[int]]] = None ) -> types.GetConnections: """ Returns a paged list of all the vehicles that are connected to the application - associated with the management API token used sorted in descending order by connection date. + associated with the management API token used, sorted in descending order by connection date. Args: amt (str): Application Management Token from Smartcar Dashboard - - filter (dict, optional) - vehicle_id (str, optional) - user_id (str, optional) - - paging (dict, optional) - limit (int, optional) - cursor_id (str, optional) + filter (dict, optional): + vehicle_id (str, optional): If provided, filters connections to a specific vehicle. + user_id (str, optional): If provided, filters connections to a specific user. + paging (dict, optional): + limit (int, optional): The maximum number of connections to return. + cursor (str, optional): The cursor ID for pagination to retrieve the next set of results. Returns: - GetConnections = NamedTuple("GetConnections", [ - ("connections", List[Connection]), - ("paging", PagingCursor), - ("meta", namedtuple) - ], - ) + GetConnections: A named tuple containing connections, paging information, and meta data. """ + if filter is None: + filter = {} + if paging is None: + paging = {} + params = {} - if filter.get("user_id"): - params["user_id"] = filter.get("user_id") - if filter.get("vehicle_id"): - params["vehicle_id"] = filter.get("vehicle_id") - if paging.get("cursor"): - params["cursor"] = filter.get("cursor") - if paging.get("limit"): - params["limit"] = filter.get("limit") + if "user_id" in filter: + params["user_id"] = filter["user_id"] + if "vehicle_id" in filter: + params["vehicle_id"] = filter["vehicle_id"] + if "cursor" in paging: + params["cursor"] = paging["cursor"] + if "limit" in paging: + params["limit"] = paging["limit"] url = f"{config.MANAGEMENT_API_URL}/v{get_api_version()}/management/connections/" headers = {"Authorization": f"Basic {get_management_token(amt)}"} From 14bf1cb723e785bb3290398370ac26aad3761c24 Mon Sep 17 00:00:00 2001 From: aytekin Date: Mon, 3 Jun 2024 16:12:46 -0500 Subject: [PATCH 2/2] feat: file formatted --- smartcar/smartcar.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/smartcar/smartcar.py b/smartcar/smartcar.py index 4daf03f7..d3a74506 100644 --- a/smartcar/smartcar.py +++ b/smartcar/smartcar.py @@ -265,7 +265,9 @@ def get_management_token(amt: str, username: str = "default") -> str: def get_connections( - amt: str, filter: Optional[Dict[str, str]] = None, paging: Optional[Dict[str, Optional[int]]] = None + amt: str, + filter: Optional[Dict[str, str]] = None, + paging: Optional[Dict[str, Optional[int]]] = None, ) -> types.GetConnections: """ Returns a paged list of all the vehicles that are connected to the application @@ -273,10 +275,10 @@ def get_connections( Args: amt (str): Application Management Token from Smartcar Dashboard - filter (dict, optional): + filter (dict, optional): vehicle_id (str, optional): If provided, filters connections to a specific vehicle. user_id (str, optional): If provided, filters connections to a specific user. - paging (dict, optional): + paging (dict, optional): limit (int, optional): The maximum number of connections to return. cursor (str, optional): The cursor ID for pagination to retrieve the next set of results.