Skip to content

Commit

Permalink
Merge pull request #150 from dsciacca/fix-get-project-with-tags
Browse files Browse the repository at this point in the history
Update project tags type to what is expected/returned by API
  • Loading branch information
Hironsan authored Jun 13, 2023
2 parents 1f1a13f + f6beadc commit 12065e3
Showing 1 changed file with 35 additions and 7 deletions.
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

0 comments on commit 12065e3

Please sign in to comment.