Skip to content

Commit 61c2ed9

Browse files
committed
feat: initial commit
1 parent 24f9ef8 commit 61c2ed9

File tree

10 files changed

+369
-0
lines changed

10 files changed

+369
-0
lines changed

.gitignore

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
.idea
2+
*.iml
3+
out
4+
gen
5+
__pycache__/
6+
*.py[cod]
7+
*$py.class
8+
*.so
9+
.Python
10+
build/
11+
develop-eggs/
12+
dist/
13+
downloads/
14+
eggs/
15+
.eggs/
16+
lib/
17+
lib64/
18+
parts/
19+
sdist/
20+
var/
21+
wheels/
22+
share/python-wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
*.manifest
28+
*.spec
29+
pip-log.txt
30+
pip-delete-this-directory.txt
31+
htmlcov/
32+
.tox/
33+
.nox/
34+
.coverage
35+
.coverage.*
36+
.cache
37+
nosetests.xml
38+
coverage.xml
39+
*.cover
40+
*.py,cover
41+
.hypothesis/
42+
.pytest_cache/
43+
cover/
44+
*.mo
45+
*.pot
46+
*.log
47+
local_settings.py
48+
db.sqlite3
49+
db.sqlite3-journal
50+
instance/
51+
.webassets-cache
52+
.scrapy
53+
docs/_build/
54+
.pybuilder/
55+
target/
56+
.ipynb_checkpoints
57+
profile_default/
58+
ipython_config.py
59+
__pypackages__/
60+
celerybeat-schedule
61+
celerybeat.pid
62+
*.sage.py
63+
.env
64+
.venv
65+
env/
66+
venv/
67+
ENV/
68+
env.bak/
69+
venv.bak/
70+
.spyderproject
71+
.spyproject
72+
.ropeproject
73+
/site
74+
.mypy_cache/
75+
.dmypy.json
76+
dmypy.json
77+
.pyre/
78+
.pytype/
79+
cython_debug/

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# notify-py-sdk
2+
3+
Send notifications to [Notify](https://github.com/znotify/Notify)
4+
5+
## Installation
6+
7+
```bash
8+
pip install znotify
9+
```
10+
11+
## Usage
12+
13+
```python
14+
import znotify
15+
16+
client = znotify.Client.create("user_id")
17+
client.send("Hello World")
18+
```
19+
20+
## Development
21+
22+
### Run tests
23+
24+
```bash
25+
python -m unittest discover
26+
```
27+

poetry.lock

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[tool.poetry]
2+
name = "znotify"
3+
version = "0.0.1"
4+
description = "sdk for notify"
5+
authors = ["Zxilly <[email protected]>"]
6+
license = "GPL-3.0"
7+
readme = "README.md"
8+
repository = "https://github.com/ZNotify/py-sdk"
9+
classifiers = [
10+
"Programming Language :: Python :: 3",
11+
"Development Status :: 4 - Beta",
12+
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
13+
"Operating System :: OS Independent",
14+
"Natural Language :: Chinese (Simplified)"
15+
]
16+
17+
[tool.poetry.dependencies]
18+
python = "^3.10"
19+
requests = "^2.27.1"
20+
21+
[tool.poetry.dev-dependencies]
22+
23+
[tool.poetry.urls]
24+
"Bug Tracker" = "https://github.com/ZNotify/py-sdk/issues"
25+
26+
[build-system]
27+
requires = ["poetry-core>=1.0.0"]
28+
build-backend = "poetry.core.masonry.api"

tests/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .send_test import *
2+
from .client_test import *

tests/client_test.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import unittest
2+
from znotify import Client
3+
4+
5+
class TestClient(unittest.TestCase):
6+
@classmethod
7+
def setUpClass(cls):
8+
cls.client = Client.create("zxilly")
9+
pass
10+
11+
def test_client_create(self):
12+
self.assertIsInstance(self.client, Client)
13+
pass
14+
15+
def test_client_create_failed(self):
16+
with self.assertRaises(Exception) as context:
17+
Client.create("error")
18+
self.assertTrue("User ID not valid" in str(context.exception))
19+
pass
20+
21+
def test_client_send_0(self):
22+
with self.assertRaises(Exception) as context:
23+
self.client.send("")
24+
self.assertTrue("Content is required" in str(context.exception))
25+
pass
26+
27+
def test_client_send_1(self):
28+
self.assertEqual(self.client.send("test"), {
29+
"content": "test",
30+
"long": "",
31+
"title": "Notification"
32+
})
33+
pass
34+
35+
def test_client_send_2(self):
36+
self.assertEqual(self.client.send("content", "title"), {
37+
"content": "content",
38+
"long": "",
39+
"title": "title"
40+
})
41+
pass
42+
43+
def test_client_send_3(self):
44+
self.assertEqual(self.client.send("content", "title", "long"), {
45+
"content": "content",
46+
"long": "long",
47+
"title": "title"
48+
})
49+
50+
51+
if __name__ == "__main__":
52+
unittest.main()

tests/send_test.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import unittest
2+
from znotify.send import send
3+
4+
5+
class TestSend(unittest.TestCase):
6+
7+
def test_send_create_failed(self):
8+
with self.assertRaises(Exception) as context:
9+
send("error", "error")
10+
self.assertTrue("User ID not valid" in str(context.exception))
11+
pass
12+
13+
def test_send_0(self):
14+
with self.assertRaises(Exception) as context:
15+
send("zxilly", "")
16+
self.assertTrue("Content is required" in str(context.exception))
17+
pass
18+
19+
def test_send_1(self):
20+
self.assertEqual(send("zxilly", "test"), {
21+
"content": "test",
22+
"long": "",
23+
"title": "Notification"
24+
})
25+
pass
26+
27+
def test_send_2(self):
28+
self.assertEqual(send("zxilly", "content", "title"), {
29+
"content": "content",
30+
"long": "",
31+
"title": "title"
32+
})
33+
pass
34+
35+
def test_send_3(self):
36+
self.assertEqual(send("zxilly", "content", "title", "long"), {
37+
"content": "content",
38+
"long": "long",
39+
"title": "title"
40+
})
41+
42+
43+
if __name__ == "__main__":
44+
unittest.main()

znotify/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .client import *
2+
from .send import *

znotify/client.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import sys
2+
3+
import requests
4+
from requests.exceptions import ConnectionError
5+
6+
7+
class Client:
8+
def __init__(self, user_id, endpoint):
9+
self.endpoint = endpoint if endpoint else "https://push.learningman.top"
10+
self.user_id = user_id
11+
12+
@staticmethod
13+
def create(user_id, endpoint=None):
14+
client = Client(user_id, endpoint)
15+
client.check()
16+
return client
17+
18+
def check(self):
19+
resp = requests.get(f"{self.endpoint}/{self.user_id}/check")
20+
if not resp.json():
21+
raise Exception("User ID not valid")
22+
23+
def send(self, content, title=None, long=None):
24+
if content is None or content == "":
25+
raise Exception("Content is required")
26+
27+
params = {}
28+
if 'unittest' in sys.modules:
29+
params['dry'] = True
30+
31+
if title is None:
32+
title = "Notification"
33+
if long is None:
34+
long = ""
35+
36+
data = {
37+
"title": title,
38+
"content": content,
39+
"long": long
40+
}
41+
try:
42+
resp = requests.post(f"{self.endpoint}/{self.user_id}/send", data=data, params=params)
43+
except ConnectionError as e:
44+
raise Exception("Connection error") from e
45+
return resp.json()

znotify/send.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from znotify import Client
2+
3+
4+
def send(user_id, content, title=None, long=None):
5+
client = Client.create(user_id)
6+
return client.send(content, title, long)

0 commit comments

Comments
 (0)