Skip to content

Commit

Permalink
Improve typing for archived histories API models
Browse files Browse the repository at this point in the history
Drop Any and use partial model for custom keys.
Consolidate archived histories serialization with regular histories.
  • Loading branch information
davelopez committed Jul 23, 2024
1 parent d3fb3fd commit 0fa0935
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
24 changes: 18 additions & 6 deletions lib/galaxy/schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1890,12 +1890,24 @@ class ArchivedHistoryDetailed(HistoryDetailed, ExportAssociationData):
pass


AnyArchivedHistoryView = Union[
ArchivedHistorySummary,
ArchivedHistoryDetailed,
# Any will cover those cases in which only specific `keys` are requested
# otherwise the validation will fail because the required fields are not returned
Any,
@partial_model()
class CustomArchivedHistoryView(CustomHistoryView, ExportAssociationData):
"""Archived History Response with all optional fields.
It is used for serializing only specific attributes using the "keys"
query parameter.
"""

pass


AnyArchivedHistoryView = Annotated[
Union[
CustomArchivedHistoryView,
ArchivedHistoryDetailed,
ArchivedHistorySummary,
],
Field(union_mode="left_to_right"),
]


Expand Down
2 changes: 2 additions & 0 deletions lib/galaxy/webapps/galaxy/api/histories.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def shared_with_me(
@router.get(
"/api/histories/archived",
summary="Get a list of all archived histories for the current user.",
response_model_exclude_unset=True,
)
def get_archived_histories(
self,
Expand Down Expand Up @@ -613,6 +614,7 @@ def get_custom_builds_metadata(
@router.post(
"/api/histories/{history_id}/archive",
summary="Archive a history.",
response_model_exclude_unset=True,
)
def archive_history(
self,
Expand Down
14 changes: 8 additions & 6 deletions lib/galaxy/webapps/galaxy/services/histories.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,22 +793,24 @@ def get_archived_histories(
filters=filters, order_by=order_by, limit=filter_query_params.limit, offset=filter_query_params.offset
)

histories = [self._serialize_archived_history(trans, history, serialization_params) for history in histories]
histories = [
self._serialize_archived_history(trans, history, serialization_params, default_view="summary")
for history in histories
]
return histories, total_matches

def _serialize_archived_history(
self,
trans: ProvidesHistoryContext,
history: model.History,
serialization_params: Optional[SerializationParams] = None,
default_view: str = "detailed",
):
if serialization_params is None:
serialization_params = SerializationParams(default_view="summary")
archived_history = self.serializer.serialize_to_view(
history, user=trans.user, trans=trans, **serialization_params.dict()
)
serialization_params = SerializationParams()
archived_history = self._serialize_history(trans, history, serialization_params, default_view)
export_record_data = self._get_export_record_data(history)
archived_history["export_record_data"] = export_record_data.dict() if export_record_data else None
archived_history["export_record_data"] = export_record_data.model_dump() if export_record_data else None
return archived_history

def _get_export_record_data(self, history: model.History) -> Optional[WriteStoreToPayload]:
Expand Down

0 comments on commit 0fa0935

Please sign in to comment.