# Path: chat/asset.py
# def sign_asset_url(name):
#     signer = get_signer()
#     return signer.sign(name)
# 
# Path: chat/exc.py
# class SkygearChatException(SkygearException):
#     pass


from skygear.encoding import deserialize_record, serialize_record
from skygear.models import Record
from .asset import sign_asset_url
from .exc import SkygearChatException

    def delete(self, arg):
        if not isinstance(arg, list):
            arg = [arg]
        ids = [Database._encode_id(item.id)
               if isinstance(item, Record)
               else item
               for item in arg]
        return self.container.send_action('record:delete', {
            'database_id': self.database_id,
            'ids': ids
        })

    def query(self, query):
        include = {v: {"$type": "keypath", "$val": v}
                   for v in list(set(query.include))}

        payload = {'database_id': self.database_id,
                   'record_type': query.record_type,
                   'predicate': query.predicate.to_dict(),
                   'count': query.count,
                   'sort': query.sort,
                   'include': include}

        if query.offset is not None:
            payload['offset'] = query.offset
        if query.limit is not None:
            payload['limit'] = query.limit
        result = self.container.send_action('record:query', payload)
        if 'error' in result:

# Path: chat/asset.py
# def sign_asset_url(name):
#     signer = get_signer()
#     return signer.sign(name)
# 
# Path: chat/exc.py
# class SkygearChatException(SkygearException):
#     pass


# Path: chat/database.py
from skygear.encoding import deserialize_record, serialize_record
from skygear.models import Record
from .asset import sign_asset_url
from .exc import SkygearChatException


    def delete(self, arg):
        if not isinstance(arg, list):
            arg = [arg]
        ids = [Database._encode_id(item.id)
               if isinstance(item, Record)
               else item
               for item in arg]
        return self.container.send_action('record:delete', {
            'database_id': self.database_id,
            'ids': ids
        })

    def query(self, query):
        include = {v: {"$type": "keypath", "$val": v}
                   for v in list(set(query.include))}

        payload = {'database_id': self.database_id,
                   'record_type': query.record_type,
                   'predicate': query.predicate.to_dict(),
                   'count': query.count,
                   'sort': query.sort,
                   'include': include}

        if query.offset is not None:
            payload['offset'] = query.offset
        if query.limit is not None:
            payload['limit'] = query.limit
        result = self.container.send_action('record:query', payload)
        if 'error' in result:

            raise SkygearChatException(result['error']['message'])