# Path: exactonline/exceptions.py
# class MultipleObjectsReturned(ExactOnlineError):
#     """
#     Exception named Django style.
#     """
#     pass
# 
# class ObjectDoesNotExist(ExactOnlineError):
#     """
#     Exception named Django style.
#     """
#     pass
# 
# Path: exactonline/http.py
# def binquote(value):
#     """
#     We use quote() instead of urlencode() because the Exact Online API
#     does not like it when we encode slashes -- in the redirect_uri -- as
#     well (which the latter does).
#     """
#     return quote(value.encode('utf-8'))
# 
# Path: exactonline/resource.py
# class DELETE(_Resource):
#     pass
# 
# class GET(_Resource):
#     pass
# 
# class POST(_Resource):
#     pass
# 
# class PUT(_Resource):
#     pass


from ..exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from ..http import binquote
from ..resource import DELETE, GET, POST, PUT

    def __init__(self, api):
        self._api = api

    # == GET / get one / get many ==

    def all(self):
        # Select all without filtering.
        return self.filter()

    def get(self, **kwargs):
        assert 'top' not in kwargs
        ret = self.filter(top=2, **kwargs)
        if not ret:
            raise ObjectDoesNotExist()
        if len(ret) > 1:
            raise MultipleObjectsReturned()
        return ret[0]

    def filter(self, **kwargs):
        # kwargs = {'filter': "EntryDate+gt+datetime'2014-01-01'", 'top': 5}
        args = []
        for key, value in kwargs.items():
            args.append('$%s=%s' % (
                key, binquote(to_unistr(value))))
        if args:
            args = ('?' + '&'.join(args))
        else:
            args = ''


# Path: exactonline/exceptions.py
# class MultipleObjectsReturned(ExactOnlineError):
#     """
#     Exception named Django style.
#     """
#     pass
# 
# class ObjectDoesNotExist(ExactOnlineError):
#     """
#     Exception named Django style.
#     """
#     pass
# 
# Path: exactonline/http.py
# def binquote(value):
#     """
#     We use quote() instead of urlencode() because the Exact Online API
#     does not like it when we encode slashes -- in the redirect_uri -- as
#     well (which the latter does).
#     """
#     return quote(value.encode('utf-8'))
# 
# Path: exactonline/resource.py
# class DELETE(_Resource):
#     pass
# 
# class GET(_Resource):
#     pass
# 
# class POST(_Resource):
#     pass
# 
# class PUT(_Resource):
#     pass


# Path: exactonline/api/manager.py
from ..exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from ..http import binquote
from ..resource import DELETE, GET, POST, PUT


    def __init__(self, api):
        self._api = api

    # == GET / get one / get many ==

    def all(self):
        # Select all without filtering.
        return self.filter()

    def get(self, **kwargs):
        assert 'top' not in kwargs
        ret = self.filter(top=2, **kwargs)
        if not ret:
            raise ObjectDoesNotExist()
        if len(ret) > 1:
            raise MultipleObjectsReturned()
        return ret[0]

    def filter(self, **kwargs):
        # kwargs = {'filter': "EntryDate+gt+datetime'2014-01-01'", 'top': 5}
        args = []
        for key, value in kwargs.items():
            args.append('$%s=%s' % (
                key, binquote(to_unistr(value))))
        if args:
            args = ('?' + '&'.join(args))
        else:
            args = ''


        ret = self._api.restv1(GET(self.resource + args))