🐛 fix(schema.py): fix the transformation of tags in ListComponentResponse to return a list of TagResponse objects
🔥 chore(service.py): remove unnecessary code that flattens the tags in ListComponentResponse
This commit is contained in:
parent
f69917f4b0
commit
dfabb80567
2 changed files with 17 additions and 5 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, validator
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
|
|
@ -45,9 +45,21 @@ class ListComponentResponse(BaseModel):
|
||||||
is_component: Optional[bool]
|
is_component: Optional[bool]
|
||||||
metadata: Optional[dict]
|
metadata: Optional[dict]
|
||||||
user_created: Optional[dict]
|
user_created: Optional[dict]
|
||||||
tags: Optional[List[TagsIdResponse]] = None
|
tags: Optional[List[TagResponse]] = None
|
||||||
downloads_count: Optional[int]
|
downloads_count: Optional[int]
|
||||||
|
|
||||||
|
# tags comes as a TagsIdResponse but we want to return a list of TagResponse
|
||||||
|
@validator("tags", pre=True)
|
||||||
|
def tags_to_list(cls, v):
|
||||||
|
# Check if all values are have id and name
|
||||||
|
# if so, return v else transform to TagResponse
|
||||||
|
if all(["id" in tag and "name" in tag for tag in v]):
|
||||||
|
return v
|
||||||
|
else:
|
||||||
|
return [
|
||||||
|
TagResponse(**tag.get("tags_id")) for tag in v if tag.get("tags_id")
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class DownloadComponentResponse(BaseModel):
|
class DownloadComponentResponse(BaseModel):
|
||||||
id: UUID
|
id: UUID
|
||||||
|
|
|
||||||
|
|
@ -188,9 +188,9 @@ class StoreService(Service):
|
||||||
results = self._get(self.components_url, api_key, params)
|
results = self._get(self.components_url, api_key, params)
|
||||||
results_objects = [ListComponentResponse(**component) for component in results]
|
results_objects = [ListComponentResponse(**component) for component in results]
|
||||||
# Flatten the tags
|
# Flatten the tags
|
||||||
for component in results_objects:
|
# for component in results_objects:
|
||||||
if component.tags:
|
# if component.tags:
|
||||||
component.tags = [tags_id.tags_id for tags_id in component.tags]
|
# component.tags = [tags_id.tags_id for tags_id in component.tags]
|
||||||
return results_objects
|
return results_objects
|
||||||
|
|
||||||
def download(self, api_key: str, component_id: str) -> DownloadComponentResponse:
|
def download(self, api_key: str, component_id: str) -> DownloadComponentResponse:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue