Skip to content

Commit 37fb5e5

Browse files
moves logic to pull info from env vars out of 'VlmResponse' classes and into 'build_vlm_response_from_caf_data'
1 parent a8ad18c commit 37fb5e5

File tree

2 files changed

+58
-55
lines changed

2 files changed

+58
-55
lines changed
Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
"""Craft a VlmResponse object from a list of CohortAlleleFrequencyStudyResults"""
22

3+
import os
4+
35
from ga4gh.va_spec.base.core import CohortAlleleFrequencyStudyResult
46

57
from anyvlm.schemas.vlm import (
8+
BeaconHandover,
9+
HandoverType,
10+
ResponseField,
11+
ResponseSummary,
612
VlmResponse,
713
)
814

915

16+
class MissingEnvironmentVariableError(Exception):
17+
"""Raised when a required environment variable is not set."""
18+
19+
20+
def _get_environment_var(key: str) -> str:
21+
"""Retrieves an environment variable, raising an error if it is not set.
22+
23+
:param key: The key for the environment variable
24+
:returns: The value for the environment variable of the provided `key`
25+
:raises: MissingEnvironmentVariableError if environment variable is not found.
26+
"""
27+
value: str | None = os.environ.get(key)
28+
if not value:
29+
message = f"Missing required environment variable: {key}"
30+
raise MissingEnvironmentVariableError(message)
31+
return value
32+
33+
1034
def build_vlm_response_from_caf_data(
1135
caf_data: list[CohortAlleleFrequencyStudyResult],
1236
) -> VlmResponse:
@@ -15,4 +39,31 @@ def build_vlm_response_from_caf_data(
1539
:param caf_data: A list of `CohortAlleleFrequencyStudyResult` objects that will be used to build the VlmResponse
1640
:return: A `VlmResponse` object.
1741
"""
18-
raise NotImplementedError # TODO: Implement this during/after Issue #16
42+
raise NotImplementedError # TODO: Remove this and finish implementing this function in Issue #35
43+
44+
# TODO - create `handover_type` and `beacon_handovers` dynamically,
45+
# instead of pulling from environment variables. See Issue #37.
46+
handover_type = HandoverType(
47+
id=_get_environment_var("HANDOVER_TYPE_ID"),
48+
label=_get_environment_var("HANDOVER_TYPE_LABEL"),
49+
)
50+
51+
beacon_handovers: list[BeaconHandover] = [
52+
BeaconHandover(
53+
handoverType=handover_type, url=_get_environment_var("BEACON_HANDOVER_URL")
54+
)
55+
]
56+
57+
num_results = len(caf_data)
58+
response_summary = ResponseSummary(
59+
exists=num_results > 0, numTotalResults=num_results
60+
)
61+
62+
# TODO - create this field in Issue #35
63+
response_field = ResponseField()
64+
65+
return VlmResponse(
66+
beaconHandovers=beacon_handovers,
67+
responseSummary=response_summary,
68+
response=response_field,
69+
)

src/anyvlm/schemas/vlm.py

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,65 +12,21 @@
1212
RESULT_ENTITY_TYPE = "genomicVariant"
1313

1414

15-
class HandoverSettings(BaseSettings):
16-
"""Settings for 'HandoverType' class"""
17-
18-
id: str
19-
label: str
20-
21-
model_config = SettingsConfigDict(
22-
env_prefix="HANDOVER_TYPE_",
23-
extra="ignore",
24-
)
25-
26-
27-
handover_type_settings = HandoverSettings() # type: ignore
28-
29-
3015
class HandoverType(BaseModel):
3116
"""The type of handover the parent `BeaconHandover` represents."""
3217

33-
id: str = Field(
34-
"",
35-
description="Node-specific identifier",
36-
frozen=True,
37-
)
18+
id: str = Field(default="gregor", description="Node-specific identifier")
3819
label: str = Field(
39-
"",
40-
description="Node-specific label",
41-
frozen=True,
42-
)
43-
44-
model_config = ConfigDict(
45-
extra="forbid",
46-
)
47-
48-
# custom __init__ to prevent overriding attributes that are static/set via environment variables
49-
def __init__(self) -> None:
50-
super().__init__(
51-
id=handover_type_settings.id,
52-
label=handover_type_settings.label,
53-
)
54-
55-
56-
class BeaconHandoverSettings(BaseSettings):
57-
"""Settings for 'BeaconHandover' class"""
58-
59-
url: str
60-
61-
model_config = SettingsConfigDict(
62-
env_prefix="BEACON_HANDOVER_",
63-
extra="ignore",
20+
description="Node-specific identifier",
6421
)
6522

6623

67-
beacon_handover_settings = BeaconHandoverSettings() # type: ignore
68-
69-
7024
class BeaconHandover(BaseModel):
7125
"""Describes how users can get more information about the results provided in the parent `VlmResponse`"""
7226

73-
handoverType: HandoverType = Field(default=HandoverType())
27+
handoverType: HandoverType = Field(
28+
..., description="The type of handover this represents"
29+
)
7430
url: str = Field(
7531
"",
7632
description="""
@@ -79,10 +35,6 @@ class BeaconHandover(BaseModel):
7935
""",
8036
)
8137

82-
# custom __init__ to prevent overriding attributes that are static/set via environment variables
83-
def __init__(self) -> None:
84-
super().__init__(url=beacon_handover_settings.url)
85-
8638

8739
class ReturnedSchema(BaseModel):
8840
"""Fixed [Beacon Schema](https://github.com/ga4gh-beacon/beacon-v2/blob/c6558bf2e6494df3905f7b2df66e903dfe509500/framework/json/common/beaconCommonComponents.json#L241)"""
@@ -190,7 +142,7 @@ class ResponseField(BaseModel):
190142
class VlmResponse(BaseModel):
191143
"""Define response structure for the variant_counts endpoint."""
192144

193-
beaconHandovers: list[BeaconHandover] = [BeaconHandover()]
145+
beaconHandovers: list[BeaconHandover]
194146
meta: Meta = Meta()
195147
responseSummary: ResponseSummary
196148
response: ResponseField

0 commit comments

Comments
 (0)