Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit e0a1f8e

Browse files
Add support for getting tokens from files that OpenRobot-CLI supports
1 parent ba306b8 commit e0a1f8e

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

openrobot/api_wrapper/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from .results import *
55
from .translate import *
66
from .speech import *
7+
from .utils import *
78

8-
from . import _async, _sync, translate, results, error, speech
9+
from . import _async, _sync, translate, results, error, speech, utils
910

1011
__version__ = '0.4.0'

openrobot/api_wrapper/_async.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .results import *
99
from .translate import Translate
1010
from .speech import Speech
11+
from .utils import get_token_from_file
1112

1213
try:
1314
from urllib.parse import quote_plus as quote
@@ -53,6 +54,8 @@ class AsyncClient:
5354
"""
5455

5556
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):
57+
token = token or get_token_from_file()
58+
5659
if not token:
5760
raise NoTokenProvided()
5861
elif token == 'I-Am-Testing' and not ignore_warning:

openrobot/api_wrapper/_sync.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .results import *
99
from .translate import Translate
1010
from .speech import Speech
11+
from .utils import get_token_from_file
1112

1213
try:
1314
from urllib.parse import quote_plus as quote
@@ -45,6 +46,8 @@ class SyncClient:
4546
"""
4647

4748
def __init__(self, token: str = 'I-Am-Testing', *, ignore_warning = False, handle_ratelimit: bool = True, tries: int = 5):
49+
token = token or get_token_from_file()
50+
4851
if not token:
4952
raise NoTokenProvided()
5053
elif token == 'I-Am-Testing' and not ignore_warning:

openrobot/api_wrapper/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
import json
3+
4+
def get_token_from_file():
5+
# OpenRobot-CLI uses this method for storing the tokens.
6+
# It gets it from ~/.openrobot/api/cridentials.json first.
7+
# 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.
8+
# If any of the above fails, it will return None.
9+
10+
try:
11+
dir = os.path.expanduser("~/.openrobot")
12+
13+
with open(f'{dir}/api/cridentials.json', 'r') as f:
14+
cridentials = json.load(f)
15+
16+
token = cridentials['token']
17+
except:
18+
token = os.environ.get('OPENROBOT_API_TOKEN')
19+
20+
return token

0 commit comments

Comments
 (0)