Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ ⚡️Change PUT to PATCH in all resource APIs and minor updates #173

Merged
merged 11 commits into from
Mar 15, 2018

Conversation

znatty22
Copy link
Member

@znatty22 znatty22 commented Mar 7, 2018

For participant, demographic, diagnosis, and sample - change the PUT http method in the resource class to PATCH http method.

  • Previous PUT implementation was actually a PATCH implementation. PATCH allows for partial update of resources. PUT replaces the entire resource and will be implemented in the near future.

For participant, demographic, diagnosis, and sample - change method of retrieval of resource to use SQLAlchemy get() which is optimized for retrieval by primary key.

@znatty22 znatty22 added feature New functionality refactor Something needs to be done better labels Mar 7, 2018
@znatty22 znatty22 added this to the CHOP Sprint 4 milestone Mar 7, 2018
@znatty22 znatty22 changed the title 🚧WIP: Change PUT to PATCH in all resource APIs 🚧 WIP: Change PUT to PATCH in all resource APIs Mar 7, 2018
@znatty22 znatty22 force-pushed the put-to-patch branch 2 times, most recently from 6ac6f13 to d5ccc4b Compare March 9, 2018 17:50
@znatty22 znatty22 mentioned this pull request Mar 9, 2018
@znatty22 znatty22 force-pushed the put-to-patch branch 4 times, most recently from 4e78d32 to c7b7950 Compare March 12, 2018 19:53
@znatty22 znatty22 changed the title 🚧 WIP: Change PUT to PATCH in all resource APIs ✨ ⚡️Change PUT to PATCH in all resource APIs and minor updates Mar 12, 2018
for k in unchanged_keys:
val = getattr(dm, k)
if isinstance(val, datetime):
d = val.replace(tzinfo=tz.tzutc())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this add the timezone to the dt or does it change an existing one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It adds a timezone to val since val doesn't have one and then returns a new datetime obj.

https://stackoverflow.com/questions/7065164/how-to-make-an-unaware-datetime-timezone-aware-in-python

@@ -369,3 +334,12 @@ def _create_save_to_db(self):
kwargs['kf_id'] = d.kf_id

return kwargs

def _test_response_content(self, resp, status_code):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we really want this, it could be part of the base test class. However, it's been removed from all the other tests and replaced by the general api tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mkay 👍

@pytest.mark.parametrize('endpoint,field', [
('/participants', 'blah'),
('/samples', 'blah')
@pytest.mark.parametrize('endpoint, method, field', [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also double parametrize things to reduce duplication. We could probably just use 'blah' for all the tests too. I think something like this will work:

@pytest.mark.parametrize('method', ['POST', 'PATCH'])
@pytest.mark.parametrize('endopint,', ['/participants', '/demographics' ... ])
def test_unkown_field(self, client, entities, endpoint, method):
    pass

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mkay 👍

@@ -119,6 +119,7 @@ def register_error_handlers(app):
from dataservice.api import errors
app.register_error_handler(HTTPException, errors.http_error)
app.register_error_handler(IntegrityError, errors.integrity_error)
app.register_error_handler(405, errors.http_error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when does this happen?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you try sending a PUT request (or any other HTTP method we don't support). It looks like
app.register_error_handler(HTTPException, errors.http_error) isn't catching all http exceptions. Maybe we need to investigate that further

Copy link
Member Author

@znatty22 znatty22 Mar 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to work though?

from werkzeug.exceptions import default_exceptions
for ex in default_exceptions:
        app.register_error_handler(ex, errors.http_error)

We could probably remove the other explicit HTTP error registrations (i.e. 400, 404, etc) if we use this.

@dankolbman I vaguely remember you said you tried this or something similar and it didn't work or you had issues with it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it has something to do with the scoping on the exception. I forget what exactly I changed to make it work, but it had to do with where the HTTPException was imported.

@@ -83,26 +82,21 @@ def get(self, kf_id):
resource:
Demographic
"""

# Get all
if kf_id is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic was supposed to be removed when we changed to the two-view method.

Copy link
Contributor

@parimalak parimalak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

app.register_error_handler(400, errors.http_error)
from werkzeug.exceptions import default_exceptions
for ex in default_exceptions:
app.register_error_handler(ex, errors.http_error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No luck with HTTPException? It looks like default_exceptions is just all the subclasses of HTTPException, so you would think it would work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have to go with this approach for now. Doesn't look like the fix for the issue with HTTPException is in Flask 0.12. Its slated for Flask milestone 1.0 which isn't complete yet

pallets/flask#2314

@@ -12,6 +14,8 @@
DIAGNOSES_URL = 'api.diagnoses'
DIAGNOSES_LIST_URL = 'api.diagnoses_list'

from pprint import pprint
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused?

Copy link
Contributor

@dankolbman dankolbman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@znatty22 znatty22 merged commit 4e7876d into master Mar 15, 2018
@znatty22 znatty22 deleted the put-to-patch branch March 16, 2018 14:15
@dankolbman dankolbman mentioned this pull request Apr 2, 2018
@dankolbman dankolbman removed the ready-for-review This PR needs a review label May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality refactor Something needs to be done better
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants