# Path: mollie/api/error.py
# class ResponseError(Error):
#     """Errors reported by the API."""
# 
#     status = None
#     field = None
# 
#     def __init__(self, resp=None):
#         message = resp["detail"]
#         super().__init__(message)
#         self.status = resp["status"]
#         if "field" in resp:
#             self.field = resp["field"]
# 
#     @staticmethod
#     def factory(resp):
#         """Return a ResponseError subclass based on the API payload.
# 
#         All errors are documented: https://docs.mollie.com/guides/handling-errors#all-possible-status-codes
#         More exceptions should be added here when appropriate, and when useful examples of API errors are available.
#         """
#         status = resp["status"]
#         if status == 401:
#             return UnauthorizedError(resp)
#         elif status == 404:
#             return NotFoundError(resp)
#         elif status == 422:
#             return UnprocessableEntityError(resp)
#         else:
#             # generic fallback
#             return ResponseError(resp)
# 
# class ResponseHandlingError(Error):
#     """Errors related to handling the response from the API."""
# 
#     pass
# 
# Path: mollie/api/objects/list.py
# class ObjectList(ObjectBase):
#     current = None
# 
#     def __init__(self, result, object_type, client=None):
#         # If an empty dataset was injected, we mock the structure that the remainder of the clas expects.
#         # TODO: it would be better if the ObjectList was initiated with a list of results, rather than with
#         # the full datastructure as it is now, so we can remove all this mucking around with fake data,
#         # mocked result objects, and loads of lengthy accessor workarounds everywhere in the ObjectList.
#         if result == {}:
#             result = {"_embedded": {"unknown": []}, "count": 0}
#             object_type = UnknownObject
# 
#         super().__init__(result, client)
#         self.object_type = object_type
# 
#     def __len__(self):
#         """Return the count field."""
#         return self.count
# 
#     def __iter__(self):
#         """Implement iterator interface."""
#         self.current = None
#         return self
# 
#     def __next__(self):
#         """Implement iterator interface."""
#         if self.current is None:
#             self.current = 0
#         else:
#             self.current += 1
#         try:
#             item = self["_embedded"][self.object_type.get_object_name()][self.current]
#             return self.object_type(item, self.client)
#         except IndexError:
#             self.current = None
#             raise StopIteration
# 
#     def __getitem__(self, key):
#         """Implement Sequence interface."""
#         object_name = self.object_type.get_object_name()
#         if isinstance(key, int):
#             # Return an index-based search from the "_embedded" dataset
#             item = self["_embedded"][object_name][key]
#             return self.object_type(item, self.client)
# 
#         if isinstance(key, slice):
#             _start = key.start or 0
#             _stop = key.stop or self["count"]
#             _step = key.step or 1
#             sliced_data = [self["_embedded"][object_name][x] for x in range(_start, _stop, _step)]
#             # Now we mock a result based on the sliced data
#             sliced_result = {
#                 "_embedded": {
#                     object_name: sliced_data,
#                 },
#                 "count": len(sliced_data),
#             }
#             return ObjectList(sliced_result, self.object_type, self.client)
# 
#         return super().__getitem__(key)
# 
#     @property
#     def count(self):
#         if "count" not in self:
#             return None
#         return int(self["count"])
# 
#     def has_next(self):
#         """Return True if the ObjectList contains an url for the next set."""
#         return self._get_link("next") is not None
# 
#     def has_previous(self):
#         """Return True if the ObjectList contains an url for the previous set."""
#         return self._get_link("previous") is not None
# 
#     def get_next(self):
#         """Return the next set of objects in an ObjectList."""
#         url = self._get_link("next")
#         resource = self.object_type.get_resource_class(self.client)
#         resp = resource.perform_api_call(resource.REST_READ, url)
#         return ObjectList(resp, self.object_type, self.client)
# 
#     def get_previous(self):
#         """Return the previous set of objects in an ObjectList."""
#         url = self._get_link("previous")
#         resource = self.object_type.get_resource_class(self.client)
#         resp = resource.perform_api_call(resource.REST_READ, url)
#         return ObjectList(resp, self.object_type, self.client)


from ..error import ResponseError, ResponseHandlingError
from ..objects.list import ObjectList
    def update(self, resource_id, data=None, **params):
        path = self.get_resource_name() + "/" + str(resource_id)
        result = self.perform_api_call(self.REST_UPDATE, path, data, params)
        return self.get_resource_object(result)

    def delete(self, resource_id, data=None):
        path = self.get_resource_name() + "/" + str(resource_id)
        return self.perform_api_call(self.REST_DELETE, path, data)

    def list(self, **params):
        path = self.get_resource_name()
        result = self.perform_api_call(self.REST_LIST, path, params=params)
        return ObjectList(result, self.get_resource_object({}).__class__, self.client)

    def perform_api_call(self, http_method, path, data=None, params=None):
        resp = self.client.perform_http_call(http_method, path, data, params)
        if "application/hal+json" in resp.headers.get("Content-Type", ""):
            # set the content type according to the media type definition
            resp.encoding = "utf-8"
        try:
            result = resp.json() if resp.status_code != 204 else {}
        except Exception:
            raise ResponseHandlingError(
                f"Unable to decode Mollie API response (status code: {resp.status_code}): '{resp.text}'."
            )
        if resp.status_code < 200 or resp.status_code > 299:
            if "status" in result and (result["status"] < 200 or result["status"] > 299):
                # the factory will return the appropriate ResponseError subclass based on the result
                raise ResponseError.factory(result)
            else:

