Skip to content

Commit 766f324

Browse files
authored
Merge pull request #23 from adator85/v2.x.x
Moving to version 2
2 parents 6196123 + 224b57a commit 766f324

File tree

8 files changed

+436
-235
lines changed

8 files changed

+436
-235
lines changed

README.MD

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,29 @@ If you are using Python3, this package can help you to parse all json responses
4949
path_to_socket_file='/path/to/unrealircd/data/rpc.socket'
5050
)
5151
```
52-
### Live Connection (Local Only)
52+
### Live Connection using UnixSocket (Local Only)
5353
```python
54-
from unrealircd_rpc_py.Live import Live
54+
from unrealircd_rpc_py.Live import LiveUnixSocket
5555
# Live Connection (Local only) using unix socket
56-
LiveRpc = Live(
57-
req_method='unixsocket',
58-
path_to_socket_file='/path/to/unrealircd/data/rpc.socket',
56+
LiveRpc = LiveUnixSocket(
5957
callback_object_instance=Callback_class_instance,
60-
callback_method_name='your_method_name'
58+
callback_method_name='your_method_name',
59+
path_to_socket_file='/path/to/unrealircd/data/rpc.socket'
6160
)
6261
```
63-
### Live connection (Local or Remote)
62+
### Live connection using Websocket (Local or Remote)
6463
```python
65-
from unrealircd_rpc_py.Live import Live
66-
# Live Connection (Local Or Remote)
67-
liveRpc = Live(
68-
req_method='websocket',
64+
from unrealircd_rpc_py.Live import LiveWebsocket
65+
# Live Connection (Local Or Remote) using websocket
66+
liveRpc = LiveWebsocket(
67+
callback_object_instance=InitCallbackClass,
68+
callback_method_name='your_method_name',
6969
url='https://your.irc.domaine.org:8600/api',
7070
username='apiname',
71-
password='apiPASSWORD',
72-
callback_object_instance=InitCallbackClass,
73-
callback_method_name='your_method_name'
71+
password='apiPASSWORD'
7472
)
7573
```
76-
## How to work with (remotly)
74+
## How to work with remotly
7775
This package allows easy interfacing with UnrealIRCd through regular Python3 code, such as:
7876
```python
7977
from unrealircd_rpc_py.Loader import Loader
@@ -169,18 +167,18 @@ This package allows easy interfacing with UnrealIRCd through regular Python3 cod
169167
# Live Connection via unixsocket or websocket
170168
## How to work with (using Live unixsocket)
171169
```python
172-
from unrealircd_rpc_py.Live import Live
170+
from unrealircd_rpc_py.Live import LiveUnixSocket
173171

174172
# This is un callback class that will recieve the response
175173
from TestObject import TestObject
176174

177175
InitCallbackClass = TestObject()
178176

179177
# The Callback method must always have 1 parameter as string
180-
liveRpc = Live(req_method='unixsocket',
181-
path_to_socket_file='/path/to/unrealircd/data/rpc.socket',
178+
liveRpc = LiveUnixSocket(
182179
callback_object_instance=InitCallbackClass,
183-
callback_method_name='your_method_name'
180+
callback_method_name='your_method_name',
181+
path_to_socket_file='/path/to/unrealircd/data/rpc.socket'
184182
)
185183

186184
# This method will run forever and will send to your callback method the response
@@ -194,20 +192,20 @@ This package allows easy interfacing with UnrealIRCd through regular Python3 cod
194192
```
195193
## How to work with (using Live websocket)
196194
```python
197-
from unrealircd_rpc_py.Live import Live
195+
from unrealircd_rpc_py.Live import LiveWebsocket
198196

199197
# This is un callback class that will recieve the response
200198
from TestObject import TestObject
201199

202200
InitCallbackClass = TestObject()
203201

204202
# The Callback method must always have 1 parameter as string
205-
liveRpc = Live(req_method='websocket',
203+
liveRpc = LiveWebsocket(
204+
callback_object_instance=InitCallbackClass,
205+
callback_method_name='your_method_name',
206206
url='https://your.irc.domaine.org:8600/api',
207207
username='apiname',
208-
password='apiPASSWORD',
209-
callback_object_instance=InitCallbackClass,
210-
callback_method_name='your_method_name'
208+
password='apiPASSWORD'
211209
)
212210

213211
# This method will run forever and will send to your callback method the response

how_to_use_it.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
password='your-rpc-password'
1111
)
1212

13+
# Check if connection is correct
14+
if rpc.get_error.code != 0:
15+
print(f"You error message: {rpc.get_error.message}")
16+
1317
# Use User object
1418
Users = rpc.User.list_()
1519

how_to_use_it_live.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import sys
2+
from unrealircd_rpc_py.Live import LiveWebsocket
3+
4+
##########################################
5+
# How to connect using callback function #
6+
##########################################
7+
8+
def callback_function_irc(response):
9+
10+
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)
14+
return
15+
16+
level = response.result.level if hasattr(response.result, 'level') else ''
17+
subsystem = response.result.subsystem if hasattr(response.result, 'subsystem') else ''
18+
event_id = response.result.event_id if hasattr(response.result, 'event_id') else ''
19+
log_source = response.result.log_source if hasattr(response.result, 'log_source') else ''
20+
msg = response.result.msg if hasattr(response.result, 'msg') else ''
21+
22+
build_msg = f"{log_source}: [{level}] {subsystem}.{event_id} - {msg}"
23+
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+
28+
print(build_msg)
29+
30+
# Init your live stream using websocket
31+
liverpc = LiveWebsocket(
32+
url="https://your.rpc.link:PORT/api",
33+
username='Your-rpc-username',
34+
password='your-rpc-password',
35+
callback_object_instance=sys.modules[__name__],
36+
callback_method_or_function_name='callback_function_irc'
37+
)
38+
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)
43+
44+
liverpc.subscribe(["all"])
45+
46+
########################################
47+
# How to connect using callback method #
48+
########################################
49+
50+
class CallBack:
51+
52+
def __init__(self):
53+
54+
self.liverpc: LiveWebsocket = LiveWebsocket(
55+
url="https://your.rpc.link:PORT/api",
56+
username='Your-rpc-username',
57+
password='your-rpc-password',
58+
callback_object_instance=self,
59+
callback_method_or_function_name='callback_method_irc'
60+
)
61+
62+
if self.liverpc.get_error.code != 0:
63+
print(self.liverpc.get_error.message, self.liverpc.get_error.code)
64+
return
65+
66+
self.liverpc.subscribe(['all'])
67+
68+
def callback_method_irc(self, response):
69+
70+
if hasattr(response, 'result'):
71+
if isinstance(response.result, bool) and response.result:
72+
print(self.liverpc.get_error)
73+
return
74+
75+
level = response.result.level if hasattr(response.result, 'level') else ''
76+
subsystem = response.result.subsystem if hasattr(response.result, 'subsystem') else ''
77+
event_id = response.result.event_id if hasattr(response.result, 'event_id') else ''
78+
log_source = response.result.log_source if hasattr(response.result, 'log_source') else ''
79+
msg = response.result.msg if hasattr(response.result, 'msg') else ''
80+
81+
build_msg = f"{log_source}: [{level}] {subsystem}.{event_id} - {msg}"
82+
print(build_msg)
83+
print(self.liverpc.get_error)
84+
85+
86+
CallBack()

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='1.1.0',
5+
version='2.0.0',
66
packages=find_packages(),
77
install_requires=[
88
"requests>=2.25.1",

unrealircd_rpc_py/EngineError.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,17 @@ def set_error(self, code:int, message:str):
3030
"""
3131
self.Error.code = code
3232
self.Error.message = message
33+
34+
class InvalidUrlFormat(Exception):
35+
36+
def __init__(self, detail: str, returncode: int = -1):
37+
self.returncode: int = returncode
38+
detail = detail.strip()
39+
super().__init__(detail)
40+
41+
class UnixSocketFileNotFoundError(Exception):
42+
43+
def __init__(self, detail: str, returncode: int = -1):
44+
self.returncode: int = returncode
45+
detail = detail.strip()
46+
super().__init__(detail)

0 commit comments

Comments
 (0)