本文整理汇总了Python中httplib2.HttpLib2Error方法的典型用法代码示例。如果您正苦于以下问题:Python httplib2.HttpLib2Error方法的具体用法?Python httplib2.HttpLib2Error怎么用?Python httplib2.HttpLib2Error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类httplib2
的用法示例。
在下文中一共展示了httplib2.HttpLib2Error方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _execute_insert_request
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def _execute_insert_request(self, request):
"""Executes a table/dataset insert request, retrying on transport errors."""
for i in range(NUM_RETRIES + 1):
try:
request.execute()
return True
except HttpError as e:
if e.resp.status == 409:
# Already exists.
return True
logs.log_error('Failed to insert table/dataset.')
return False
except httplib2.HttpLib2Error:
# Transport error.
time.sleep(random.uniform(0, (1 << i) * RETRY_SLEEP_TIME))
continue
logs.log_error('Failed to insert table/dataset.')
return False
示例2: object_put
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def object_put(auth, path, data, mimetype='application/octet-stream'):
bucket, filename = path.split(':', 1)
service = get_service('storage', 'v1', auth)
media = MediaIoBaseUpload(data, mimetype=mimetype, chunksize=CHUNKSIZE, resumable=True)
request = service.objects().insert(bucket=bucket, name=filename, media_body=media)
response = None
errors = 0
while response is None:
error = None
try:
status, response = request.next_chunk()
if project.verbose and status: print("Uploaded %d%%." % int(status.progress() * 100))
except HttpError as e:
if e.resp.status < 500: raise
error = e
except (httplib2.HttpLib2Error, IOError) as e:
error = e
errors = (errors + 1) if error else 0
if errors > RETRIES: raise error
if project.verbose: print("Uploaded 100%.")
示例3: _refresh
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def _refresh(self, http_request):
"""Refreshes the access_token.
Skip all the storage hoops and just refresh using the API.
Args:
http_request: callable, a callable that matches the method
signature of httplib2.Http.request, used to make
the refresh request.
Raises:
HttpAccessTokenRefreshError: When the refresh fails.
"""
try:
self._retrieve_info(http_request)
self.access_token, self.token_expiry = _metadata.get_token(
http_request, service_account=self.service_account_email)
except httplib2.HttpLib2Error as e:
raise client.HttpAccessTokenRefreshError(str(e))
示例4: request
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def request(self, uri, method='GET', body=None, *args, **kwargs):
for i in range(1, self._max_tries + 1):
try:
response, content = self._http.request(uri, method, body, *args,
**kwargs)
if self._retrying_statuses_fn(response.status):
logging.info('RetriableHttp: attempt %d receiving status %d, %s',
i, response.status,
'final attempt' if i == self._max_tries else \
'will retry')
else:
break
except (ValueError, errors.Error,
socket.timeout, socket.error, socket.herror, socket.gaierror,
httplib2.HttpLib2Error) as error:
logging.info('RetriableHttp: attempt %d received exception: %s, %s',
i, error, 'final attempt' if i == self._max_tries else \
'will retry')
if i == self._max_tries:
raise
time.sleep(self._backoff_time)
return response, content
示例5: send
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def send(self, metric_pb):
body = self.encode_to_json(metric_pb)
try:
resp, content = self._http.request(self._endpoint,
method='POST',
body=body,
headers={'Content-Type': 'application/json'})
if resp.status == 200:
self._failed = False
else:
logging.warning('HttpsMonitor.send received status %d: %s', resp.status,
content)
self._failed = True
except (ValueError, errors.Error,
socket.timeout, socket.error, socket.herror, socket.gaierror,
httplib2.HttpLib2Error):
logging.exception('HttpsMonitor.send failed')
self._failed = True
示例6: get_app
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def get_app(self, project_id):
"""Gets information about an application.
Args:
project_id (str): The id of the project.
Returns:
dict: The response of retrieving the AppEngine app.
"""
try:
results = self.repository.apps.get(project_id)
LOGGER.debug('Getting information about an application,'
' project_id = %s, result = %s', project_id, results)
return results
except (errors.HttpError, HttpLib2Error) as e:
if _is_status_not_found(e):
return {}
raise api_errors.ApiExecutionError(project_id, e)
示例7: get_service
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def get_service(self, project_id, service_id):
"""Gets information about a specific service.
Args:
project_id (str): The id of the project.
service_id (str): The id of the service to query.
Returns:
dict: A Service resource dict for a given project_id and
service_id.
"""
try:
results = self.repository.app_services.get(
project_id, target=service_id)
LOGGER.debug('Getting information about a specific service,'
' project_id = %s, service_id = %s, results = %s',
project_id, service_id, results)
return results
except (errors.HttpError, HttpLib2Error) as e:
if _is_status_not_found(e):
return {}
raise api_errors.ApiExecutionError(project_id, e)
示例8: list_services
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def list_services(self, project_id):
"""Lists services of a project.
Args:
project_id (str): The id of the project.
Returns:
list: A list of Service resource dicts for a project_id.
"""
try:
paged_results = self.repository.app_services.list(project_id)
flattened_results = api_helpers.flatten_list_results(
paged_results, 'services')
LOGGER.debug('Listing services of a project, project_id = %s,'
' flattened_results = %s',
project_id, flattened_results)
return flattened_results
except (errors.HttpError, HttpLib2Error) as e:
if _is_status_not_found(e):
return []
raise api_errors.ApiExecutionError(project_id, e)
示例9: get_version
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def get_version(self, project_id, service_id, version_id):
"""Gets information about a specific version of a service.
Args:
project_id (str): The id of the project.
service_id (str): The id of the service to query.
version_id (str): The id of the version to query.
Returns:
dict: A Version resource dict for a given project_id and
service_id.
"""
try:
results = self.repository.service_versions.get(
project_id, target=version_id, services_id=service_id)
LOGGER.debug('Getting information about a specific version'
' of a service, project_id = %s, service_id = %s,'
' version_id = %s, results = %s',
project_id, service_id, version_id, results)
return results
except (errors.HttpError, HttpLib2Error) as e:
if _is_status_not_found(e):
return {}
raise api_errors.ApiExecutionError(project_id, e)
示例10: _flatten_list_results
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def _flatten_list_results(project_id, paged_results, item_key):
"""Flatten results and handle exceptions.
Args:
project_id (str): The project id the results are for.
paged_results (list): A list of paged API response objects.
[{page 1 results}, {page 2 results}, {page 3 results}, ...]
item_key (str): The name of the key within the inner "items" lists
containing the objects of interest.
Returns:
list: A list of items.
Raises:
ApiNotEnabledError: Raised if the API is not enabled for the project.
ApiExecutionError: Raised if there is another error while calling the
API method.
"""
try:
return api_helpers.flatten_list_results(paged_results, item_key)
except (errors.HttpError, HttpLib2Error) as e:
api_not_enabled, details = _api_not_enabled(e)
if api_not_enabled:
raise api_errors.ApiNotEnabledError(details, e)
raise api_errors.ApiExecutionError(project_id, e)
示例11: get_snapshots
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def get_snapshots(self, project_id):
"""Return the list of all snapshots in the project.
Args:
project_id (str): The project id.
Returns:
list: A list of snapshot resources for this project.
"""
try:
LOGGER.debug('Getting the list of all snapshots in project: %s',
project_id)
repository = self.repository.snapshots
results = repository.list(project_id)
return api_helpers.flatten_list_results(results, 'items')
except (errors.HttpError, HttpLib2Error) as e:
api_not_enabled, details = _api_not_enabled(e)
if api_not_enabled:
err = api_errors.ApiNotEnabledError(details, e)
else:
err = api_errors.ApiExecutionError(project_id, e)
LOGGER.warning(err)
raise err
示例12: get_image
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def get_image(self, project_id, image_name):
"""Get an image from a project.
Args:
project_id (str): The project id.
image_name (str): The image name to get.
Returns:
dict: A Compute Image resource dict.
https://cloud.google.com/compute/docs/reference/latest/images
"""
try:
results = self.repository.images.get(project_id, target=image_name)
LOGGER.debug('Getting an image from a project, project_id = %s, '
'image_name = %s, results = %s',
project_id, image_name, results)
return results
except (errors.HttpError, HttpLib2Error) as e:
api_not_enabled, details = _api_not_enabled(e)
if api_not_enabled:
raise api_errors.ApiNotEnabledError(details, e)
raise api_errors.ApiExecutionError(project_id, e)
示例13: is_api_enabled
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def is_api_enabled(self, project_id):
"""Checks if the Compute API is enabled for the specified project.
Args:
project_id (str): The project id.
Returns:
bool: True if the API is enabled, else False.
"""
try:
result = self.repository.projects.get(project_id, fields='name')
LOGGER.debug('Checking if Compute API is enabled, project_id = '
'%s, result = %s', project_id, result)
return bool('name' in result) # True if name, otherwise False.
except (errors.HttpError, HttpLib2Error) as e:
api_not_enabled, _ = _api_not_enabled(e)
if api_not_enabled:
return False
raise api_errors.ApiExecutionError(project_id, e)
示例14: get_organization_sinks
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def get_organization_sinks(self, org_id):
"""Get information about organization sinks.
Args:
org_id (str): The id of the organization.
Returns:
list: The response of retrieving the organization sinks.
Raises:
ApiExecutionError: ApiExecutionError is raised if the call to the
GCP API fails.
"""
name = self.repository.organizations_sinks.get_name(org_id)
try:
paged_results = self.repository.organizations_sinks.list(name)
flattened_results = api_helpers.flatten_list_results(paged_results,
'sinks')
LOGGER.debug('Getting information about organization sinks,'
' org_id = %s, flattened_results = %s',
org_id, flattened_results)
return flattened_results
except (errors.HttpError, HttpLib2Error) as e:
api_exception = api_errors.ApiExecutionError(
'organizations_sinks', e, 'name', name)
LOGGER.exception(api_exception)
raise api_exception
示例15: get_folder_sinks
# 需要导入模块: import httplib2 [as 别名]
# 或者: from httplib2 import HttpLib2Error [as 别名]
def get_folder_sinks(self, folder_id):
"""Get information about folder sinks.
Args:
folder_id (str): The id of the folder.
Returns:
list: The response of retrieving the folder sinks.
Raises:
ApiExecutionError: ApiExecutionError is raised if the call to the
GCP API fails.
"""
name = self.repository.folders_sinks.get_name(folder_id)
try:
paged_results = self.repository.folders_sinks.list(name)
flattened_results = api_helpers.flatten_list_results(paged_results,
'sinks')
LOGGER.debug('Getting information about folder sinks,'
' folder_id = %s, flattened_results = %s',
folder_id, flattened_results)
return flattened_results
except (errors.HttpError, HttpLib2Error) as e:
api_exception = api_errors.ApiExecutionError(
'folders_sinks', e, 'name', name)
LOGGER.exception(api_exception)
raise api_exception