Skip to content

Commit 5306a35

Browse files
committed
Implement Project subclient
1 parent 7751bad commit 5306a35

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

openproject/client.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import httpx
22
from openproject.exceptions import APIError, AuthenticationError
3-
from openproject.types import WorkPackage
3+
from openproject.types import WorkPackage, Project
44

55

66
class Client:
@@ -10,6 +10,7 @@ def __init__(self, base_url: str, api_token: str):
1010
self.api_token = api_token
1111

1212
self.work_packages = WorkPackages(self)
13+
self.projects = Projects(self)
1314

1415
def _handle_response(self, response: httpx.Response):
1516
if response.status_code == 401:
@@ -99,3 +100,34 @@ def update(self, id: int, **kwargs):
99100

100101
def delete(self, id: int):
101102
return self.client._send_request("DELETE", f"/work_packages/{id}")
103+
104+
105+
class Projects(SubClient):
106+
_args_api_mapping = {
107+
"_links": "_links",
108+
"name": "name",
109+
"status_explanation": "statusExplanation",
110+
"description": "description",
111+
}
112+
113+
def _api_payload_from_kwargs(self, **kwargs: Project):
114+
items = self._args_api_mapping.items()
115+
data = {api_args: kwargs[args] for args, api_args in items if args in kwargs}
116+
return data
117+
118+
def list(self):
119+
return self.client._send_request("GET", "/projects")
120+
121+
def view(self, id: int):
122+
return self.client._send_request("GET", f"/projects/{id}")
123+
124+
def create(self, **kwargs):
125+
data = self._api_payload_from_kwargs(**kwargs)
126+
return self.client._send_request("POST", "/projects", data=data)
127+
128+
def update(self, id: int, **kwargs):
129+
data = self._api_payload_from_kwargs(**kwargs)
130+
return self.client._send_request("PATCH", f"/projects/{id}", data=data)
131+
132+
def delete(self, id: int):
133+
return self.client._send_request("DELETE", f"/projects/{id}")

openproject/types.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,16 @@ class WorkPackage(TypedDict, total=False):
8080
custom_field_2: int
8181
created_at: str
8282
updated_at: str
83+
84+
85+
class StatusExplanation(TypedDict, total=False):
86+
format: str
87+
raw: str
88+
html: str
89+
90+
91+
class Project(TypedDict, total=False):
92+
_links: Links
93+
name: str
94+
status_explanation: StatusExplanation
95+
description: Description

0 commit comments

Comments
 (0)