add wrapper to get signed in pan.do/ra api

This commit is contained in:
j 2018-05-09 18:58:56 +01:00
parent bd64a2dba3
commit 7099ea7e13

View file

@ -137,3 +137,27 @@ class API(object):
for chunk in iter(lambda: u.read(chunk_size), b''): for chunk in iter(lambda: u.read(chunk_size), b''):
fd.write(chunk) fd.write(chunk)
shutil.move(tmpname, filename) 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