Skip to content

Commit 4a16355

Browse files
committed
Updates to support query options for collections
1 parent c4e1e59 commit 4a16355

25 files changed

Lines changed: 362 additions & 187 deletions

kepconfig/admin/ua_server.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010
from typing import Union
1111
from ..error import KepHTTPError, KepError
12+
from ..connection import server
1213

1314

1415
UA_ROOT = '/admin/ua_endpoints'
@@ -132,11 +133,14 @@ def get_endpoint(server, endpoint) -> dict:
132133
r = server._config_get(server.url + _create_url(endpoint))
133134
return r.payload
134135

135-
def get_all_endpoints(server) -> list:
136+
def get_all_endpoints(server: server, *, options: dict = None) -> list:
136137
'''Returns list of all endpoint objects and their properties. Returned object is JSON list.
137138
138139
INPUTS:
139-
"server" - instance of the "server" class
140+
server - instance of the "server" class
141+
142+
options - (optional) Dict of parameters to filter, sort or pagenate the list of UA endpoints. Options are 'filter',
143+
'sortOrder', 'sortProperty', 'pageNumber', and 'pageSize'.
140144
141145
RETURNS:
142146
list - data for all endpoints requested
@@ -146,5 +150,5 @@ def get_all_endpoints(server) -> list:
146150
KepURLError - If urllib provides an URLError
147151
'''
148152

149-
r = server._config_get(server.url + _create_url())
153+
r = server._config_get(f'{server.url}{_create_url()}', params= options)
150154
return r.payload

kepconfig/admin/user_groups.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010
from typing import Union
1111
from ..error import KepHTTPError, KepError
12+
from ..connection import server
1213

1314

1415
USERGROUPS_ROOT = '/admin/server_usergroups'
@@ -132,12 +133,15 @@ def get_user_group(server, user_group) -> dict:
132133
r = server._config_get(server.url + _create_url(user_group))
133134
return r.payload
134135

135-
def get_all_user_groups(server) -> list:
136+
def get_all_user_groups(server: server, *, options: dict = None) -> list:
136137
'''Returns list of all user_group objects and their properties. Returned object is JSON list.
137138
138139
INPUTS:
139-
"server" - instance of the "server" class
140+
server - instance of the "server" class
140141
142+
options - (optional) Dict of parameters to filter, sort or pagenate the list of user groups. Options are 'filter',
143+
'sortOrder', 'sortProperty', 'pageNumber', and 'pageSize'.
144+
141145
RETURNS:
142146
list - data for all user_groups requested
143147
@@ -146,7 +150,7 @@ def get_all_user_groups(server) -> list:
146150
KepURLError - If urllib provides an URLError
147151
'''
148152

149-
r = server._config_get(server.url + _create_url())
153+
r = server._config_get(f'{server.url}{_create_url()}', params= options)
150154
return r.payload
151155

152156
def enable_user_group(server, user_group) -> bool:

kepconfig/admin/users.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010
from typing import Union
1111
from ..error import KepError, KepHTTPError
12+
from ..connection import server
1213

1314

1415
USERS_ROOT = '/admin/server_users'
@@ -132,11 +133,14 @@ def get_user(server, user) -> dict:
132133
r = server._config_get(server.url + _create_url(user))
133134
return r.payload
134135

135-
def get_all_users(server) -> list:
136+
def get_all_users(server: server, *, options: dict = None) -> list:
136137
'''Returns list of all user objects and their properties. Returned object is JSON list.
137138
138139
INPUTS:
139-
"server" - instance of the "server" class
140+
server - instance of the "server" class
141+
142+
options - (optional) Dict of parameters to filter, sort or pagenate the list of users. Options are 'filter',
143+
'sortOrder', 'sortProperty', 'pageNumber', and 'pageSize'.
140144
141145
RETURNS:
142146
list - data for all users requested
@@ -146,7 +150,7 @@ def get_all_users(server) -> list:
146150
KepURLError - If urllib provides an URLError
147151
'''
148152

149-
r = server._config_get(server.url + _create_url())
153+
r = server._config_get(f'{server.url}{_create_url()}', params= options)
150154
return r.payload
151155

152156
def enable_user(server, user) -> bool:

kepconfig/connection.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from .error import KepError, KepHTTPError, KepURLError
1717
import socket
1818
import ssl
19-
import sys
2019

