-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_api_client.py
More file actions
71 lines (56 loc) · 2.39 KB
/
github_api_client.py
File metadata and controls
71 lines (56 loc) · 2.39 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
"""GitHub API client for fetching and managing notifications."""
from __future__ import annotations
import logging
from typing import Any, Dict, List, Optional
import requests
logger = logging.getLogger(__name__)
GITHUB_API = "https://api.github.com"
class GitHubAPIClient:
"""Minimal GitHub REST API client focused on notifications."""
def __init__(self, token: str):
self._session = requests.Session()
self._session.headers.update(
{
"Authorization": f"token {token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
)
def _get(self, path: str, params: Optional[Dict] = None) -> Any:
resp = self._session.get(f"{GITHUB_API}{path}", params=params, timeout=15)
resp.raise_for_status()
return resp.json()
def _patch(self, path: str, data: Dict) -> Any:
resp = self._session.patch(f"{GITHUB_API}{path}", json=data, timeout=15)
resp.raise_for_status()
return resp.json() if resp.content else {}
def _delete(self, path: str) -> None:
resp = self._session.delete(f"{GITHUB_API}{path}", timeout=15)
resp.raise_for_status()
def get_notifications(
self, all_: bool = False, participating: bool = False, per_page: int = 50
) -> List[Dict]:
"""Fetch unread (or all) notifications."""
return self._get(
"/notifications",
params={"all": str(all_).lower(), "participating": str(participating).lower(), "per_page": per_page},
)
def mark_thread_read(self, thread_id: str) -> None:
"""Mark a notification thread as read."""
self._patch(f"/notifications/threads/{thread_id}", {})
def mute_thread(self, thread_id: str) -> None:
"""Mute (unsubscribe) a notification thread."""
resp = self._session.put(
f"{GITHUB_API}/notifications/threads/{thread_id}/subscription",
json={"ignored": True},
timeout=15,
)
resp.raise_for_status()
def add_label(self, owner: str, repo: str, issue_number: int, label: str) -> None:
"""Add a label to an issue or PR."""
resp = self._session.post(
f"{GITHUB_API}/repos/{owner}/{repo}/issues/{issue_number}/labels",
json={"labels": [label]},
timeout=15,
)
resp.raise_for_status()