def get_remote_app_id(remote_url, extra_headers=None):
"""Get the app_id from the remote_api endpoint.
This also has the side effect of verifying that it is a remote_api endpoint.
Args:
remote_url: The url to the remote_api handler.
extra_headers: Headers to send (for authentication).
Returns:
app_id: The app_id of the target app.
Raises:
FetchFailed: Urlfetch call failed.
ConfigurationError: URLfetch succeeded but results were invalid.
"""
rtok = str(random.random())[2:]
url = remote_url + '?rtok=' + rtok
if not extra_headers:
extra_headers = {}
if 'X-appcfg-api-version' not in extra_headers:
extra_headers['X-appcfg-api-version'] = '1'
try:
urlfetch_response = urlfetch.fetch(url, None, urlfetch.GET,
extra_headers, follow_redirects=False,
deadline=10)
except Exception, e:
logging.exception('Fetch failed to %s', remote_url)
raise FetchFailed('Fetch to %s failed: %r' % (remote_url, e))
if urlfetch_response.status_code != 200:
logging.error('Fetch failed to %s; Status %s; body %s',
remote_url,
urlfetch_response.status_code,
urlfetch_response.content)
raise FetchFailed('Fetch to %s failed with status %s' %
(remote_url, urlfetch_response.status_code))
response = urlfetch_response.content
if not response.startswith('{'):
logging.info('Response unparasable: %s', response)
raise ConfigurationError(
'Invalid response received from server: %s' % response)
app_info = yaml.load(response)
if not app_info or 'rtok' not in app_info or 'app_id' not in app_info:
logging.info('Response unparsable: %s', response)
raise ConfigurationError('Error parsing app_id lookup response')
if str(app_info['rtok']) != rtok:
logging.info('Response invalid token (expected %s): %s', rtok, response)
raise ConfigurationError('Token validation failed during app_id lookup. '
'(sent %s, got %s)' % (repr(rtok),
repr(app_info['rtok'])))
return app_info['app_id']
remote_api_put_stub.py 文件源码
python
阅读 20
收藏 0
点赞 0
评论 0
评论列表
文章目录