Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_list(self):
with self.assertRaises(MethodNotImplemented):
self.res.list()
def test_delete(self):
with self.assertRaises(MethodNotImplemented):
self.res.delete()
def delete_list(self, *args, **kwargs):
"""
Deletes *ALL* data in the collection for a DELETE on a list-style
endpoint.
Uncommonly implemented due to potential of trashing large datasets.
Implement with care.
**MUST BE OVERRIDDEN BY THE USER** - By default, this returns
``MethodNotImplemented``.
:returns: ``None``
"""
raise MethodNotImplemented()
def update(self, *args, **kwargs):
"""
Updates existing data for a PUT on a detail-style endpoint.
**MUST BE OVERRIDDEN BY THE USER** - By default, this returns
``MethodNotImplemented``.
:returns: May return the updated item or ``None``
"""
raise MethodNotImplemented()
def list(self, *args, **kwargs):
"""
Returns the data for a GET on a list-style endpoint.
**MUST BE OVERRIDDEN BY THE USER** - By default, this returns
``MethodNotImplemented``.
:returns: A collection of data
:rtype: list or iterable
"""
raise MethodNotImplemented()
def get_queryset(self):
return MethodNotImplemented()
def handle(self, endpoint, *args, **kwargs):
"""
almost identical to Resource.handle, except
the way we handle the return value of view_method.
"""
method = self.request_method()
try:
if not method in self.http_methods.get(endpoint, {}):
raise MethodNotImplemented(
"Unsupported method '{}' for {} endpoint.".format(
method,
endpoint
)
)
if not self.is_authenticated():
raise Unauthorized()
self.data = self.deserialize(method, endpoint, self.request_body())
view_method = getattr(self, self.http_methods[endpoint][method])
data = view_method(*args, **kwargs)
if is_future(data):
# need to check if the view_method is a generator or not
data = yield data
serialized = self.serialize(method, endpoint, data)
def delete(self, *args, **kwargs):
"""
Deletes data for a DELETE on a detail-style endpoint.
**MUST BE OVERRIDDEN BY THE USER** - By default, this returns
``MethodNotImplemented``.
:returns: ``None``
"""
raise MethodNotImplemented()
def update_list(self, *args, **kwargs):
"""
Updates the entire collection for a PUT on a list-style endpoint.
Uncommonly implemented due to the complexity & (varying) busines-logic
involved.
**MUST BE OVERRIDDEN BY THE USER** - By default, this returns
``MethodNotImplemented``.
:returns: A collection of data
:rtype: list or iterable
"""
raise MethodNotImplemented()
:param args: (Optional) Any positional URI parameter data is passed
along here. Somewhat framework/URL-specific.
:param kwargs: (Optional) Any keyword/named URI parameter data is
passed along here. Somewhat framework/URL-specific.
:returns: A response object
"""
self.endpoint = endpoint
method = self.request_method()
try:
# Use ``.get()`` so we can also dodge potentially incorrect
# ``endpoint`` errors as well.
if not method in self.http_methods.get(endpoint, {}):
raise MethodNotImplemented(
"Unsupported method '{}' for {} endpoint.".format(
method,
endpoint
)
)
if not self.is_authenticated():
raise Unauthorized()
self.data = self.deserialize(method, endpoint, self.request_body())
view_method = getattr(self, self.http_methods[endpoint][method])
data = view_method(*args, **kwargs)
serialized = self.serialize(method, endpoint, data)
except Exception as err:
return self.handle_error(err)