Skip to content

Commit 11fa9f8

Browse files
committed
Feeback from review
1 parent 711e2cc commit 11fa9f8

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Add method schema.set_cache() to populate the cache manually (primarily for unit testing)
1+
Add method `client.schema.set_cache()` to populate the cache manually (primarily for unit testing)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
The 'timeout' parameter while fetching the schema has been deprecated. the default_timeout will be used instead.
1+
The 'timeout' parameter while creating a node or fetching the schema has been deprecated. the default_timeout will be used instead.

infrahub_sdk/schema/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,19 @@ def validate_data_against_schema(self, schema: MainSchemaTypesAPI, data: dict) -
110110
message=f"{key} is not a valid value for {identifier}",
111111
)
112112

113-
def set_cache(self, schema: dict[str, Any] | BranchSchema, branch: str | None = None) -> None:
113+
def set_cache(self, schema: dict[str, Any] | SchemaRootAPI | BranchSchema, branch: str | None = None) -> None:
114+
"""
115+
Set the cache manually (primarily for unit testing)
116+
117+
Args:
118+
schema: The schema to set the cache as provided by the /api/schema endpoint either in dict or SchemaRootAPI format
119+
branch: The name of the branch to set the cache for.
120+
"""
114121
branch = branch or self.client.default_branch
115122

116-
if isinstance(schema, dict):
123+
if isinstance(schema, SchemaRootAPI):
124+
schema = BranchSchema.from_schema_root_api(data=schema)
125+
elif isinstance(schema, dict):
117126
schema = BranchSchema.from_api_response(data=schema)
118127

119128
self.cache[branch] = schema

infrahub_sdk/schema/main.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def to_schema_dict(self) -> dict[str, Any]:
345345
class SchemaRootAPI(BaseModel):
346346
model_config = ConfigDict(use_enum_values=True)
347347

348-
version: str
348+
main: str | None = None
349349
generics: list[GenericSchemaAPI] = Field(default_factory=list)
350350
nodes: list[NodeSchemaAPI] = Field(default_factory=list)
351351
profiles: list[ProfileSchemaAPI] = Field(default_factory=list)
@@ -360,23 +360,29 @@ class BranchSchema(BaseModel):
360360

361361
@classmethod
362362
def from_api_response(cls, data: MutableMapping[str, Any]) -> Self:
363+
"""
364+
Convert an API response from /api/schema into a BranchSchema object.
365+
"""
366+
return cls.from_schema_root_api(data=SchemaRootAPI(**data))
367+
368+
@classmethod
369+
def from_schema_root_api(cls, data: SchemaRootAPI) -> Self:
370+
"""
371+
Convert a SchemaRootAPI object to a BranchSchema object.
372+
"""
363373
nodes: MutableMapping[str, GenericSchemaAPI | NodeSchemaAPI | ProfileSchemaAPI | TemplateSchemaAPI] = {}
364-
for node_schema in data.get("nodes", []):
365-
node = NodeSchemaAPI(**node_schema)
374+
for node in data.nodes:
366375
nodes[node.kind] = node
367376

368-
for generic_schema in data.get("generics", []):
369-
generic = GenericSchemaAPI(**generic_schema)
377+
for generic in data.generics:
370378
nodes[generic.kind] = generic
371379

372-
for profile_schema in data.get("profiles", []):
373-
profile = ProfileSchemaAPI(**profile_schema)
380+
for profile in data.profiles:
374381
nodes[profile.kind] = profile
375382

376-
for template_schema in data.get("templates", []):
377-
template = TemplateSchemaAPI(**template_schema)
383+
for template in data.templates:
378384
nodes[template.kind] = template
379385

380-
schema_hash = data.get("main", "")
386+
schema_hash = data.main or ""
381387

382388
return cls(hash=schema_hash, nodes=nodes)

0 commit comments

Comments
 (0)