From e0a1f8e347731eefdaba03cb8d15d7c9db1f8dd4 Mon Sep 17 00:00:00 2001 From: proguy914629 Date: Wed, 5 Jan 2022 21:58:17 +0700 Subject: [PATCH] Add support for getting tokens from files that OpenRobot-CLI supports --- openrobot/api_wrapper/__init__.py | 3 ++- openrobot/api_wrapper/_async.py | 3 +++ openrobot/api_wrapper/_sync.py | 3 +++ openrobot/api_wrapper/utils.py | 20 ++++++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 openrobot/api_wrapper/utils.py diff --git a/openrobot/api_wrapper/__init__.py b/openrobot/api_wrapper/__init__.py index 0d56983..a46e593 100644 --- a/openrobot/api_wrapper/__init__.py +++ b/openrobot/api_wrapper/__init__.py @@ -4,7 +4,8 @@ from .results import * from .translate import * from .speech import * +from .utils import * -from . import _async, _sync, translate, results, error, speech +from . import _async, _sync, translate, results, error, speech, utils __version__ = '0.4.0' diff --git a/openrobot/api_wrapper/_async.py b/openrobot/api_wrapper/_async.py index 5c21e61..0cf3f28 100644 --- a/openrobot/api_wrapper/_async.py +++ b/openrobot/api_wrapper/_async.py @@ -8,6 +8,7 @@ from .results import * from .translate import Translate from .speech import Speech +from .utils import get_token_from_file try: from urllib.parse import quote_plus as quote @@ -53,6 +54,8 @@ class AsyncClient: """ def __init__(self, token: str = 'I-Am-Testing', *, session: aiohttp.ClientSession = None, loop: asyncio.AbstractEventLoop = None, ignore_warning: bool = False, handle_ratelimit: bool = True, tries: int = 5): + token = token or get_token_from_file() + if not token: raise NoTokenProvided() elif token == 'I-Am-Testing' and not ignore_warning: diff --git a/openrobot/api_wrapper/_sync.py b/openrobot/api_wrapper/_sync.py index e38e520..16dd9b5 100644 --- a/openrobot/api_wrapper/_sync.py +++ b/openrobot/api_wrapper/_sync.py @@ -8,6 +8,7 @@ from .results import * from .translate import Translate from .speech import Speech +from .utils import get_token_from_file try: from urllib.parse import quote_plus as quote @@ -45,6 +46,8 @@ class SyncClient: """ def __init__(self, token: str = 'I-Am-Testing', *, ignore_warning = False, handle_ratelimit: bool = True, tries: int = 5): + token = token or get_token_from_file() + if not token: raise NoTokenProvided() elif token == 'I-Am-Testing' and not ignore_warning: diff --git a/openrobot/api_wrapper/utils.py b/openrobot/api_wrapper/utils.py new file mode 100644 index 0000000..8a6fffa --- /dev/null +++ b/openrobot/api_wrapper/utils.py @@ -0,0 +1,20 @@ +import os +import json + +def get_token_from_file(): + # OpenRobot-CLI uses this method for storing the tokens. + # It gets it from ~/.openrobot/api/cridentials.json first. + # If the token key or the file/folder can't be found, it will try to get the token from the OPENROBOT_API_TOKEN env. + # If any of the above fails, it will return None. + + try: + dir = os.path.expanduser("~/.openrobot") + + with open(f'{dir}/api/cridentials.json', 'r') as f: + cridentials = json.load(f) + + token = cridentials['token'] + except: + token = os.environ.get('OPENROBOT_API_TOKEN') + + return token \ No newline at end of file