|
1 | 1 | import requests |
2 | | -from requests.exceptions import ConnectionError |
3 | 2 |
|
4 | | -from znotify.static import ENDPOINT |
| 3 | +from znotify.static import ENDPOINT, Priority |
| 4 | +from znotify.version import __version__ |
5 | 5 |
|
6 | 6 |
|
7 | 7 | class Client: |
8 | | - def __init__(self, user_id, endpoint): |
| 8 | + def __init__(self, user_id: str, endpoint: str): |
9 | 9 | self.endpoint = endpoint if endpoint else ENDPOINT |
10 | 10 | self.user_id = user_id |
| 11 | + self.session = requests.Session() |
| 12 | + self.session.headers.update({ |
| 13 | + "User-Agent": f"znotify-py-sdk/{__version__}", |
| 14 | + }) |
11 | 15 |
|
12 | 16 | @staticmethod |
13 | | - def create(user_id, endpoint=None): |
| 17 | + def create(user_id: str, endpoint: str = None) -> "Client": |
14 | 18 | client = Client(user_id, endpoint) |
15 | 19 | client.check() |
16 | 20 | return client |
17 | 21 |
|
18 | 22 | def check(self): |
19 | | - resp = requests.get(f"{self.endpoint}/check", params={"user_id": self.user_id}) |
| 23 | + resp = self.session.get(f"{self.endpoint}/check", params={"user_id": self.user_id}) |
20 | 24 | if not resp.json()["body"]: |
21 | 25 | raise Exception("User ID not valid") |
22 | 26 |
|
23 | | - def send(self, content, title=None, long=None): |
| 27 | + def send(self, content: str, title: str = None, long: str = None, priority: Priority = None): |
24 | 28 | if content is None or content == "": |
25 | 29 | raise Exception("Content is required") |
26 | 30 |
|
27 | 31 | if title is None: |
28 | 32 | title = "Notification" |
29 | 33 | if long is None: |
30 | 34 | long = "" |
| 35 | + if priority is None: |
| 36 | + priority = Priority.NORMAL |
31 | 37 |
|
32 | 38 | data = { |
33 | 39 | "title": title, |
34 | 40 | "content": content, |
35 | | - "long": long |
| 41 | + "long": long, |
| 42 | + "priority": priority.value, |
36 | 43 | } |
37 | | - try: |
38 | | - resp = requests.post(f"{self.endpoint}/{self.user_id}/send", data=data) |
39 | | - except ConnectionError as e: |
40 | | - raise Exception("Connection error") from e |
| 44 | + |
| 45 | + resp = self.session.post(f"{self.endpoint}/{self.user_id}/send", data=data) |
41 | 46 | return resp.json()["body"] |
0 commit comments