diff --git a/ox/api.py b/ox/api.py index 6f9aa67..4b56fa6 100644 --- a/ox/api.py +++ b/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 +