# Path: src/cloudant/_2to3.py
# PY2 = sys.version_info[0] < 3
# ENCODING = 'utf-8'
# NONETYPE = type(None)
# STRTYPE = basestring if PY2 else str
# UNITYPE = unicode if PY2 else str
# LONGTYPE = long if PY2 else int
# UNICHR = unichr if PY2 else chr
#     def iteritems_(adict):
#     def next_(itr):
#     def iteritems_(adict):
#     def next_(itr):
# def bytes_(astr):
# def unicode_(astr):
# 
# Path: src/cloudant/_common_util.py
# def response_to_json_dict(response, **kwargs):
#     """
#     Standard place to convert responses to JSON.
# 
#     :param response: requests response object
#     :param **kwargs: arguments accepted by json.loads
# 
#     :returns: dict of JSON response
#     """
#     if response.encoding is None:
#         response.encoding = 'utf-8'
#     return json.loads(response.text, **kwargs)
# 
# Path: src/cloudant/error.py
# class CloudantException(Exception):
#     """
#     Provides a way to issue Cloudant Python client library specific exceptions.
#     A CloudantException object is instantiated with a message and optional code.
# 
#     Note:  The intended use for this class is internal to the Cloudant Python
#     client library.
# 
#     :param str msg: A message that describes the exception.
#     :param int code: A code value used to identify the exception.
#     """
#     def __init__(self, msg, code=None):
#         super(CloudantException, self).__init__(msg)
#         self.status_code = code


import base64
import json
import os
from requests import RequestException, Session
from ._2to3 import bytes_, unicode_, url_join
from ._common_util import response_to_json_dict
from .error import CloudantException
        """
        Overrides ``requests.Session.request`` to renew the cookie and then
        retry the original request (if required).
        """
        resp = super(CookieSession, self).request(method, url, **kwargs)

        if not self._auto_renew:
            return resp

        is_expired = any((
            resp.status_code == 403 and
            response_to_json_dict(resp).get('error') == 'credentials_expired',
            resp.status_code == 401
        ))

        if is_expired:
            self.login()
            resp = super(CookieSession, self).request(method, url, **kwargs)

        return resp


class IAMSession(ClientSession):
    """
    This class extends ClientSession and provides IAM authentication.
    """

    def __init__(self, api_key, server_url, client_id=None, client_secret=None,
                 **kwargs):
        super(IAMSession, self).__init__(

# Path: src/cloudant/_2to3.py
# PY2 = sys.version_info[0] < 3
# ENCODING = 'utf-8'
# NONETYPE = type(None)
# STRTYPE = basestring if PY2 else str
# UNITYPE = unicode if PY2 else str
# LONGTYPE = long if PY2 else int
# UNICHR = unichr if PY2 else chr
#     def iteritems_(adict):
#     def next_(itr):
#     def iteritems_(adict):
#     def next_(itr):
# def bytes_(astr):
# def unicode_(astr):
# 
# Path: src/cloudant/_common_util.py
# def response_to_json_dict(response, **kwargs):
#     """
#     Standard place to convert responses to JSON.
# 
#     :param response: requests response object
#     :param **kwargs: arguments accepted by json.loads
# 
#     :returns: dict of JSON response
#     """
#     if response.encoding is None:
#         response.encoding = 'utf-8'
#     return json.loads(response.text, **kwargs)
# 
# Path: src/cloudant/error.py
# class CloudantException(Exception):
#     """
#     Provides a way to issue Cloudant Python client library specific exceptions.
#     A CloudantException object is instantiated with a message and optional code.
# 
#     Note:  The intended use for this class is internal to the Cloudant Python
#     client library.
# 
#     :param str msg: A message that describes the exception.
#     :param int code: A code value used to identify the exception.
#     """
#     def __init__(self, msg, code=None):
#         super(CloudantException, self).__init__(msg)
#         self.status_code = code


# Path: src/cloudant/_client_session.py
import base64
import json
import os
from requests import RequestException, Session
from ._2to3 import bytes_, unicode_, url_join
from ._common_util import response_to_json_dict
from .error import CloudantException

        """
        Overrides ``requests.Session.request`` to renew the cookie and then
        retry the original request (if required).
        """
        resp = super(CookieSession, self).request(method, url, **kwargs)

        if not self._auto_renew:
            return resp

        is_expired = any((
            resp.status_code == 403 and
            response_to_json_dict(resp).get('error') == 'credentials_expired',
            resp.status_code == 401
        ))

        if is_expired:
            self.login()
            resp = super(CookieSession, self).request(method, url, **kwargs)

        return resp


class IAMSession(ClientSession):
    """
    This class extends ClientSession and provides IAM authentication.
    """

    def __init__(self, api_key, server_url, client_id=None, client_secret=None,
                 **kwargs):
        super(IAMSession, self).__init__(

            session_url=url_join(server_url, '_iam_session'),