Skip to content

Commit

Permalink
#136: Add framework for PATCH entity by ID endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Jul 16, 2020
1 parent 0b0bc20 commit 9bfefcf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions common/python_icat_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from common.backend import Backend
from common.helpers import queries_records
from common.python_icat_helpers import requires_session_id, get_session_details_helper, logout_icat_client, \
refresh_client_session, get_entity_by_id
refresh_client_session, get_entity_by_id, update_entity_by_id
from common.config import config
from common.exceptions import AuthenticationError
from common.models.db_models import SESSION
Expand Down Expand Up @@ -90,7 +90,7 @@ def delete_with_id(self, session_id, table, id):
@requires_session_id
@queries_records
def update_with_id(self, session_id, table, id, data):
pass
return update_entity_by_id(self.client, table.__name__, id, data)

@requires_session_id
@queries_records
Expand Down
20 changes: 13 additions & 7 deletions common/python_icat_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,20 @@ def get_entity_by_id(client, table, id):
for entity_name in entity_names:
lowercase_name = entity_name.lower()

if lowercase_name == lowercase_table_name:
selected_entity = entity_name
def update_entity_by_id(client, table_name, id, new_data):
"""
Updates certain attributes Gets a record of a given ID of the specified entity
# Raise a 400 if a valid entity cannot be found
if selected_entity is None:
raise BadRequestError(f"Bad request made, cannot find {table.__name__} entity within Python ICAT")
:param client: ICAT client containing an authenticated user
:type client: :class:`icat.client.Client`
:param table_name: Table name to extract which entity to use
:type table_name: TODO
:param id: ID number of the entity to retrieve
:type id: :class:`int`
:param new_data: JSON from request body providing new data to update the record with the
specified ID
"""

id_query = construct_icat_query(client, selected_entity, id_condition)
entity_by_id_data = execute_icat_query(client, id_query)
pass

return entity_by_id_data
2 changes: 1 addition & 1 deletion src/resources/entities/entity_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def delete(self, id):
def patch(self, id):
session_id = get_session_id_from_auth_header()
backend.update_with_id(session_id, table, id, request.json)
return backend.get_with_id(session_id, table, id).to_dict(), 200
return backend.get_with_id(session_id, table, id), 200

patch.__doc__ = f"""
---
Expand Down

0 comments on commit 9bfefcf

Please sign in to comment.