Ejemplo n.º 1
0
def _get_smart():
    if not USE_SMART:
        return None

    sess = _get_session()
    if sess is None:
        logging.info("There is no session")
        return None

    # configure SMART client
    api_base = sess.get('api_base')
    if not api_base:
        logging.info("No api_base is set")
        return None

    # we're not using SMART
    if 'none' == api_base:
        return None

    # find server credentials and store in session
    cons_key = sess.get('consumer_key')
    cons_sec = sess.get('consumer_secret')
    if not cons_key or not cons_sec:
        server = None
        for ep in ENDPOINTS:
            if ep.get('url') == api_base:
                server = ep
                break

        if server is None:
            logging.error("There is no server with base URI %s" % api_base)
            return None

        sess['consumer_key'] = cons_key = server.get('consumer_key')
        sess['consumer_secret'] = cons_sec = server.get('consumer_secret')

    # init client
    config = {'consumer_key': cons_key, 'consumer_secret': cons_sec}

    try:
        smart = SMARTClient(os.environ.get('USE_APP_ID'), api_base, config)
        smart.record_id = sess.get('record_id')
    except Exception as e:
        logging.warning("Failed to instantiate SMART client: %s" % e)
        smart = None

    # if we have tokens, update the client
    token = sess.get('token')
    if token is not None:
        smart.update_token(token)

    return smart