Skip to content

Commit

Permalink
Merge pull request #80 from ecmwf-projects/COPDS-1806-messages-by-site
Browse files Browse the repository at this point in the history
messages: portal to site header
  • Loading branch information
keul authored Aug 28, 2024
2 parents d86702e + a2b43dd commit cdc1d7a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
1 change: 1 addition & 0 deletions cads_catalogue_api_service/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
MAX_LIMIT = 50

PORTAL_HEADER_NAME = "X-CADS-Portal"
SITE_HEADER_NAME = "X-CADS-Site"


class SqlalchemySettings(stac_fastapi.types.config.ApiSettings): # type: ignore
Expand Down
7 changes: 7 additions & 0 deletions cads_catalogue_api_service/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ def get_portals(
"""Fastapi dependency that provides the CADS portal profile."""
portals = get_portals_values(portal)
return portals


def get_site(
site=fastapi.Header(default=None, alias=config.SITE_HEADER_NAME),
) -> list[str] | None:
"""Fastapi dependency that provides the CADS portal profile."""
return site
25 changes: 8 additions & 17 deletions cads_catalogue_api_service/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def query_messages(
live: bool = True,
is_global: bool = True,
collection_id: str | None = None,
portals: list[str] | None = None,
site: str | None = None,
) -> list[cads_catalogue.database.Message]:
"""Query messages."""
results = (
Expand All @@ -49,7 +49,7 @@ def query_messages(
cads_catalogue.database.ResourceMessage,
isouter=True,
)
# FIXEME: this can be slow. Please do not load the full dataset.
# FIXME: this can be slow. Please do not load the full dataset.
.join(
cads_catalogue.database.Resource,
full=True,
Expand All @@ -59,13 +59,8 @@ def query_messages(
cads_catalogue.database.Message.is_global.is_(is_global),
)
)
if portals and is_global:
results = results.where(
sa.or_(
cads_catalogue.database.Message.portal.in_(portals),
cads_catalogue.database.Message.portal.is_(None),
),
)
if site and is_global:
results = results.where(cads_catalogue.database.Message.site == site)
if collection_id:
results = results.where(
cads_catalogue.database.Resource.resource_uid == collection_id
Expand Down Expand Up @@ -133,12 +128,10 @@ def list_changelog_by_id(
@router.get("/messages", response_model=models.Messages)
def list_messages(
session=fastapi.Depends(dependencies.get_session),
portals: list[str] | None = fastapi.Depends(dependencies.get_portals),
site: str | None = fastapi.Depends(dependencies.get_site),
) -> models.Messages:
"""Endpoint to get all messages."""
results = query_messages(
session=session, live=True, is_global=True, portals=portals
)
results = query_messages(session=session, live=True, is_global=True, site=site)
return models.Messages(
messages=[
models.Message(
Expand All @@ -158,12 +151,10 @@ def list_messages(
@router.get("/messages/changelog", response_model=models.Changelog)
def list_changelog(
session=fastapi.Depends(dependencies.get_session),
portals: list[str] | None = fastapi.Depends(dependencies.get_portals),
site: str | None = fastapi.Depends(dependencies.get_site),
) -> models.Changelog:
"""Endpoint to get all changelog."""
results = query_messages(
session=session, live=False, is_global=True, portals=portals
)
results = query_messages(session=session, live=False, is_global=True, site=site)
return models.Changelog(
changelog=[
models.Message(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_30_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def static_messages_query(
live: bool = True,
is_global: bool = True,
collection_id: str | None = None,
portals: list[str] | None = None,
site: str | None = None,
) -> list[cads_catalogue.database.Message]:
return [
Message(
Expand Down Expand Up @@ -78,7 +78,7 @@ def static_changelog_query(
live: bool = False,
is_global: bool = True,
collection_id: str | None = None,
portals: list[str] | None = None,
site: str | None = None,
) -> list[cads_catalogue.database.Message]:
return [
Message(
Expand Down

0 comments on commit cdc1d7a

Please sign in to comment.