|
| 1 | +from abc import ABC |
| 2 | +from abc import abstractmethod |
| 3 | +from dataclasses import dataclass |
| 4 | +from dataclasses import field |
| 5 | +from enum import Enum |
| 6 | +from typing import Optional |
| 7 | + |
| 8 | +from datacommons_client.utils.error_handling import ( |
| 9 | + InvalidObservationSelectError, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class EndpointRequestPayload(ABC): |
| 15 | + """ |
| 16 | + Abstract base class for payload dataclasses. |
| 17 | + Defines the required interface for all payload dataclasses. |
| 18 | + """ |
| 19 | + |
| 20 | + @abstractmethod |
| 21 | + def normalize(self) -> None: |
| 22 | + """Normalize the payload for consistent internal representation.""" |
| 23 | + pass |
| 24 | + |
| 25 | + @abstractmethod |
| 26 | + def validate(self) -> None: |
| 27 | + """Validate the payload to ensure its structure and contents are correct.""" |
| 28 | + pass |
| 29 | + |
| 30 | + @property |
| 31 | + @abstractmethod |
| 32 | + def to_dict(self) -> dict: |
| 33 | + """Convert the payload into a dictionary format for API requests.""" |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +@dataclass |
| 38 | +class NodeRequestPayload(EndpointRequestPayload): |
| 39 | + """ |
| 40 | + A dataclass to structure, normalize, and validate the payload for a Node V2 API request. |
| 41 | +
|
| 42 | + Attributes: |
| 43 | + node_dcids (str | list[str]): The DCID(s) of the nodes to query. |
| 44 | + expression (str): The property or relation expression(s) to query. |
| 45 | + """ |
| 46 | + |
| 47 | + node_dcids: str | list[str] |
| 48 | + expression: str |
| 49 | + |
| 50 | + def __post_init__(self): |
| 51 | + self.normalize() |
| 52 | + self.validate() |
| 53 | + |
| 54 | + def normalize(self): |
| 55 | + if isinstance(self.node_dcids, str): |
| 56 | + self.node_dcids = [self.node_dcids] |
| 57 | + |
| 58 | + def validate(self): |
| 59 | + if not isinstance(self.expression, str): |
| 60 | + raise ValueError("Expression must be a string.") |
| 61 | + |
| 62 | + @property |
| 63 | + def to_dict(self) -> dict: |
| 64 | + return {"nodes": self.node_dcids, "property": self.expression} |
| 65 | + |
| 66 | + |
| 67 | +class ObservationSelect(str, Enum): |
| 68 | + DATE = "date" |
| 69 | + VARIABLE = "variable" |
| 70 | + ENTITY = "entity" |
| 71 | + VALUE = "value" |
| 72 | + |
| 73 | + @classmethod |
| 74 | + def _missing_(cls, value): |
| 75 | + """Handle missing enum values by raising a custom error.""" |
| 76 | + valid_values = [member.value for member in cls] |
| 77 | + message = f"Invalid `select` field: '{value}'. Only {', '.join(valid_values)} are allowed." |
| 78 | + raise InvalidObservationSelectError(message=message) |
| 79 | + |
| 80 | + |
| 81 | +class ObservationDate(str, Enum): |
| 82 | + LATEST = "LATEST" |
| 83 | + ALL = "" |
| 84 | + |
| 85 | + |
| 86 | +@dataclass |
| 87 | +class ObservationRequestPayload(EndpointRequestPayload): |
| 88 | + """ |
| 89 | + A dataclass to structure, normalize, and validate the payload for an Observation V2 API request. |
| 90 | +
|
| 91 | + Attributes: |
| 92 | + date (str): The date for which data is being requested. |
| 93 | + variable_dcids (str | list[str]): One or more variable IDs for the data. |
| 94 | + select (list[ObservationSelect]): Fields to include in the response. |
| 95 | + Defaults to ["date", "variable", "entity", "value"]. |
| 96 | + entity_dcids (Optional[str | list[str]]): One or more entity IDs to filter the data. |
| 97 | + entity_expression (Optional[str]): A string expression to filter entities. |
| 98 | + """ |
| 99 | + |
| 100 | + date: ObservationDate | str = "" |
| 101 | + variable_dcids: str | list[str] = field(default_factory=list) |
| 102 | + select: list[ObservationSelect | str] = field( |
| 103 | + default_factory=lambda: [ |
| 104 | + ObservationSelect.DATE, |
| 105 | + ObservationSelect.VARIABLE, |
| 106 | + ObservationSelect.ENTITY, |
| 107 | + ObservationSelect.VALUE, |
| 108 | + ] |
| 109 | + ) |
| 110 | + entity_dcids: Optional[str | list[str]] = None |
| 111 | + entity_expression: Optional[str] = None |
| 112 | + |
| 113 | + def __post_init__(self): |
| 114 | + """ |
| 115 | + Initializes the payload, performing validation and normalization. |
| 116 | +
|
| 117 | + Raises: |
| 118 | + ValueError: If validation rules are violated. |
| 119 | + """ |
| 120 | + self.RequiredSelect = {"variable", "entity"} |
| 121 | + self.normalize() |
| 122 | + self.validate() |
| 123 | + |
| 124 | + def normalize(self): |
| 125 | + """ |
| 126 | + Normalizes the payload for consistent internal representation. |
| 127 | +
|
| 128 | + - Converts `variable_dcids` and `entity_dcids` to lists if they are passed as strings. |
| 129 | + - Normalizes the `date` field to ensure it is in the correct format. |
| 130 | + """ |
| 131 | + # Normalize variable |
| 132 | + if isinstance(self.variable_dcids, str): |
| 133 | + self.variable_dcids = [self.variable_dcids] |
| 134 | + |
| 135 | + # Normalize entity |
| 136 | + if isinstance(self.entity_dcids, str): |
| 137 | + self.entity_dcids = [self.entity_dcids] |
| 138 | + |
| 139 | + # Normalize date field |
| 140 | + if self.date.upper() == "ALL": |
| 141 | + self.date = ObservationDate.ALL |
| 142 | + elif (self.date.upper() == "LATEST") or (self.date == ""): |
| 143 | + self.date = ObservationDate.LATEST |
| 144 | + |
| 145 | + def validate(self): |
| 146 | + """ |
| 147 | + Validates the payload to ensure consistency and correctness. |
| 148 | +
|
| 149 | + Raises: |
| 150 | + ValueError: If both `entity_dcids` and `entity_expression` are set, |
| 151 | + if neither is set, or if required fields are missing from `select`. |
| 152 | + """ |
| 153 | + |
| 154 | + # Validate mutually exclusive entity fields |
| 155 | + if bool(self.entity_dcids) == bool(self.entity_expression): |
| 156 | + raise ValueError( |
| 157 | + "Exactly one of 'entity_dcids' or 'entity_expression' must be set." |
| 158 | + ) |
| 159 | + |
| 160 | + # Check if all required fields are present |
| 161 | + missing_fields = self.RequiredSelect - set(self.select) |
| 162 | + if missing_fields: |
| 163 | + raise ValueError( |
| 164 | + f"The 'select' field must include at least the following: {', '.join(self.RequiredSelect)} " |
| 165 | + f"(missing: {', '.join(missing_fields)})" |
| 166 | + ) |
| 167 | + |
| 168 | + # Check all select fields are valid |
| 169 | + [ObservationSelect(select_field) for select_field in self.select] |
| 170 | + |
| 171 | + @property |
| 172 | + def to_dict(self) -> dict: |
| 173 | + """ |
| 174 | + Converts the payload into a dictionary format for API requests. |
| 175 | +
|
| 176 | + Returns: |
| 177 | + dict: The normalized and validated payload. |
| 178 | + """ |
| 179 | + return { |
| 180 | + "date": self.date, |
| 181 | + "variable": {"dcids": self.variable_dcids}, |
| 182 | + "entity": ( |
| 183 | + {"dcids": self.entity_dcids} |
| 184 | + if self.entity_dcids |
| 185 | + else {"expression": self.entity_expression} |
| 186 | + ), |
| 187 | + "select": self.select, |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | +@dataclass |
| 192 | +class ResolveRequestPayload(EndpointRequestPayload): |
| 193 | + """ |
| 194 | + A dataclass to structure, normalize, and validate the payload for a Resolve V2 API request. |
| 195 | +
|
| 196 | + Attributes: |
| 197 | + node_dcids (str | list[str]): The DCID(s) of the nodes to query. |
| 198 | + expression (str): The relation expression to query. |
| 199 | + """ |
| 200 | + |
| 201 | + node_dcids: str | list[str] |
| 202 | + expression: str |
| 203 | + |
| 204 | + def __post_init__(self): |
| 205 | + self.normalize() |
| 206 | + self.validate() |
| 207 | + |
| 208 | + def normalize(self): |
| 209 | + if isinstance(self.node_dcids, str): |
| 210 | + self.node_dcids = [self.node_dcids] |
| 211 | + |
| 212 | + def validate(self): |
| 213 | + if not isinstance(self.expression, str): |
| 214 | + raise ValueError("Expression must be a string.") |
| 215 | + |
| 216 | + @property |
| 217 | + def to_dict(self) -> dict: |
| 218 | + return {"nodes": self.node_dcids, "property": self.expression} |
0 commit comments