|
| 1 | +# (C) 2024 GoodData Corporation |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import attrs |
| 7 | +from gooddata_api_client.model.json_api_parameter_in import JsonApiParameterIn |
| 8 | +from gooddata_api_client.model.json_api_parameter_in_document import JsonApiParameterInDocument |
| 9 | +from gooddata_api_client.model.json_api_parameter_patch import JsonApiParameterPatch |
| 10 | +from gooddata_api_client.model.json_api_parameter_patch_document import JsonApiParameterPatchDocument |
| 11 | +from gooddata_api_client.model.json_api_parameter_post_optional_id import JsonApiParameterPostOptionalId |
| 12 | +from gooddata_api_client.model.json_api_parameter_post_optional_id_document import ( |
| 13 | + JsonApiParameterPostOptionalIdDocument, |
| 14 | +) |
| 15 | +from gooddata_api_client.model.number_constraints import NumberConstraints |
| 16 | +from gooddata_api_client.model.number_parameter_definition import NumberParameterDefinition |
| 17 | + |
| 18 | +from gooddata_sdk.catalog.base import Base |
| 19 | + |
| 20 | + |
| 21 | +@attrs.define(kw_only=True) |
| 22 | +class CatalogWorkspaceParameterConstraints(Base): |
| 23 | + """Constraints for a number parameter.""" |
| 24 | + |
| 25 | + min: float | None = None |
| 26 | + max: float | None = None |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def client_class() -> type[NumberConstraints]: |
| 30 | + return NumberConstraints |
| 31 | + |
| 32 | + def as_api_model(self) -> NumberConstraints: |
| 33 | + kwargs: dict[str, Any] = {} |
| 34 | + if self.min is not None: |
| 35 | + kwargs["min"] = self.min |
| 36 | + if self.max is not None: |
| 37 | + kwargs["max"] = self.max |
| 38 | + return NumberConstraints(_check_type=False, **kwargs) |
| 39 | + |
| 40 | + |
| 41 | +@attrs.define(kw_only=True) |
| 42 | +class CatalogNumberParameterDefinition(Base): |
| 43 | + """Definition of a number-typed parameter.""" |
| 44 | + |
| 45 | + default_value: float |
| 46 | + type: str = "NUMBER" |
| 47 | + constraints: CatalogWorkspaceParameterConstraints | None = None |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def client_class() -> type[NumberParameterDefinition]: |
| 51 | + return NumberParameterDefinition |
| 52 | + |
| 53 | + def as_api_model(self) -> NumberParameterDefinition: |
| 54 | + kwargs: dict[str, Any] = {} |
| 55 | + if self.constraints is not None: |
| 56 | + kwargs["constraints"] = self.constraints.as_api_model() |
| 57 | + return NumberParameterDefinition( |
| 58 | + default_value=self.default_value, |
| 59 | + _check_type=False, |
| 60 | + **kwargs, |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +@attrs.define(kw_only=True) |
| 65 | +class CatalogWorkspaceParameter(Base): |
| 66 | + """A workspace-scoped parameter entity.""" |
| 67 | + |
| 68 | + id: str | None = None |
| 69 | + definition: CatalogNumberParameterDefinition | None = None |
| 70 | + title: str | None = None |
| 71 | + description: str | None = None |
| 72 | + tags: list[str] = attrs.field(factory=list) |
| 73 | + |
| 74 | + @staticmethod |
| 75 | + def client_class() -> type[JsonApiParameterIn]: |
| 76 | + return JsonApiParameterIn |
| 77 | + |
| 78 | + @classmethod |
| 79 | + def init( |
| 80 | + cls, |
| 81 | + *, |
| 82 | + parameter_id: str, |
| 83 | + default_value: float, |
| 84 | + title: str | None = None, |
| 85 | + description: str | None = None, |
| 86 | + tags: list[str] | None = None, |
| 87 | + constraints: CatalogWorkspaceParameterConstraints | None = None, |
| 88 | + ) -> CatalogWorkspaceParameter: |
| 89 | + definition = CatalogNumberParameterDefinition( |
| 90 | + default_value=default_value, |
| 91 | + constraints=constraints, |
| 92 | + ) |
| 93 | + return cls( |
| 94 | + id=parameter_id, |
| 95 | + definition=definition, |
| 96 | + title=title, |
| 97 | + description=description, |
| 98 | + tags=tags or [], |
| 99 | + ) |
| 100 | + |
| 101 | + @classmethod |
| 102 | + def from_api(cls, entity: Any) -> CatalogWorkspaceParameter: |
| 103 | + """Deserialize from an API model object or a snake_case dict. |
| 104 | +
|
| 105 | + When entity is an API model object (ModelNormal), its internal |
| 106 | + _data_store uses snake_case keys. When it is a plain dict produced |
| 107 | + by ``model.to_dict()``, the keys are also snake_case (the default). |
| 108 | + """ |
| 109 | + attrs_data = entity.get("attributes") or {} |
| 110 | + raw_definition = attrs_data.get("definition") |
| 111 | + definition: CatalogNumberParameterDefinition | None = None |
| 112 | + if raw_definition is not None: |
| 113 | + raw_constraints = raw_definition.get("constraints") |
| 114 | + constraints: CatalogWorkspaceParameterConstraints | None = None |
| 115 | + if raw_constraints is not None: |
| 116 | + constraints = CatalogWorkspaceParameterConstraints( |
| 117 | + min=raw_constraints.get("min"), |
| 118 | + max=raw_constraints.get("max"), |
| 119 | + ) |
| 120 | + definition = CatalogNumberParameterDefinition( |
| 121 | + default_value=raw_definition.get("default_value", 0.0), |
| 122 | + type=raw_definition.get("type", "NUMBER"), |
| 123 | + constraints=constraints, |
| 124 | + ) |
| 125 | + raw_tags = attrs_data.get("tags") |
| 126 | + return cls( |
| 127 | + id=entity.get("id"), |
| 128 | + definition=definition, |
| 129 | + title=attrs_data.get("title"), |
| 130 | + description=attrs_data.get("description"), |
| 131 | + tags=raw_tags if raw_tags is not None else [], |
| 132 | + ) |
| 133 | + |
| 134 | + def as_post_document(self) -> JsonApiParameterPostOptionalIdDocument: |
| 135 | + """Serialize to document for POST (create).""" |
| 136 | + attributes = self._build_attributes() |
| 137 | + kwargs: dict[str, Any] = {} |
| 138 | + if self.id is not None: |
| 139 | + kwargs["id"] = self.id |
| 140 | + data = JsonApiParameterPostOptionalId( |
| 141 | + type="parameter", |
| 142 | + attributes=attributes, |
| 143 | + _check_type=False, |
| 144 | + **kwargs, |
| 145 | + ) |
| 146 | + return JsonApiParameterPostOptionalIdDocument(data=data, _check_type=False) |
| 147 | + |
| 148 | + def as_put_document(self) -> JsonApiParameterInDocument: |
| 149 | + """Serialize to document for PUT (full update).""" |
| 150 | + attributes = self._build_attributes() |
| 151 | + data = JsonApiParameterIn( |
| 152 | + id=self.id, |
| 153 | + type="parameter", |
| 154 | + attributes=attributes, |
| 155 | + _check_type=False, |
| 156 | + ) |
| 157 | + return JsonApiParameterInDocument(data=data, _check_type=False) |
| 158 | + |
| 159 | + def as_patch_document(self) -> JsonApiParameterPatchDocument: |
| 160 | + """Serialize to document for PATCH.""" |
| 161 | + attributes = self._build_attributes() |
| 162 | + data = JsonApiParameterPatch( |
| 163 | + id=self.id, |
| 164 | + type="parameter", |
| 165 | + attributes=attributes, |
| 166 | + _check_type=False, |
| 167 | + ) |
| 168 | + return JsonApiParameterPatchDocument(data=data, _check_type=False) |
| 169 | + |
| 170 | + def _build_attributes(self) -> dict[str, Any]: |
| 171 | + attributes: dict[str, Any] = {} |
| 172 | + if self.definition is not None: |
| 173 | + definition_dict: dict[str, Any] = { |
| 174 | + "defaultValue": self.definition.default_value, |
| 175 | + "type": self.definition.type, |
| 176 | + } |
| 177 | + if self.definition.constraints is not None: |
| 178 | + constraints_dict: dict[str, Any] = {} |
| 179 | + if self.definition.constraints.min is not None: |
| 180 | + constraints_dict["min"] = self.definition.constraints.min |
| 181 | + if self.definition.constraints.max is not None: |
| 182 | + constraints_dict["max"] = self.definition.constraints.max |
| 183 | + if constraints_dict: |
| 184 | + definition_dict["constraints"] = constraints_dict |
| 185 | + attributes["definition"] = definition_dict |
| 186 | + if self.title is not None: |
| 187 | + attributes["title"] = self.title |
| 188 | + if self.description is not None: |
| 189 | + attributes["description"] = self.description |
| 190 | + if self.tags: |
| 191 | + attributes["tags"] = self.tags |
| 192 | + return attributes |
0 commit comments