# 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

        :param str username: Username parameter is unused.
        :param str api_key: New IAM API key.
        """
        if api_key is not None:
            self._api_key = api_key

    def _get_access_token(self):
        """
        Get IAM access token using API key.
        """
        err = 'Failed to contact IAM token service'
        try:
            resp = super(IAMSession, self).request(
                'POST',
                self._token_url,
                auth=self._token_auth,
                headers={'Accepts': 'application/json'},
                data={
                    'grant_type': 'urn:ibm:params:oauth:grant-type:apikey',
                    'response_type': 'cloud_iam',
                    'apikey': self._api_key
                }
            )
            err = response_to_json_dict(resp).get('errorMessage', err)
            resp.raise_for_status()

            return response_to_json_dict(resp)['access_token']

        except KeyError:

# 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


        :param str username: Username parameter is unused.
        :param str api_key: New IAM API key.
        """
        if api_key is not None:
            self._api_key = api_key

    def _get_access_token(self):
        """
        Get IAM access token using API key.
        """
        err = 'Failed to contact IAM token service'
        try:
            resp = super(IAMSession, self).request(
                'POST',
                self._token_url,
                auth=self._token_auth,
                headers={'Accepts': 'application/json'},
                data={
                    'grant_type': 'urn:ibm:params:oauth:grant-type:apikey',
                    'response_type': 'cloud_iam',
                    'apikey': self._api_key
                }
            )
            err = response_to_json_dict(resp).get('errorMessage', err)
            resp.raise_for_status()

            return response_to_json_dict(resp)['access_token']

        except KeyError:

            raise CloudantException('Invalid response from IAM token service')