# 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)
# 
# def assert_document_type_id(docid):
#     """
#     Validate the document ID.  Raises an error if the ID is an `_` prefixed name
#     that isn't either `_design` or `_local`.
#     :return:
#     """
#     invalid = False
#     if docid.startswith('_'):
#         if docid.startswith(DESIGN_PREFIX) and DESIGN_PREFIX != docid:
#             invalid = False
#         elif docid.startswith(LOCAL_PREFIX) and LOCAL_PREFIX != docid:
#             invalid = False
#         else:
#             invalid = True
#     if invalid:
#         raise CloudantArgumentError(137, docid)
# 
# def assert_attachment_name(attname):
#     """
#     Validate the document attachment's name.  Raises an error if `_` prefixed name exists.
#     :return:
#     """
#     if attname.startswith('_'):
#         raise CloudantArgumentError(138, attname)
# 
# Path: src/cloudant/error.py
# class CloudantDocumentException(CloudantException):
#     """
#     Provides a way to issue Cloudant library document specific exceptions.
# 
#     :param int code: A code value used to identify the document exception.
#     :param args: A list of arguments used to format the exception message.
#     """
#     def __init__(self, code=100, *args):
#         try:
#             msg = DOCUMENT[code].format(*args)
#         except (KeyError, IndexError):
#             code = 100
#             msg = DOCUMENT[code]
#         super(CloudantDocumentException, self).__init__(msg, code)


import json
import requests
from requests.exceptions import HTTPError
from ._2to3 import url_quote, url_quote_plus
from ._common_util import response_to_json_dict, assert_document_type_id, assert_attachment_name
from .error import CloudantDocumentException
        """
        Returns the database instance ``r_session`` used by the document.

        :returns: Client ``r_session``
        """
        return self._client.r_session

    @property
    def document_url(self):
        """
        Constructs and returns the document URL.

        :returns: Document URL
        """
        if '_id' not in self or self['_id'] is None:
            return None

        # handle design document url
        if self['_id'].startswith('_design/'):
            return '/'.join((
                self._database_host,
                url_quote_plus(self._database_name),
                '_design',
                url_quote(self['_id'][8:], safe='')
            ))

        # handle _local document url
        if self['_id'].startswith('_local/'):
            return '/'.join((
                self._database_host,

# 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)
# 
# def assert_document_type_id(docid):
#     """
#     Validate the document ID.  Raises an error if the ID is an `_` prefixed name
#     that isn't either `_design` or `_local`.
#     :return:
#     """
#     invalid = False
#     if docid.startswith('_'):
#         if docid.startswith(DESIGN_PREFIX) and DESIGN_PREFIX != docid:
#             invalid = False
#         elif docid.startswith(LOCAL_PREFIX) and LOCAL_PREFIX != docid:
#             invalid = False
#         else:
#             invalid = True
#     if invalid:
#         raise CloudantArgumentError(137, docid)
# 
# def assert_attachment_name(attname):
#     """
#     Validate the document attachment's name.  Raises an error if `_` prefixed name exists.
#     :return:
#     """
#     if attname.startswith('_'):
#         raise CloudantArgumentError(138, attname)
# 
# Path: src/cloudant/error.py
# class CloudantDocumentException(CloudantException):
#     """
#     Provides a way to issue Cloudant library document specific exceptions.
# 
#     :param int code: A code value used to identify the document exception.
#     :param args: A list of arguments used to format the exception message.
#     """
#     def __init__(self, code=100, *args):
#         try:
#             msg = DOCUMENT[code].format(*args)
#         except (KeyError, IndexError):
#             code = 100
#             msg = DOCUMENT[code]
#         super(CloudantDocumentException, self).__init__(msg, code)


# Path: src/cloudant/document.py
import json
import requests
from requests.exceptions import HTTPError
from ._2to3 import url_quote, url_quote_plus
from ._common_util import response_to_json_dict, assert_document_type_id, assert_attachment_name
from .error import CloudantDocumentException

        """
        Returns the database instance ``r_session`` used by the document.

        :returns: Client ``r_session``
        """
        return self._client.r_session

    @property
    def document_url(self):
        """
        Constructs and returns the document URL.

        :returns: Document URL
        """
        if '_id' not in self or self['_id'] is None:
            return None

        # handle design document url
        if self['_id'].startswith('_design/'):
            return '/'.join((
                self._database_host,
                url_quote_plus(self._database_name),
                '_design',
                url_quote(self['_id'][8:], safe='')
            ))

        # handle _local document url
        if self['_id'].startswith('_local/'):
            return '/'.join((
                self._database_host,

                url_quote_plus(self._database_name),