2120
class KepServiceResponse:
2221
'''A class to represent a return object when calling a "service" API of Kepware. This is
@@ -82,7 +81,7 @@ class server:
8281
Methods:
8382
8483
"reinitialize()" - reinitialize the Kepware server
85-
"get_trans_log()" - retrieve the Configuration API transaction logs
84+
"get_transaction_log()" - retrieve the Configuration API transaction logs
8685
"get_event_log()" - retrieve the Kepware Event Log
8786
"get_project_properties()" - retrieve the Kepware Project Properties
8887
"modify_project_properties()" - modify the Kepware Project Properties
@@ -206,32 +205,38 @@ def reinitialize(self, job_ttl = None) -> KepServiceResponse:
206205
except Exception as err:
207206
raise err
208207

209-
def get_trans_log(self, start = None, end = None, limit = None) -> list:
208+
def get_transaction_log(self, limit: int = None, start: datetime.datetime = None, end: datetime.datetime = None) -> list:
210209
''' Get the Transaction Log from the Kepware instance.
211210
212-
"start" (optional) - datetime.datetime type and should be UTC
211+
limit (optional) - number of event log entries to request
212+
213+
start (optional) - datetime.datetime type and should be UTC
213214
214-
"end" (optional) - datetime.datetime type and should be UTC
215+
end (optional) - datetime.datetime type and should be UTC
215216
216-
"limit" (optional) - number of event log entries to request
217217
'''
218218
query = self.__create_query(start, end, limit)
219-
url = self.url + self.__trans_log_url + '?' + parse.urlencode(query)
220-
r = self._config_get(url)
219+
url = f'{self.url}{self.__trans_log_url}'
220+
r = self._config_get(url, params= query)
221221
return r.payload
222222

223-
def get_event_log(self, limit = None, start = None, end = None) -> list:
223+
def get_event_log(self, limit: int = None, start: datetime.datetime = None, end: datetime.datetime = None, *, options: dict = None) -> list:
224224
''' Get the Event Log from the Kepware instance.
225225
226-
"start" (optional) - datetime.datetime type and should be UTC
226+
start (optional) - datetime.datetime type and should be UTC
227+
228+
end (optional) - datetime.datetime type and should be UTC
227229
228-
"end" (optional) - datetime.datetime type and should be UTC
230+
limit (optional) - number of event log entries to request
229231
230-
"limit" (optional) - number of event log entries to request
232+
options - (optional) Dict of parameters to filter, sort or pagenate the list of transactions. Options are 'event',
233+
'sortOrder', 'sortProperty', 'pageNumber', and 'pageSize'
231234
'''
232235
query = self.__create_query(start, end, limit)
233-
url = self.url + self.__event_log_url + '?' + parse.urlencode(query)
234-
r = self._config_get(url)
236+
if options is not None:
237+
query = {**query, **options}
238+
url = f'{self.url}{self.__event_log_url}'
239+
r = self._config_get(url, params= query)
235240
return r.payload
236241

237242
def get_project_properties(self) -> dict:
@@ -399,8 +404,15 @@ def _config_update(self, url, DATA = None):
399404
return r
400405

401406
#Function used to Read an object from Kepware (HTTP GET) and return the JSON response
402-
def _config_get(self, url):
403-
'''Conducts an GET method at *url* to retrieve an objects properties in the Kepware Configuration.'''
407+
def _config_get(self, url, *, params = None):
408+
'''
409+
Conducts an GET method at *url* to retrieve an objects properties with query parameters in
410+
the Kepware Configuration.
411+
'''
412+
# Add parameters when necessary
413+
if params is not None and params != {}:
414+
qparams = parse.urlencode(params)
415+
url = f'{url}?{qparams}'
404416
url_obj = self.__url_validate(url)
405417
q = request.Request(url_obj, method='GET')
406418
r = self.__connect(q)

kepconfig/connectivity/channel.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111

1212

1313
import inspect
14+
from ..connection import server
1415
from ..error import KepHTTPError, KepError
1516
from typing import Union
16-
from. import device
17+
from . import device
1718

1819
CHANNEL_ROOT = '/project/channels'
1920

@@ -29,7 +30,7 @@ def _create_url(channel = None):
2930
else:
3031
return '{}/{}'.format(CHANNEL_ROOT,channel)
3132

32-
def add_channel(server, DATA) -> Union[bool, list]:
33+
def add_channel(server: server, DATA) -> Union[bool, list]:
3334
'''Add a "channel" or multiple "channel" objects to Kepware. Can be used to pass children of a channel object
3435
such as devices and tags/tag groups. This allows you to create a channel, it's devices and tags
3536
all in one function, if desired.
@@ -65,7 +66,7 @@ def add_channel(server, DATA) -> Union[bool, list]:
6566
else:
6667
raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)
6768

68-
def del_channel(server, channel) -> bool:
69+
def del_channel(server: server, channel) -> bool:
6970
'''Delete a "channel" object in Kepware. This will delete all children as well
7071
7172
INPUTS:
@@ -86,7 +87,7 @@ def del_channel(server, channel) -> bool:
8687
else:
8788
raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)
8889

89-
def modify_channel(server, DATA, channel = None, force = False) -> bool:
90+
def modify_channel(server: server, DATA, channel = None, force = False) -> bool:
9091
'''Modify a channel object and it's properties in Kepware. If a "channel" is not provided as an input,
9192
you need to identify the channel in the 'common.ALLTYPES_NAME' property field in the "DATA". It will
9293
assume that is the channel that is to be modified.
@@ -125,7 +126,7 @@ def modify_channel(server, DATA, channel = None, force = False) -> bool:
125126
else:
126127
raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)
127128

