import json
import tornado.gen
import traceback

from base64 import b64encode
from tornado.httpclient import AsyncHTTPClient, HTTPRequest, HTTPError

from settings import Settings
from mongo_db_controller import ZoomUserDB

@tornado.gen.coroutine
def zoomRefresh(zoom_user):
    url = "https://zoom.us/oauth/token"
    payload = "grant_type=refresh_token&"
    payload += "refresh_token={0}".format(zoom_user.get('refresh_token'))
    #we need to base 64 encode it
    #and then decode it to acsii as python 3 stores it as a byte string
    userAndPass = b64encode("{0}:{1}".format(Settings.zoom_client_id, Settings.zoom_client_secret).encode()).decode("ascii")
    headers = {
        'authorization': 'Basic {0}'.format(userAndPass),
        'content-type': "application/x-www-form-urlencoded"
        }
    request = HTTPRequest(url, method="POST", headers=headers, body=payload)
    http_client = AsyncHTTPClient()
    print(zoom_user)
    print('making zoomRefresh')
    print(payload)
    try:
        response = yield http_client.fetch(request)
        resp = json.loads(response.body.decode("utf-8"))
        print("zoomRefresh /access_token Response: {0}".format(resp))
        zoom_user = ZoomUserDB.db.insert_user(zoom_user['person_id'], resp['access_token'], resp['expires_in'], resp['refresh_token'], "zoom")
        print('new zoom_user:{0}'.format(zoom_user))
    except HTTPError as he:
        print('zoomRefresh HTTPError:')
        print(he.code)
        print(he.response.body)
        if he.code == 401:
            ZoomUserDB.db.delete_user(zoom_user['person_id'], "zoom")
        zoom_user = None
    raise tornado.gen.Return(zoom_user)

@tornado.gen.coroutine
def zoomGET(endpoint_url, zoom_user):
    url = "https://api.zoom.us/v2{0}".format(endpoint_url)
    headers = {"Authorization":"Bearer {0}".format(zoom_user.get('token'))}
    request = HTTPRequest(url, method="GET", headers=headers)
    http_client = AsyncHTTPClient()
    response = None
    try:
        response = yield http_client.fetch(request)
        body = response.body.decode('utf-8')
        response = json.loads(body)
    except HTTPError as he:
        if he.code == 401:
            print('token may be expired, attempting refresh')
            zoom_user = yield zoomRefresh(zoom_user)
            if zoom_user:
                response, zoom_user = yield zoomGET(endpoint_url, zoom_user)
        else:
            try:
                print(he.response.body)
            except Exception as e:
                pass
            traceback.print_exc()
    raise tornado.gen.Return((response, zoom_user))

[((23, 14, 23, 76), 'tornado.httpclient.HTTPRequest', 'HTTPRequest', (), '', False, 'from tornado.httpclient import AsyncHTTPClient, HTTPRequest, HTTPError\n'), ((24, 18, 24, 35), 'tornado.httpclient.AsyncHTTPClient', 'AsyncHTTPClient', ({}, {}), '()', False, 'from tornado.httpclient import AsyncHTTPClient, HTTPRequest, HTTPError\n'), ((47, 14, 47, 61), 'tornado.httpclient.HTTPRequest', 'HTTPRequest', (), '', False, 'from tornado.httpclient import AsyncHTTPClient, HTTPRequest, HTTPError\n'), ((48, 18, 48, 35), 'tornado.httpclient.AsyncHTTPClient', 'AsyncHTTPClient', ({}, {}), '()', False, 'from tornado.httpclient import AsyncHTTPClient, HTTPRequest, HTTPError\n'), ((32, 20, 32, 142), 'mongo_db_controller.ZoomUserDB.db.insert_user', 'ZoomUserDB.db.insert_user', ({(32, 46, 32, 68): "zoom_user['person_id']", (32, 70, 32, 90): "resp['access_token']", (32, 92, 32, 110): "resp['expires_in']", (32, 112, 32, 133): "resp['refresh_token']", (32, 135, 32, 141): '"""zoom"""'}, {}), "(zoom_user['person_id'], resp['access_token'],\n    resp['expires_in'], resp['refresh_token'], 'zoom')", False, 'from mongo_db_controller import ZoomUserDB\n'), ((53, 19, 53, 35), 'json.loads', 'json.loads', ({(53, 30, 53, 34): 'body'}, {}), '(body)', False, 'import json\n'), ((39, 12, 39, 69), 'mongo_db_controller.ZoomUserDB.db.delete_user', 'ZoomUserDB.db.delete_user', ({(39, 38, 39, 60): "zoom_user['person_id']", (39, 62, 39, 68): '"""zoom"""'}, {}), "(zoom_user['person_id'], 'zoom')", False, 'from mongo_db_controller import ZoomUserDB\n'), ((65, 12, 65, 33), 'traceback.print_exc', 'traceback.print_exc', ({}, {}), '()', False, 'import traceback\n')]