# Path: src/cloudant/_2to3.py
# STRTYPE = basestring 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/_common_util.py
# JSON_INDEX_TYPE = 'json'
# 
# Path: src/cloudant/_common_util.py
# TEXT_INDEX_TYPE = 'text'
# 
# Path: src/cloudant/_common_util.py
# SPECIAL_INDEX_TYPE = 'special'
# 
# Path: src/cloudant/_common_util.py
# TEXT_INDEX_ARGS = {'fields': list, 'default_field': dict, 'selector': dict}
# 
# 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 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 CloudantIndexException(CloudantException):
#     """
#     Provides a way to issue Cloudant library index specific exceptions.
# 
#     :param int code: A code value used to identify the index exception.
#     :param args: A list of arguments used to format the exception message.
#     """
#     def __init__(self, code=100, *args):
#         try:
#             msg = INDEX[code].format(*args)
#         except (KeyError, IndexError):
#             code = 100
#             msg = INDEX[code]
#         super(CloudantIndexException, self).__init__(msg, code)


import json
from ._2to3 import STRTYPE, iteritems_
from ._common_util import JSON_INDEX_TYPE
from ._common_util import TEXT_INDEX_TYPE
from ._common_util import SPECIAL_INDEX_TYPE
from ._common_util import TEXT_INDEX_ARGS
from ._common_util import response_to_json_dict
from .error import CloudantArgumentError, CloudantIndexException
        Creates the current index in the remote database.
        """
        payload = {'type': self._type}
        if self._ddoc_id and self._ddoc_id != '':
            if isinstance(self._ddoc_id, STRTYPE):
                if self._ddoc_id.startswith('_design/'):
                    payload['ddoc'] = self._ddoc_id[8:]
                else:
                    payload['ddoc'] = self._ddoc_id
            else:
                raise CloudantArgumentError(122, self._ddoc_id)
        if self._name and self._name != '':
            if isinstance(self._name, STRTYPE):
                payload['name'] = self._name
            else:
                raise CloudantArgumentError(123, self._name)
        self._def_check()
        payload['index'] = self._def

        if self._partitioned is not None:
            payload['partitioned'] = bool(self._partitioned)

        headers = {'Content-Type': 'application/json'}
        resp = self._r_session.post(
            self.index_url,
            data=json.dumps(payload, cls=self._database.client.encoder),
            headers=headers
        )
        resp.raise_for_status()
        self._ddoc_id = response_to_json_dict(resp)['id']

# Path: src/cloudant/_2to3.py
# STRTYPE = basestring 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/_common_util.py
# JSON_INDEX_TYPE = 'json'
# 
# Path: src/cloudant/_common_util.py
# TEXT_INDEX_TYPE = 'text'
# 
# Path: src/cloudant/_common_util.py
# SPECIAL_INDEX_TYPE = 'special'
# 
# Path: src/cloudant/_common_util.py
# TEXT_INDEX_ARGS = {'fields': list, 'default_field': dict, 'selector': dict}
# 
# 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 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 CloudantIndexException(CloudantException):
#     """
#     Provides a way to issue Cloudant library index specific exceptions.
# 
#     :param int code: A code value used to identify the index exception.
#     :param args: A list of arguments used to format the exception message.
#     """
#     def __init__(self, code=100, *args):
#         try:
#             msg = INDEX[code].format(*args)
#         except (KeyError, IndexError):
#             code = 100
#             msg = INDEX[code]
#         super(CloudantIndexException, self).__init__(msg, code)


# Path: src/cloudant/index.py
import json
from ._2to3 import STRTYPE, iteritems_
from ._common_util import JSON_INDEX_TYPE
from ._common_util import TEXT_INDEX_TYPE
from ._common_util import SPECIAL_INDEX_TYPE
from ._common_util import TEXT_INDEX_ARGS
from ._common_util import response_to_json_dict
from .error import CloudantArgumentError, CloudantIndexException

        Creates the current index in the remote database.
        """
        payload = {'type': self._type}
        if self._ddoc_id and self._ddoc_id != '':
            if isinstance(self._ddoc_id, STRTYPE):
                if self._ddoc_id.startswith('_design/'):
                    payload['ddoc'] = self._ddoc_id[8:]
                else:
                    payload['ddoc'] = self._ddoc_id
            else:
                raise CloudantArgumentError(122, self._ddoc_id)
        if self._name and self._name != '':
            if isinstance(self._name, STRTYPE):
                payload['name'] = self._name
            else:
                raise CloudantArgumentError(123, self._name)
        self._def_check()
        payload['index'] = self._def

        if self._partitioned is not None:
            payload['partitioned'] = bool(self._partitioned)

        headers = {'Content-Type': 'application/json'}
        resp = self._r_session.post(
            self.index_url,
            data=json.dumps(payload, cls=self._database.client.encoder),
            headers=headers
        )
        resp.raise_for_status()
        self._ddoc_id = response_to_json_dict(resp)['id']

        self._name = response_to_json_dict(resp)['name']