128-
def get_channel(server, channel) -> dict:
129+
def get_channel(server: server, channel) -> dict:
129130
'''Returns the properties of the channel object. Returned object is JSON.
130131
131132
INPUTS:
@@ -144,24 +145,30 @@ def get_channel(server, channel) -> dict:
144145
r = server._config_get(server.url + _create_url(channel))
145146
return r.payload
146147

147-
def get_all_channels(server) -> list:
148+
def get_all_channels(server: server, *, options: dict = None) -> list:
148149
'''Returns list of all channel objects and their properties. Returned object is JSON list.
149150
150151
INPUTS:
151-
"server" - instance of the "server" class
152+
153+
server - instance of the "server" class
154+
155+
options - (optional) Dict of parameters to filter, sort or pagenate the list of channels. Options are 'filter',
156+
'sortOrder', 'sortProperty', 'pageNumber', and 'pageSize'
152157
153158
RETURNS:
154159
list - data for the channel requested
155160
156161
EXCEPTIONS:
162+
157163
KepHTTPError - If urllib provides an HTTPError
164+
158165
KepURLError - If urllib provides an URLError
159166
'''
160167

161-
r = server._config_get(server.url + _create_url())
168+
r = server._config_get(server.url + _create_url(), params= options)
162169
return r.payload
163170

164-
def get_channel_structure(server, channel) -> dict:
171+
def get_channel_structure(server: server, channel) -> dict:
165172
'''Returns the properties of "channel" and includes all "devices" and the "tag" and "tag group" objects for a
166173
channel in Kepware. Returned object is a dict of channel properties including a device list with
167174
tag lists and tag group lists.

kepconfig/connectivity/device.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
device objects within the Kepware Configuration API
1010
"""
1111

12-
from ..connection import KepServiceResponse
12+
from ..connection import KepServiceResponse, server
1313
from ..error import KepHTTPError, KepError
1414
from typing import Union
1515
import kepconfig
@@ -30,17 +30,17 @@ def _create_url(device = None):
3030
else:
3131
return '{}/{}'.format(DEVICE_ROOT,device)
3232

33-
def add_device(server, dev_channel, DATA) -> Union[bool, list]:
33+
def add_device(server, channel_name, DATA) -> Union[bool, list]:
3434
'''Add a "device" object to a channel in Kepware. Can be used to pass children of a device object
3535
such as tags and tag groups. This allows you to create a device and tags
3636
all in one function, if desired.
3737
38-
Additionally it can be used to pass a list of channelsdevices and it's children to be added all at once.
38+
Additionally it can be used to pass a list of devices and it's children to be added all at once.
3939
4040
INPUTS:
4141
"server" - instance of the "server" class
4242
43-
"dev_channel" - channel the device object exists
43+
"channel_name" - channel the device object exists
4444
4545
"DATA" - properly JSON object (dict) of the device and it's children
4646
expected by Kepware Configuration API
@@ -56,7 +56,7 @@ def add_device(server, dev_channel, DATA) -> Union[bool, list]:
5656
KepURLError - If urllib provides an URLError
5757
'''
5858

59-
r = server._config_add(server.url + channel._create_url(dev_channel) + _create_url(), DATA)
59+
r = server._config_add(server.url + channel._create_url(channel_name) + _create_url(), DATA)
6060
if r.code == 201: return True
6161
elif r.code == 207:
6262
errors = []
@@ -155,11 +155,14 @@ def get_device(server, device_path) -> dict:
155155
# print('Error: Error with {}: {}'.format(inspect.currentframe().f_code.co_name, str(err)))
156156
# raise err
157157

158-
def get_all_devices(server, dev_channel) -> list:
158+
def get_all_devices(server: server, channel_name: str, *, options: dict = None) -> list:
159159
'''Returns list of all device objects and their properties within a channel. Returned object is JSON list.
160160
161161
INPUTS:
162-
"dev_channel" - channel the device object exists
162+
channel_name - channel the device object exists
163+
164+
options - (optional) Dict of parameters to filter, sort or pagenate the list of devices. Options are 'filter',
165+
'sortOrder', 'sortProperty', 'pageNumber', and 'pageSize'
163166
164167
RETURNS:
165168
list - data for the devices requested
@@ -168,7 +171,7 @@ def get_all_devices(server, dev_channel) -> list:
168171
KepHTTPError - If urllib provides an HTTPError
169172
KepURLError - If urllib provides an URLError
170173
'''
171-
r = server._config_get(server.url + channel._create_url(dev_channel) + _create_url())
174+
r = server._config_get(f'{server.url}{channel._create_url(channel_name)}{_create_url()}', params= options)
172175
return r.payload
173176

174177
def auto_tag_gen(server, device_path, job_ttl = None) -> KepServiceResponse:

0 commit comments

Comments
 (0)