-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplane_client.py
More file actions
117 lines (94 loc) · 3.69 KB
/
plane_client.py
File metadata and controls
117 lines (94 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
import requests
from dotenv import load_dotenv
load_dotenv()
class PlaneAPIError(Exception):
"""To handle API Errors."""
...
class PlaneClient:
"""
Sends GET requests to API.
Fetch issues in specific cycle.
Return the data needed for estimation.
"""
def __init__(self) -> None:
self.BASE_URL = os.getenv("PLANE_BASE_URL")
self.TOKEN = os.getenv("PLANE_API_KEY")
self.session = requests.Session()
self.session.headers.update({
"x-api-Key": f"{self.TOKEN}",
"Accept": "application/json"
})
self.workspace_slug = os.getenv('WORKSPACE_SLUG')
self.project_id = os.getenv('PROJECT_ID')
self.cycle_id = os.getenv('CYCLE_ID')
if not self.TOKEN or not self.BASE_URL:
raise ValueError("(e) Missing API Base URL or Token")
def get_project_details(self, workspace_slug: str = None, project_id: str = None) -> dict:
"""
Get details of a project.
Returns:
Dictionary with project details
"""
if workspace_slug is None:
workspace_slug = self.workspace_slug
if project_id is None:
project_id = self.project_id
url = f"{self.BASE_URL}/workspaces/{workspace_slug}/projects/{project_id}/"
try:
response = self.session.get(url, timeout=10)
response.raise_for_status()
except requests.exceptions.RequestException as e:
raise PlaneAPIError(
f"(e) Error fetching project details: {e}") from e
except Exception as e:
raise PlaneAPIError(f"(e) Unexpected error occured: {e}") from e
return response.json()
def get_cycle_details(self, workspace_slug: str = None, project_id: str = None, cycle_id: str = None) -> dict:
"""
Get details of a project.
Returns:
Dictionary with project details
"""
if workspace_slug is None:
workspace_slug = self.workspace_slug
if project_id is None:
project_id = self.project_id
if cycle_id is None:
cycle_id = self.cycle_id
url = f"{self.BASE_URL}/workspaces/{workspace_slug}/projects/{project_id}/cycles/{cycle_id}"
try:
response = self.session.get(url, timeout=10)
response.raise_for_status()
except requests.exceptions.RequestException as e:
raise PlaneAPIError(
f"(e) Error fetching project details: {e}") from e
except Exception as e:
raise PlaneAPIError(f"(e) Unexpected error occured: {e}") from e
return response.json()
def get_issues_by_cycle(self, workspace_slug: str = None, project_id: str = None, cycle_id: str = None) -> list:
"""
Get all issues in a cycle.
Returns:
List of issues
"""
if workspace_slug is None:
workspace_slug = self.workspace_slug
if project_id is None:
project_id = self.project_id
if cycle_id is None:
cycle_id = self.cycle_id
url = f"{self.BASE_URL}/workspaces/{workspace_slug}/projects/{project_id}/issues/"
params = {
"cycle": cycle_id,
"expand": "estimate_point"
}
try:
response = self.session.get(url, params=params, timeout=10)
response.raise_for_status()
except requests.exceptions.RequestException as e:
raise PlaneAPIError(f"(e) Error fetching isssues: {e}") from e
except Exception as e:
raise PlaneAPIError(f"(e) Unexpected error occured: {e}") from e
data = response.json().get("results", [])
return data