hydroshare / hs_restclient

Python client for the https://www.hydroshare.org REST API
BSD 3-Clause "New" or "Revised" License
6 stars 8 forks source link

Deleting Resource Causes Exception #111

Closed rajadain closed 5 years ago

rajadain commented 5 years ago

Previously, this code used to work:

# DELETE existing resource removes it from MMW and HydroShare
if hsresource and request.method == 'DELETE':
    if hs.check_resource_exists(hsresource.resource):
        hs.deleteResource(hsresource.resource)
    hsresource.delete()
    return Response(status=status.HTTP_204_NO_CONTENT)

It now fails with the following error message:

Internal Server Error: /export/hydroshare
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/decorators.py", line 55, in handler
    return func(*args, **kwargs)
  File "/opt/app/apps/export/views.py", line 95, in hydroshare
    hs.deleteResource(hsresource.resource)
  File "/usr/local/lib/python2.7/dist-packages/hs_restclient/__init__.py", line 791, in deleteResource
    raise HydroShareHTTPException((url, 'DELETE', r.status_code))
  File "/usr/local/lib/python2.7/dist-packages/hs_restclient/exceptions.py", line 66, in __init__
    self.status_msg = args[3]
IndexError: tuple index out of range

This is because the HydroShare API recently switched from sending back a 200 OK to a 204 NO CONTENT https://github.com/hydroshare/hydroshare/commit/c99b8423c38b3dce020e4a3a03e9c55ab0201b7d#diff-69f87776b87c2b6db0a596870ce8c6d1R155.

To fix this, the deleteResource method should be updated to handle 204s in addition to 200s:

https://github.com/hydroshare/hs_restclient/blob/9cd106238b512e01ecd3e33425fe48c13b7f63d5/hs_restclient/__init__.py#L784-L791

sblack-usu commented 5 years ago

This is on me, I updated the response but did not update the restclient to look for the updated code. You've done all the investigative work, so I'll turn this around pretty quickly. Do you need a release pushed to pip right away or are you comfortable installing straight from github? I've got some more work I've got to implement in the rest_client that'll take a little more time, but I can do a patch release if you need it.

rajadain commented 5 years ago

If you expect a release in the next week or so, that's alright. We're doing some tech debt cleanup on MMW, discovered this while exercising the app. I'll make a follow up card on our side to update the hs_restclient version once this has been released.

On a side note: we're currently on 1.2.10. Is there a changelog, or any breaking changes since that version?

sblack-usu commented 5 years ago

There shouldn't be any breaking features, I try not to do that because it generates more work for me. There are several additions, largely just adding support for new endpoints.

https://github.com/hydroshare/hs_restclient/blob/develop/RELEASE_NOTES.md

sblack-usu commented 5 years ago

I can turn around a release next week. I'll keep you posted.

sblack-usu commented 5 years ago

@rajadain - I just pushed out 1.3.4 with this fix. Please let me know if everything works as expected.

rajadain commented 5 years ago

Thanks @sblack-usu, this was upgraded and verified in https://github.com/WikiWatershed/model-my-watershed/pull/3134. This issue can now be closed.