Skip to content

Commit 7b40fa1

Browse files
authored
Merge pull request #26 from adator85/V2.x.x
V2.0.5
2 parents a9f561e + 30717fd commit 7b40fa1

File tree

6 files changed

+306
-241
lines changed

6 files changed

+306
-241
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ docs/*
99
setup.py
1010
test.py
1111
test_live.py
12-
CallbackObject.py
1312
*.zip

how_to_use_it_live.py

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1+
import asyncio
12
import sys
3+
import time
4+
import threading
5+
from types import SimpleNamespace
26
from unrealircd_rpc_py.Live import LiveWebsocket
37

48
##########################################
59
# How to connect using callback function #
610
##########################################
711

8-
def callback_function_irc(response):
12+
def callback_function_irc(response: SimpleNamespace) -> None:
13+
# Response model: https://www.unrealircd.org/docs/JSON-RPC:Log#log.subscribe
14+
# Possible responses: https://www.unrealircd.org/docs/List_of_all_log_messages
15+
16+
# High level error
17+
if liverpc.get_error.code != 0:
18+
print("ERROR:", liverpc.get_error.code, liverpc.get_error.message)
19+
return
20+
21+
# Because of the concurrent approach! there is also local errors
22+
if hasattr(response, 'error'):
23+
# Check if you are allowed to use log endpoint
24+
print(response.error.code, response.error.message)
25+
return
926

1027
if hasattr(response, 'result'):
11-
if isinstance(response.result, bool) and response.result:
12-
# Check if you are allowed to use log endpoint
13-
print(liverpc.get_error)
28+
if isinstance(response.result, bool):
29+
if response.result:
30+
print(f"{response.method} has been activated")
1431
return
1532

1633
level = response.result.level if hasattr(response.result, 'level') else ''
@@ -21,10 +38,6 @@ def callback_function_irc(response):
2138

2239
build_msg = f"{log_source}: [{level}] {subsystem}.{event_id} - {msg}"
2340

24-
# Check if there is an error
25-
if liverpc.get_error.code != 0:
26-
print(f"Your Error message: {liverpc.get_error.message}")
27-
2841
print(build_msg)
2942

3043
# Init your live stream using websocket
@@ -36,12 +49,28 @@ def callback_function_irc(response):
3649
callback_method_or_function_name='callback_function_irc'
3750
)
3851

39-
# Check if connection is correct
40-
if liverpc.get_error.code != 0:
41-
print(liverpc.get_error.message, liverpc.get_error.code)
42-
sys.exit(1)
52+
def thread_subscribe() -> None:
53+
# This will run until the thread_unsubscribe is called!
54+
response = asyncio.run(liverpc.subscribe(["all"]))
55+
print(f"FINAL RESPONSE OF SUBSCRIBE: {response}")
4356

44-
liverpc.subscribe(["all"])
57+
def thread_unsubscribe() -> None:
58+
response = asyncio.run(liverpc.unsubscribe())
59+
print(f"FINAL RESPONSE OF UNSUBSCRIBE: {response}")
60+
61+
62+
th_subscribe = threading.Thread(target=thread_subscribe, daemon=False)
63+
th_unsubscribe = threading.Thread(target=thread_unsubscribe, daemon=False)
64+
65+
# Subscribe to the stream
66+
th_subscribe.start()
67+
68+
# Do some stuff
69+
time.sleep(2)
70+
71+
# If you want to disconnect from the stream
72+
# run your new unsubscribe thread
73+
th_unsubscribe.start()
4574

4675
########################################
4776
# How to connect using callback method #
@@ -63,13 +92,37 @@ def __init__(self):
6392
print(self.liverpc.get_error.message, self.liverpc.get_error.code)
6493
return
6594

66-
self.liverpc.subscribe(['all'])
95+
def thread_subscribe(self) -> None:
96+
97+
response: dict[str, dict] = asyncio.run(self.liverpc.subscribe())
98+
print("[JSONRPC SUBSCRIBE] Subscribe to the stream!")
99+
print(response)
67100

68-
def callback_method_irc(self, response):
101+
def thread_unsubscribe(self) -> None:
102+
103+
response: dict[str, dict] = asyncio.run(self.liverpc.unsubscribe())
104+
print("[JSONRPC UNSUBSCRIBE] Unsubscribe from the stream!")
105+
print(response)
106+
107+
def callback_method_irc(self, response: SimpleNamespace) -> None:
108+
# Response model: https://www.unrealircd.org/docs/JSON-RPC:Log#log.subscribe
109+
# Possible responses: https://www.unrealircd.org/docs/List_of_all_log_messages
110+
111+
# High level error
112+
if self.liverpc.get_error.code != 0:
113+
print("ERROR:", self.liverpc.get_error.code, self.liverpc.get_error.message)
114+
return
115+
116+
# Because of the concurrent approach! there is also local errors
117+
if hasattr(response, 'error'):
118+
# Check if you are allowed to use log endpoint
119+
print(response.error.code, response.error.message)
120+
return
69121

70122
if hasattr(response, 'result'):
71-
if isinstance(response.result, bool) and response.result:
72-
print(self.liverpc.get_error)
123+
if isinstance(response.result, bool):
124+
if response.result:
125+
print(f"{response.method} has been activated")
73126
return
74127

75128
level = response.result.level if hasattr(response.result, 'level') else ''
@@ -80,7 +133,18 @@ def callback_method_irc(self, response):
80133

81134
build_msg = f"{log_source}: [{level}] {subsystem}.{event_id} - {msg}"
82135
print(build_msg)
83-
print(self.liverpc.get_error)
84136

85137

86-
CallBack()
138+
live_stream = CallBack()
139+
th_subscribe = threading.Thread(target=live_stream.thread_subscribe)
140+
th_unsubscribe = threading.Thread(target=live_stream.thread_unsubscribe)
141+
142+
# Subscribe to the stream
143+
th_subscribe.start()
144+
145+
# Do some stuff
146+
time.sleep(2)
147+
148+
# If you want to disconnect from the stream
149+
# run your unsubscribe thread
150+
th_unsubscribe.start()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='unrealircd_rpc_py',
5-
version='2.0.2',
5+
version='2.0.5',
66
packages=find_packages(exclude=["tests","test_*"]),
77
install_requires=[
88
"requests>=2.25.1",

unrealircd_rpc_py/Definition.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dataclasses import dataclass, field, asdict, fields
22
from json import dumps
3-
from typing import Any
3+
from typing import Any, Optional
44
from warnings import warn
55
from functools import wraps
66

@@ -37,8 +37,22 @@ def get_attributes(self) -> list[str]:
3737
class RPCError(MainModel):
3838
"""This model will contain the error if any"""
3939
code: int = 0
40-
message: str = None
40+
message: Optional[str] = None
4141

42+
@dataclass
43+
class LiveRPCError(MainModel):
44+
"""This the Live JSONRPC Model Error"""
45+
jsonrpc: str = "2.0"
46+
error: RPCError = field(default_factory=RPCError())
47+
id: int = 123
48+
49+
@dataclass
50+
class LiveRPCResult(MainModel):
51+
"""This the Live JSONRPC Model Result"""
52+
jsonrpc: str = "2.0"
53+
method: Optional[str] = None
54+
result: Optional[Any] = None
55+
id: int = 123
4256

4357
@dataclass
4458
class Tls(MainModel):

0 commit comments

Comments
 (0)