# Path: src/cloudant/_2to3.py
# LONGTYPE = long if PY2 else int
# 
# STRTYPE = basestring if PY2 else str
# 
# NONETYPE = type(None)
# 
# UNITYPE = unicode if PY2 else str
# 
# def iteritems_(adict):
#     """
#     iterate dict key, value tuples in a py2 and 3 compatible way
# 
#     :param dict adict:
#     :return: iterator of (key, value) tuples
#     """
#     return adict.iteritems()
# 
# Path: src/cloudant/error.py
# class CloudantArgumentError(CloudantException):
#     """
#     Provides a way to issue Cloudant Python client library specific exceptions
#     that pertain to invalid argument errors.
# 
#     Note:  The intended use for this class is internal to the Cloudant Python
#     client library.
# 
#     :param int code: An optional code value used to identify the exception.
#         Defaults to 100.
#     :param args: A list of arguments used to format the exception message.
#     """
#     def __init__(self, code=100, *args):
#         try:
#             msg = ARGUMENT_ERROR[code].format(*args)
#         except (KeyError, IndexError):
#             code = 100
#             msg = ARGUMENT_ERROR[code]
#         super(CloudantArgumentError, self).__init__(msg, code)
# 
# 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
# 
# class CloudantClientException(CloudantException):
#     """
#     Provides a way to issue Cloudant library client specific exceptions.
# 
#     :param int code: A code value used to identify the client exception.
#     :param args: A list of arguments used to format the exception message.
#     """
#     def __init__(self, code=100, *args):
#         try:
#             msg = CLIENT[code].format(*args)
#         except (KeyError, IndexError):
#             code = 100
#             msg = CLIENT[code]
#         super(CloudantClientException, self).__init__(msg, code)


import sys
import platform
import json
from ._2to3 import LONGTYPE, STRTYPE, NONETYPE, UNITYPE, iteritems_
from .error import CloudantArgumentError, CloudantException, CloudantClientException
    from collections.abc import Sequence
    from collections import Sequence
    """ Manages Cloud Foundry service configuration. """

    def __init__(self, vcap_services, instance_name=None, service_name=None):
        try:
            services = vcap_services
            if not isinstance(vcap_services, dict):
                services = json.loads(vcap_services)

            cloudant_services = services.get(service_name, [])

            # use first service if no name given and only one service present
            use_first = instance_name is None and len(cloudant_services) == 1
            for service in cloudant_services:
                if use_first or service.get('name') == instance_name:
                    credentials = service['credentials']
                    self._host = credentials['host']
                    self._name = service.get('name')
                    self._port = credentials.get('port', 443)
                    self._username = credentials['username']
                    if 'apikey' in credentials:
                        self._iam_api_key = credentials['apikey']
                    elif 'username' in credentials and 'password' in credentials:
                        self._password = credentials['password']
                    else:
                        raise CloudantClientException(103)
                    break
            else:
                raise CloudantException('Missing service in VCAP_SERVICES')

        except KeyError as ex:

# Path: src/cloudant/_2to3.py
# LONGTYPE = long if PY2 else int
# 
# STRTYPE = basestring if PY2 else str
# 
# NONETYPE = type(None)
# 
# UNITYPE = unicode if PY2 else str
# 
# def iteritems_(adict):
#     """
#     iterate dict key, value tuples in a py2 and 3 compatible way
# 
#     :param dict adict:
#     :return: iterator of (key, value) tuples
#     """
#     return adict.iteritems()
# 
# Path: src/cloudant/error.py
# class CloudantArgumentError(CloudantException):
#     """
#     Provides a way to issue Cloudant Python client library specific exceptions
#     that pertain to invalid argument errors.
# 
#     Note:  The intended use for this class is internal to the Cloudant Python
#     client library.
# 
#     :param int code: An optional code value used to identify the exception.
#         Defaults to 100.
#     :param args: A list of arguments used to format the exception message.
#     """
#     def __init__(self, code=100, *args):
#         try:
#             msg = ARGUMENT_ERROR[code].format(*args)
#         except (KeyError, IndexError):
#             code = 100
#             msg = ARGUMENT_ERROR[code]
#         super(CloudantArgumentError, self).__init__(msg, code)
# 
# 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
# 
# class CloudantClientException(CloudantException):
#     """
#     Provides a way to issue Cloudant library client specific exceptions.
# 
#     :param int code: A code value used to identify the client exception.
#     :param args: A list of arguments used to format the exception message.
#     """
#     def __init__(self, code=100, *args):
#         try:
#             msg = CLIENT[code].format(*args)
#         except (KeyError, IndexError):
#             code = 100
#             msg = CLIENT[code]
#         super(CloudantClientException, self).__init__(msg, code)


# Path: src/cloudant/_common_util.py
import sys
import platform
import json
from ._2to3 import LONGTYPE, STRTYPE, NONETYPE, UNITYPE, iteritems_
from .error import CloudantArgumentError, CloudantException, CloudantClientException
    from collections.abc import Sequence
    from collections import Sequence

    """ Manages Cloud Foundry service configuration. """

    def __init__(self, vcap_services, instance_name=None, service_name=None):
        try:
            services = vcap_services
            if not isinstance(vcap_services, dict):
                services = json.loads(vcap_services)

            cloudant_services = services.get(service_name, [])

            # use first service if no name given and only one service present
            use_first = instance_name is None and len(cloudant_services) == 1
            for service in cloudant_services:
                if use_first or service.get('name') == instance_name:
                    credentials = service['credentials']
                    self._host = credentials['host']
                    self._name = service.get('name')
                    self._port = credentials.get('port', 443)
                    self._username = credentials['username']
                    if 'apikey' in credentials:
                        self._iam_api_key = credentials['apikey']
                    elif 'username' in credentials and 'password' in credentials:
                        self._password = credentials['password']
                    else:
                        raise CloudantClientException(103)
                    break
            else:
                raise CloudantException('Missing service in VCAP_SERVICES')

        except KeyError as ex:

            raise CloudantException(