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

Configure query on request #1073

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
13 changes: 9 additions & 4 deletions emmet-api/emmet/api/core/global_header.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from maggma.api.resource.core import HeaderProcessor
from fastapi import Response, Request
from maggma.api.query_operator import QueryOperator
from maggma.api.utils import STORE_PARAMS
from emmet.api.routes.materials.materials.query_operators import LicenseQuery


class GlobalHeaderProcessor(HeaderProcessor):
Expand All @@ -16,6 +17,10 @@ def process_header(self, response: Response, request: Request):
response.headers["X-Consumer-Id"] = consumer_id

def configure_query_on_request(
self, request: Request, query_operator: QueryOperator
):
pass
self, request: Request, query_operator: LicenseQuery
) -> STORE_PARAMS:
groups = request.headers.get("x-consumer-groups", None)
privileged_groups = {"TERMS:ACCEPT-NC", "admin"}
is_privileged = groups and any(group in groups for group in privileged_groups)
license_type = "All" if is_privileged else None
return query_operator.query(license=license_type)
20 changes: 17 additions & 3 deletions emmet-api/emmet/api/routes/materials/materials/query_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,25 @@ class LicenseQuery(QueryOperator):
license information in builder meta
"""

# def query(
# self,
# license: Optional[Literal["BY-C", "BY-NC"]] = Query(
# "BY-C",
# description="Query by license. Either commercial or non-commercial CC-BY",
# ),
# ) -> STORE_PARAMS:
# return {"criteria": {"builder_meta.license": license}}

def query(
self,
license: Optional[Literal["BY-C", "BY-NC"]] = Query(
license: Optional[Literal["BY-C", "BY-NC", "All"]] = Query(
"BY-C",
description="Query by license. Either commercial or non-commercial CC-BY",
description="Query by license. Can be commercial or non-commercial, or both",
),
) -> STORE_PARAMS:
return {"criteria": {"builder_meta.license": license}}
criteria = (
{"builder_meta.license": {"$in": ["BY-C", "BY-NC"]}}
if license == "All"
else {"builder_meta.license": license}
)
return {"criteria": criteria}
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ def materials_resource(materials_store):
LicenseQuery(),
],
header_processor=GlobalHeaderProcessor(),
query_to_configure_on_request=LicenseQuery(),
tags=["Materials"],
sub_path="/core/",
disable_validation=True,
timeout=MAPISettings().TIMEOUT, # type: ignore
)

return resource
1 change: 1 addition & 0 deletions emmet-api/emmet/api/routes/materials/summary/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def summary_resource(summary_store):
],
hint_scheme=SummaryHintScheme(),
header_processor=GlobalHeaderProcessor(),
# query_to_configure_on_request=LicenseQuery(),
tags=["Materials Summary"],
sub_path="/summary/",
disable_validation=True,
Expand Down
Loading