# Path: mollie/api/error.py
# class ResponseError(Error):
#     """Errors reported by the API."""
# 
#     status = None
#     field = None
# 
#     def __init__(self, resp=None):
#         message = resp["detail"]
#         super().__init__(message)
#         self.status = resp["status"]
#         if "field" in resp:
#             self.field = resp["field"]
# 
#     @staticmethod
#     def factory(resp):
#         """Return a ResponseError subclass based on the API payload.
# 
#         All errors are documented: https://docs.mollie.com/guides/handling-errors#all-possible-status-codes
#         More exceptions should be added here when appropriate, and when useful examples of API errors are available.
#         """
#         status = resp["status"]
#         if status == 401:
#             return UnauthorizedError(resp)
#         elif status == 404:
#             return NotFoundError(resp)
#         elif status == 422:
#             return UnprocessableEntityError(resp)
#         else:
#             # generic fallback
#             return ResponseError(resp)
# 
# class ResponseHandlingError(Error):
#     """Errors related to handling the response from the API."""
# 
#     pass
# 
# Path: mollie/api/objects/list.py
# class ObjectList(ObjectBase):
#     current = None
# 
#     def __init__(self, result, object_type, client=None):
#         # If an empty dataset was injected, we mock the structure that the remainder of the clas expects.
#         # TODO: it would be better if the ObjectList was initiated with a list of results, rather than with
#         # the full datastructure as it is now, so we can remove all this mucking around with fake data,
#         # mocked result objects, and loads of lengthy accessor workarounds everywhere in the ObjectList.
#         if result == {}:
#             result = {"_embedded": {"unknown": []}, "count": 0}
#             object_type = UnknownObject
# 
#         super().__init__(result, client)
#         self.object_type = object_type
# 
#     def __len__(self):
#         """Return the count field."""
#         return self.count
# 
#     def __iter__(self):
#         """Implement iterator interface."""
#         self.current = None
#         return self
# 
#     def __next__(self):
#         """Implement iterator interface."""
#         if self.current is None:
#             self.current = 0
#         else:
#             self.current += 1
#         try:
#             item = self["_embedded"][self.object_type.get_object_name()][self.current]
#             return self.object_type(item, self.client)
#         except IndexError:
#             self.current = None
#             raise StopIteration
# 
#     def __getitem__(self, key):
#         """Implement Sequence interface."""
#         object_name = self.object_type.get_object_name()
#         if isinstance(key, int):
#             # Return an index-based search from the "_embedded" dataset
#             item = self["_embedded"][object_name][key]
#             return self.object_type(item, self.client)
# 
#         if isinstance(key, slice):
#             _start = key.start or 0
#             _stop = key.stop or self["count"]
#             _step = key.step or 1
#             sliced_data = [self["_embedded"][object_name][x] for x in range(_start, _stop, _step)]
#             # Now we mock a result based on the sliced data
#             sliced_result = {
#                 "_embedded": {
#                     object_name: sliced_data,
#                 },
#                 "count": len(sliced_data),
#             }
#             return ObjectList(sliced_result, self.object_type, self.client)
# 
#         return super().__getitem__(key)
# 
#     @property
#     def count(self):
#         if "count" not in self:
#             return None
#         return int(self["count"])
# 
#     def has_next(self):
#         """Return True if the ObjectList contains an url for the next set."""
#         return self._get_link("next") is not None
# 
#     def has_previous(self):
#         """Return True if the ObjectList contains an url for the previous set."""
#         return self._get_link("previous") is not None
# 
#     def get_next(self):
#         """Return the next set of objects in an ObjectList."""
#         url = self._get_link("next")
#         resource = self.object_type.get_resource_class(self.client)
#         resp = resource.perform_api_call(resource.REST_READ, url)
#         return ObjectList(resp, self.object_type, self.client)
# 
#     def get_previous(self):
#         """Return the previous set of objects in an ObjectList."""
#         url = self._get_link("previous")
#         resource = self.object_type.get_resource_class(self.client)
#         resp = resource.perform_api_call(resource.REST_READ, url)
#         return ObjectList(resp, self.object_type, self.client)


# Path: mollie/api/resources/base.py
from ..error import ResponseError, ResponseHandlingError
from ..objects.list import ObjectList

    def update(self, resource_id, data=None, **params):
        path = self.get_resource_name() + "/" + str(resource_id)
        result = self.perform_api_call(self.REST_UPDATE, path, data, params)
        return self.get_resource_object(result)

    def delete(self, resource_id, data=None):
        path = self.get_resource_name() + "/" + str(resource_id)
        return self.perform_api_call(self.REST_DELETE, path, data)

    def list(self, **params):
        path = self.get_resource_name()
        result = self.perform_api_call(self.REST_LIST, path, params=params)
        return ObjectList(result, self.get_resource_object({}).__class__, self.client)

    def perform_api_call(self, http_method, path, data=None, params=None):
        resp = self.client.perform_http_call(http_method, path, data, params)
        if "application/hal+json" in resp.headers.get("Content-Type", ""):
            # set the content type according to the media type definition
            resp.encoding = "utf-8"
        try:
            result = resp.json() if resp.status_code != 204 else {}
        except Exception:
            raise ResponseHandlingError(
                f"Unable to decode Mollie API response (status code: {resp.status_code}): '{resp.text}'."
            )
        if resp.status_code < 200 or resp.status_code > 299:
            if "status" in result and (result["status"] < 200 or result["status"] > 299):
                # the factory will return the appropriate ResponseError subclass based on the result
                raise ResponseError.factory(result)
            else:

                raise ResponseHandlingError(