Skip to content

Commit

Permalink
added back the addt code to client.py to remove circular imports - th…
Browse files Browse the repository at this point in the history
…is probably can be resolved better in a future release
  • Loading branch information
Nevalicjus committed Dec 4, 2022
1 parent 318c996 commit 09746bf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = ntfpy
version = 0.0.10
version = 0.0.11
author = Nevalicjus
author_email = [email protected]
description = Python ntfy.sh API Wrapper
Expand Down
54 changes: 45 additions & 9 deletions src/ntfpy/types/client.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
from requests import Response
from typing import Optional, Callable, Final, Sequence
import requests
import logging
import json
import base64
from typing import Callable, Final, Optional, Sequence

from .server import NTFYServer
from .actions import NTFYAction
from .attachments import NTFYUrlAttachment
from .message import NTFYMessage
from .push_message import NTFYPushMessage, PRIORITY
from .user import NTFYUser
from ..raw_send import raw_send, raw_send_message
from ..raw_subscribe import raw_subscribe

__all__ = [
"NTFYClient"
]

logger = logging.getLogger(__name__)
OPTIONAL_FIELDS = ["title", "priority", "tags", "click", "attach", "actions", "email", "delay", "icon"]

class NTFYClient():
"""
Attributes
Expand All @@ -29,7 +33,7 @@ def __init__(self, server: NTFYServer, topic: str, user: Optional[NTFYUser] = No

def send(self, message: str, title: Optional[str] = None, priority: Optional[PRIORITY] = None, tags: Optional[str] = None,
click: Optional[str] = None, attach: Optional[NTFYUrlAttachment] = None, actions: Optional[Sequence[NTFYAction]] = None,
email: Optional[str] = None, delay: Optional[str] = None, icon: Optional[str] = None) -> Response:
email: Optional[str] = None, delay: Optional[str] = None, icon: Optional[str] = None) -> requests.Response:
"""
Parameters
----------
Expand Down Expand Up @@ -59,17 +63,34 @@ def send(self, message: str, title: Optional[str] = None, priority: Optional[PRI
requests.Response
"""
auth = self.user.auth() if self.user is not None else None
return raw_send(self.server.url, self.topic, message, auth = auth, title = title, priority = priority, tags = tags, click = click, attach = attach, actions = actions, email = email, delay = delay, icon = icon)
msg = NTFYPushMessage(message, tags = tags, actions = actions)
msg.title = title
msg.priority = priority
msg.click_url = click
msg.attachment = attach
msg.email = email
msg.delay = delay
msg.icon_url = icon

headers: Mapping[str, str] = {}
if auth is not None:
headers["Authorization"] = f"Basic {base64.b64encode(auth.encode('ascii')).decode('ascii')}"
data = msg.json(self.topic)
return requests.post(f"{self.server.url}/", headers = headers, json = data)

def send_message(self, message: NTFYPushMessage) -> Response:
def send_message(self, message: NTFYPushMessage) -> requests.Response:
"""
Parameters
----------
message: :class:`NTFYPushMessage`
message to send
"""
auth = self.user.auth() if self.user is not None else None
return raw_send_message(self.server.url, self.topic, message, auth = auth)
headers: Mapping[str, str] = {}
if auth is not None:
headers["Authorization"] = f"Basic {base64.b64encode(auth.encode('ascii')).decode('ascii')}"
data = message.json(self.topic)
return requests.post(f"{self.server.url}/", headers = headers, json = data)

async def subscribe(self, handler: Callable[[NTFYMessage], None] = print):
"""
Expand All @@ -79,4 +100,19 @@ async def subscribe(self, handler: Callable[[NTFYMessage], None] = print):
function to handle printing of received messages; by default it's :py:func:`print`
"""
auth = self.user.auth() if self.user is not None else None
await raw_subscribe(self.server.url, self.topic, auth = auth, handler = handler)

headers = {}
if auth is not None:
headers["Authorization"] = f"Basic {base64.b64encode(auth.encode('ascii')).decode('ascii')}"
r = requests.get(f"{self.server}/{self.topic}/json", stream = True, headers = headers)
for l in r.iter_lines():
if l:
d = json.loads(l.decode("utf-8"))
if d["event"] == "message":
m = NTFYMessage(d["message"], d["id"], d["time"], d["topic"])
for x in OPTIONAL_FIELDS:
if x in d:
setattr(m, x, d[x])
handler(m)
else:
logger.debug(d)

0 comments on commit 09746bf

Please sign in to comment.