Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update project tags type to what is expected/returned by API #150

Merged
merged 3 commits into from
Jun 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions doccano_client/repositories/project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Iterator
from typing import Any, Dict, Iterator

from doccano_client.models.project import Project
from doccano_client.repositories.base import BaseRepository
Expand All @@ -12,6 +12,31 @@ class ProjectRepository:
def __init__(self, client: BaseRepository):
self._client = client

def _to_domain(self, response: Dict[str, Any]) -> Project:
"""Convert a response to a domain object

Args:
response (Dict[str, Any]): The response to convert

Returns:
Project: The converted project
"""
response["tags"] = [tag["text"] for tag in response.get("tags", [])]
return Project.parse_obj(response)

def _to_persistent(self, project: Project) -> Dict[str, Any]:
"""Convert a domain object to a persistent object

Args:
project (Project): The project to convert

Returns:
Dict[str, Any]: The converted project
"""
project_dict = project.dict()
project_dict["tags"] = [{"text": tag} for tag in project_dict["tags"]]
return project_dict

def find_by_id(self, project_id: int) -> Project:
"""Find a project by id

Expand All @@ -22,7 +47,7 @@ def find_by_id(self, project_id: int) -> Project:
Project: The found project
"""
response = self._client.get(f"projects/{project_id}")
return Project.parse_obj(response.json())
return self._to_domain(response.json())

def list(self) -> Iterator[Project]:
"""Return all projects in which you are a member
Expand All @@ -35,7 +60,7 @@ def list(self) -> Iterator[Project]:
while True:
projects = response.json()
for project in projects["results"]:
yield Project.parse_obj(project)
yield self._to_domain(project)

if projects["next"] is None:
break
Expand All @@ -51,8 +76,10 @@ def create(self, project: Project) -> Project:
Returns:
Project: The created project
"""
response = self._client.post("projects", json=project.dict(exclude={"id"}))
return Project.parse_obj(response.json())
payload = self._to_persistent(project)
payload.pop("id", None)
response = self._client.post("projects", json=payload)
return self._to_domain(response.json())

def update(self, project: Project) -> Project:
"""Update a project
Expand All @@ -64,8 +91,9 @@ def update(self, project: Project) -> Project:
Project: The updated project
"""
resource = f"projects/{project.id}"
response = self._client.put(resource, json=project.dict())
return Project.parse_obj(response.json())
payload = self._to_persistent(project)
response = self._client.put(resource, json=payload)
return self._to_domain(response.json())

def delete(self, project: Project | int):
"""Delete a project
Expand Down