Skip to content

Commit 6196123

Browse files
authored
Merge pull request #22 from adator85/V1.x.x
Moving to 1.1.0 version.
2 parents 3bfce33 + 393a58f commit 6196123

20 files changed

+566
-539
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ tests/*
77
docs/*
88
setup.py
99
test.py
10+
test_live.py
1011
CallbackObject.py
1112
*.zip

README.MD

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ If you are using Python3, this package can help you to parse all json responses
1818
> I recommend installing a virtual environment and then installing the package within it.
1919
2020
## How to establish the link
21+
### Using requests module
2122
```python
2223
from unrealircd_rpc_py.Loader import Loader
2324
# Using requests method
@@ -27,30 +28,41 @@ If you are using Python3, this package can help you to parse all json responses
2728
username='apiname',
2829
password='apiPASSWORD'
2930
)
30-
31+
```
32+
### Using socket method
33+
```python
34+
from unrealircd_rpc_py.Loader import Loader
3135
# Using socket method
3236
rpc = Loader(
3337
req_method='socket',
3438
url='https://your.irc.domaine.org:8600/api',
3539
username='apiname',
3640
password='apiPASSWORD'
3741
)
38-
42+
```
43+
### Using unixsocket method (Local only)
44+
```python
45+
from unrealircd_rpc_py.Loader import Loader
3946
# Using unixsocket method (Local only)
4047
rpc = Loader(
4148
req_method='unixsocket',
4249
path_to_socket_file='/path/to/unrealircd/data/rpc.socket'
4350
)
44-
51+
```
52+
### Live Connection (Local Only)
53+
```python
4554
from unrealircd_rpc_py.Live import Live
46-
# Live Connection (Local only)
55+
# Live Connection (Local only) using unix socket
4756
LiveRpc = Live(
4857
req_method='unixsocket',
4958
path_to_socket_file='/path/to/unrealircd/data/rpc.socket',
5059
callback_object_instance=Callback_class_instance,
5160
callback_method_name='your_method_name'
5261
)
53-
62+
```
63+
### Live connection (Local or Remote)
64+
```python
65+
from unrealircd_rpc_py.Live import Live
5466
# Live Connection (Local Or Remote)
5567
liveRpc = Live(
5668
req_method='websocket',
@@ -60,7 +72,6 @@ If you are using Python3, this package can help you to parse all json responses
6072
callback_object_instance=InitCallbackClass,
6173
callback_method_name='your_method_name'
6274
)
63-
6475
```
6576
## How to work with (remotly)
6677
This package allows easy interfacing with UnrealIRCd through regular Python3 code, such as:
@@ -107,9 +118,17 @@ This package allows easy interfacing with UnrealIRCd through regular Python3 cod
107118
)
108119

109120
# Enjoy the power of JSON-RPC
110-
111121
User = rpc.User
112122
response = User.get('adator')
123+
124+
# Handling errors
125+
if rpc.get_error.code != 0:
126+
# if code is not 0 then there is an error
127+
print(f"Your Error Message: {rpc.get_error.message}")
128+
129+
# You can also access errors like so:
130+
if User.get_error.code != 0:
131+
print(f"Your Error Message: {User.get_error.message}")
113132

114133
print(f'Nickname: {response.name}')
115134
print(f'Ip: {response.ip}')
@@ -166,12 +185,12 @@ This package allows easy interfacing with UnrealIRCd through regular Python3 cod
166185

167186
# This method will run forever and will send to your callback method the response
168187
# in SimpleNameSpace type that you can parse
169-
if liveRpc.Error.code == 0:
188+
if liveRpc.get_error.code == 0:
170189
# Subscribe to live events
171190
liveRpc.subscribe()
172191
else:
173192
# If error show the error message
174-
print(liveRpc.Error.message)
193+
print(liveRpc.get_error.message)
175194
```
176195
## How to work with (using Live websocket)
177196
```python
@@ -193,12 +212,12 @@ This package allows easy interfacing with UnrealIRCd through regular Python3 cod
193212

194213
# This method will run forever and will send to your callback method the response
195214
# in SimpleNameSpace type that you can parse
196-
if liveRpc.Error.code == 0:
215+
if liveRpc.get_error.code == 0:
197216
# Subscribe to live events
198217
liveRpc.subscribe()
199218
else:
200219
# If error show the error message
201-
print(liveRpc.Error.message)
220+
print(liveRpc.get_error.message)
202221
```
203222

204223
## Exemple of a Callback Class

how_to_use_it.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@
3535
print(mod.name)
3636

3737
except AttributeError as ae:
38-
rpc.Connection.Logs.error(ae)
38+
print(ae)

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

unrealircd_rpc_py/Channel.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Channel:
1010
def __init__(self, connection: Connection) -> None:
1111

1212
# Store the original response
13-
self.response_raw: str
13+
self.response_raw: str = ''
1414
"""Original response used to see available keys."""
1515

1616
self.response_np: SimpleNamespace
@@ -25,6 +25,14 @@ def __init__(self, connection: Connection) -> None:
2525
def get_error(self) -> Dfn.RPCError:
2626
return self.Error
2727

28+
@property
29+
def get_response(self) -> Union[dict, None]:
30+
return self.Connection.get_response()
31+
32+
@property
33+
def get_response_np(self) -> Union[SimpleNamespace, None]:
34+
return self.Connection.get_response_np()
35+
2836
def list_(self, object_detail_level: Literal[0, 1, 2, 3, 4] = 1) -> list[Dfn.Channel]:
2937
"""List channels.
3038
@@ -42,10 +50,7 @@ def list_(self, object_detail_level: Literal[0, 1, 2, 3, 4] = 1) -> list[Dfn.Cha
4250
self.DB_CHANNELS = []
4351
self.Connection.EngineError.init_error()
4452

45-
response = self.Connection.query(method='channel.list', param={'object_detail_level': object_detail_level})
46-
47-
self.response_raw = response
48-
self.response_np = self.Connection.json_response_np
53+
response:dict[str, dict] = self.Connection.query(method='channel.list', param={'object_detail_level': object_detail_level})
4954

5055
if response is None:
5156
self.Logs.error('Empty response')
@@ -100,9 +105,6 @@ def get(self, channel: str, object_detail_level: int = 3) -> Union[Dfn.Channel,
100105

101106
response: dict[str, dict] = self.Connection.query(method='channel.get', param={'channel': channel, 'object_detail_level': object_detail_level})
102107

103-
self.response_raw = response
104-
self.response_np = self.Connection.json_response_np
105-
106108
if response is None:
107109
self.Logs.error('Empty response')
108110
self.Connection.EngineError.set_error(code=-2, message='Empty response')
@@ -146,7 +148,7 @@ def get(self, channel: str, object_detail_level: int = 3) -> Union[Dfn.Channel,
146148

147149
db_members.append(member_obj)
148150

149-
objectChannel = Dfn.Channel(
151+
channel_obj = Dfn.Channel(
150152
**channel_copy,
151153
bans=[Dfn.ChannelBans(**ban) for ban in channel.get('bans', [])],
152154
ban_exemptions=[Dfn.ChannelBanExemptions(**ban_ex) for ban_ex in channel.get('ban_exemptions', [])],
@@ -155,7 +157,7 @@ def get(self, channel: str, object_detail_level: int = 3) -> Union[Dfn.Channel,
155157
members=db_members
156158
)
157159

158-
return objectChannel
160+
return channel_obj
159161

160162
except KeyError as ke:
161163
self.Logs.error(f'KeyError: {ke}')
@@ -178,10 +180,7 @@ def set_mode(self, channel: str, modes: str, parameters: str = "") -> bool:
178180
try:
179181
self.Connection.EngineError.init_error()
180182

181-
response = self.Connection.query(method='channel.set_mode', param={"channel": channel,"modes": modes,"parameters": parameters})
182-
183-
self.response_raw = response
184-
self.response_np = self.Connection.json_response_np
183+
response:dict[str, dict] = self.Connection.query(method='channel.set_mode', param={"channel": channel,"modes": modes,"parameters": parameters})
185184

186185
if response is None:
187186
self.Logs.error('Empty response')
@@ -202,8 +201,10 @@ def set_mode(self, channel: str, modes: str, parameters: str = "") -> bool:
202201

203202
except KeyError as ke:
204203
self.Logs.error(f'KeyError: {ke}')
204+
return False
205205
except Exception as err:
206206
self.Logs.error(f'General error: {err}')
207+
return False
207208

208209
def set_topic(self, channel: str, topic: str, set_by: str = None, set_at: str = None) -> bool:
209210
"""Set a topic on a channel.
@@ -220,10 +221,7 @@ def set_topic(self, channel: str, topic: str, set_by: str = None, set_at: str =
220221
try:
221222
self.Connection.EngineError.init_error()
222223

223-
response = self.Connection.query(method='channel.set_topic', param={"channel": channel, "topic": topic, "set_by": set_by, "set_at": set_at})
224-
225-
self.response_raw = response
226-
self.response_np = self.Connection.json_response_np
224+
response:dict[str, dict] = self.Connection.query(method='channel.set_topic', param={"channel": channel, "topic": topic, "set_by": set_by, "set_at": set_at})
227225

228226
if response is None:
229227
self.Logs.error('Empty response')
@@ -244,8 +242,10 @@ def set_topic(self, channel: str, topic: str, set_by: str = None, set_at: str =
244242

245243
except KeyError as ke:
246244
self.Logs.error(f'KeyError: {ke}')
245+
return False
247246
except Exception as err:
248247
self.Logs.error(f'General error: {err}')
248+
return False
249249

250250
def kick(self, channel: str, nick: str, reason: str) -> bool:
251251
"""Kick a user from a channel
@@ -261,10 +261,7 @@ def kick(self, channel: str, nick: str, reason: str) -> bool:
261261
try:
262262
self.Connection.EngineError.init_error()
263263

264-
response = self.Connection.query(method='channel.kick', param={"channel": channel, "nick": nick, "reason": reason})
265-
266-
self.response_raw = response
267-
self.response_np = self.Connection.json_response_np
264+
response:dict[str, dict] = self.Connection.query(method='channel.kick', param={"channel": channel, "nick": nick, "reason": reason})
268265

269266
if response is None:
270267
self.Logs.error('Empty response')
@@ -285,5 +282,7 @@ def kick(self, channel: str, nick: str, reason: str) -> bool:
285282

286283
except KeyError as ke:
287284
self.Logs.error(f'KeyError: {ke}')
285+
return False
288286
except Exception as err:
289-
self.Logs.error(f'General error: {err}')
287+
self.Logs.error(f'General error: {err}')
288+
return False

0 commit comments

Comments
 (0)