# Path: apitools/base/py/exceptions.py
# class Error(Exception):
# class TypecheckError(Error, TypeError):
# class NotFoundError(Error):
# class UserError(Error):
# class InvalidDataError(Error):
# class CommunicationError(Error):
# class HttpError(CommunicationError):
# class InvalidUserInputError(InvalidDataError):
# class InvalidDataFromServerError(InvalidDataError, CommunicationError):
# class BatchError(Error):
# class ConfigurationError(Error):
# class GeneratedClientError(Error):
# class ConfigurationValueError(UserError):
# class ResourceUnavailableError(Error):
# class CredentialsError(Error):
# class TransferError(CommunicationError):
# class TransferRetryError(TransferError):
# class TransferInvalidError(TransferError):
# class RequestError(CommunicationError):
# class RetryAfterError(HttpError):
# class BadStatusCodeError(HttpError):
# class NotYetImplementedError(GeneratedClientError):
# class StreamExhausted(Error):
#   def __init__(self, response, content, url):
#   def __str__(self):
#   def status_code(self):
#   def FromResponse(cls, http_response):
#   def __init__(self, response, content, url, retry_after):
#   def FromResponse(cls, http_response):
# 
# Path: apitools/base/py/util.py
# _RESERVED_URI_CHARS = r":/?#[]@!$&'()*+,;="
# def DetectGae():
# def DetectGce():
# def NormalizeScopes(scope_spec):
# def Typecheck(arg, arg_type, msg=None):
# def ExpandRelativePath(method_config, params, relative_path=None):
# def CalculateWaitForRetry(retry_attempt, max_wait=60):
# def AcceptableMimeType(accept_patterns, mime_type):
#   def MimeTypeMatches(pattern, mime_type):
# def MapParamNames(params, request_type):
# def MapRequestParams(params, request_type):


import collections
import contextlib
import logging
import socket
import time
import httplib2
import six
from six.moves import http_client
from six.moves.urllib import parse
from apitools.base.py import exceptions
from apitools.base.py import util
  # Transport failures
  if isinstance(retry_args.exc, http_client.BadStatusLine):
    logging.debug('Caught BadStatusLine from httplib, retrying: %s',
                  retry_args.exc)
  elif isinstance(retry_args.exc, socket.error):
    logging.debug('Caught socket error, retrying: %s', retry_args.exc)
  elif isinstance(retry_args.exc, socket.gaierror):
    logging.debug('Caught socket address error, retrying: %s', retry_args.exc)
  elif isinstance(retry_args.exc, httplib2.ServerNotFoundError):
    logging.debug('Caught server not found error, retrying: %s', retry_args.exc)
  elif isinstance(retry_args.exc, ValueError):
    # oauth2client tries to JSON-decode the response, which can result
    # in a ValueError if the response was invalid. Until that is fixed in
    # oauth2client, need to handle it here.
    logging.debug('Response content was invalid (%s), retrying',
                  retry_args.exc)
  elif isinstance(retry_args.exc, exceptions.RequestError):
    logging.debug('Request returned no response, retrying')
  # API-level failures
  elif isinstance(retry_args.exc, exceptions.BadStatusCodeError):
    logging.debug('Response returned status %s, retrying',
                  retry_args.exc.status_code)
  elif isinstance(retry_args.exc, exceptions.RetryAfterError):
    logging.debug('Response returned a retry-after header, retrying')
    retry_after = retry_args.exc.retry_after
  else:
    raise
  RebuildHttpConnections(retry_args.http)
  logging.debug('Retrying request to url %s after exception %s',
                retry_args.http_request.url, retry_args.exc)

# Path: apitools/base/py/exceptions.py
# class Error(Exception):
# class TypecheckError(Error, TypeError):
# class NotFoundError(Error):
# class UserError(Error):
# class InvalidDataError(Error):
# class CommunicationError(Error):
# class HttpError(CommunicationError):
# class InvalidUserInputError(InvalidDataError):
# class InvalidDataFromServerError(InvalidDataError, CommunicationError):
# class BatchError(Error):
# class ConfigurationError(Error):
# class GeneratedClientError(Error):
# class ConfigurationValueError(UserError):
# class ResourceUnavailableError(Error):
# class CredentialsError(Error):
# class TransferError(CommunicationError):
# class TransferRetryError(TransferError):
# class TransferInvalidError(TransferError):
# class RequestError(CommunicationError):
# class RetryAfterError(HttpError):
# class BadStatusCodeError(HttpError):
# class NotYetImplementedError(GeneratedClientError):
# class StreamExhausted(Error):
#   def __init__(self, response, content, url):
#   def __str__(self):
#   def status_code(self):
#   def FromResponse(cls, http_response):
#   def __init__(self, response, content, url, retry_after):
#   def FromResponse(cls, http_response):
# 
# Path: apitools/base/py/util.py
# _RESERVED_URI_CHARS = r":/?#[]@!$&'()*+,;="
# def DetectGae():
# def DetectGce():
# def NormalizeScopes(scope_spec):
# def Typecheck(arg, arg_type, msg=None):
# def ExpandRelativePath(method_config, params, relative_path=None):
# def CalculateWaitForRetry(retry_attempt, max_wait=60):
# def AcceptableMimeType(accept_patterns, mime_type):
#   def MimeTypeMatches(pattern, mime_type):
# def MapParamNames(params, request_type):
# def MapRequestParams(params, request_type):


# Path: apitools/base/py/http_wrapper.py
import collections
import contextlib
import logging
import socket
import time
import httplib2
import six
from six.moves import http_client
from six.moves.urllib import parse
from apitools.base.py import exceptions
from apitools.base.py import util

  # Transport failures
  if isinstance(retry_args.exc, http_client.BadStatusLine):
    logging.debug('Caught BadStatusLine from httplib, retrying: %s',
                  retry_args.exc)
  elif isinstance(retry_args.exc, socket.error):
    logging.debug('Caught socket error, retrying: %s', retry_args.exc)
  elif isinstance(retry_args.exc, socket.gaierror):
    logging.debug('Caught socket address error, retrying: %s', retry_args.exc)
  elif isinstance(retry_args.exc, httplib2.ServerNotFoundError):
    logging.debug('Caught server not found error, retrying: %s', retry_args.exc)
  elif isinstance(retry_args.exc, ValueError):
    # oauth2client tries to JSON-decode the response, which can result
    # in a ValueError if the response was invalid. Until that is fixed in
    # oauth2client, need to handle it here.
    logging.debug('Response content was invalid (%s), retrying',
                  retry_args.exc)
  elif isinstance(retry_args.exc, exceptions.RequestError):
    logging.debug('Request returned no response, retrying')
  # API-level failures
  elif isinstance(retry_args.exc, exceptions.BadStatusCodeError):
    logging.debug('Response returned status %s, retrying',
                  retry_args.exc.status_code)
  elif isinstance(retry_args.exc, exceptions.RetryAfterError):
    logging.debug('Response returned a retry-after header, retrying')
    retry_after = retry_args.exc.retry_after
  else:
    raise
  RebuildHttpConnections(retry_args.http)
  logging.debug('Retrying request to url %s after exception %s',
                retry_args.http_request.url, retry_args.exc)

  time.sleep(retry_after or util.CalculateWaitForRetry(retry_args.num_retries))