add wrapper to get signed in pan.do/ra api
This commit is contained in:
parent
bd64a2dba3
commit
7099ea7e13
1 changed files with 24 additions and 0 deletions
24
ox/api.py
24
ox/api.py
|
@ -137,3 +137,27 @@ class API(object):
|
|||
for chunk in iter(lambda: u.read(chunk_size), b''):
|
||||
fd.write(chunk)
|
||||
shutil.move(tmpname, filename)
|
||||
|
||||
|
||||
def signin(site):
|
||||
from getpass import getpass
|
||||
from .web import auth
|
||||
api = API('https://%s/api/' % site)
|
||||
update = False
|
||||
try:
|
||||
credentials = auth.get(site)
|
||||
except:
|
||||
credentials = {}
|
||||
print('Please provide your username and password for %s:' % site)
|
||||
credentials['username'] = input('Username: ')
|
||||
credentials['password'] = getpass('Password: ')
|
||||
update = True
|
||||
r = api.signin(**credentials)
|
||||
if 'errors' in r.get('data', {}):
|
||||
for kv in r['data']['errors'].items():
|
||||
print('%s: %s' % kv)
|
||||
sys.exit(1)
|
||||
if update:
|
||||
auth.update(site, credentials)
|
||||
return api
|
||||
|
||||
|
|
Loading…
Reference in a new issue