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

Commit

Permalink
Add support for getting tokens from files that OpenRobot-CLI supports
Browse files Browse the repository at this point in the history
  • Loading branch information
proguy914629bot committed Jan 5, 2022
1 parent ba306b8 commit e0a1f8e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion openrobot/api_wrapper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
3 changes: 3 additions & 0 deletions openrobot/api_wrapper/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions openrobot/api_wrapper/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions openrobot/api_wrapper/utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e0a1f8e

Please sign in to comment.