def _request_token_if_needed(request, record_id, api_base): """ Requests a request token for record id, if needed. """ ts = TokenStore() cookie = request.get_cookie(_cookie_name) token, ex_api_base, ex_record_id = ts.tokenServerRecordForCookie(cookie) # we already got a token, test if it still works if token is not None \ and (record_id is None or int(ex_record_id) == int(record_id)) \ and (api_base is None or unicode(ex_api_base) == unicode(api_base)) \ and _test_record_token(api_base, record_id, token): _log_debug("reusing existing token") return False, None # request a fresh token bottle.response.delete_cookie(_cookie_name) _log_debug("requesting token for record %s on %s" % (record_id, api_base)) smart = _smart_client(api_base, record_id) if smart is None: return False, None smart.token = None try: token = smart.fetch_request_token() except Exception, e: return False, str(e)
def _testrecord_from_request(request): """ Returns a "TestRecord" instance from the cookie in the request. """ if request is None: return None # read the cookie cookie = request.get_cookie(_cookie_name) if cookie is None: return None # get the token ts = TokenStore() token, api_base, record_id = ts.tokenServerRecordForCookie(cookie) record = None if record_id is not None: smart_client = _smart_client(api_base, record_id) if smart_client is not None: smart_client.update_token(token) record = TestRecord(smart_client) return record