Skip to content

Commit 0cba789

Browse files
authored
Merge pull request #3 from PTCInc/rlabbeptc/issue2
Multistatus response during add
2 parents 608b450 + 9f46d6a commit 0cba789

20 files changed

Lines changed: 557 additions & 107 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Kepware Configuration API SDK for Python
22

3-
This is a package SDK to create Python modules to conduct operations with the Kepware Configuration API. THis package is designed to work with all versions of Kepware that support the Configuration API including Thingworx Kepware Server (TKS), Thingworx Kepware Edge (TKE) and KEPServerEX (KEP).
3+
This is a package SDK to create Python modules to conduct operations with the Kepware Configuration API. This package is designed to work with all versions of Kepware that support the Configuration API including Thingworx Kepware Server (TKS), Thingworx Kepware Edge (TKE) and KEPServerEX (KEP).
44

55
## Prerequisites
66

32.3 KB
Binary file not shown.

dist/kepconfig-1.0b4.tar.gz

17.8 KB
Binary file not shown.

kepconfig/admin/ua_server.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
r""":mod:`ua_server` exposes an API to allow modifications (add, delete, modify) to
88
OPC UA Server endpoints within the Kepware Administration through the Kepware Configuration API
99
"""
10+
from typing import Union
11+
12+
1013
UA_ROOT = '/admin/ua_endpoints'
1114

1215
def _create_url(endpoint = None):
@@ -21,7 +24,7 @@ def _create_url(endpoint = None):
2124
else:
2225
return '{}/{}'.format(UA_ROOT,endpoint)
2326

24-
def add_endpoint(server, DATA):
27+
def add_endpoint(server, DATA) -> Union[bool, list]:
2528
'''Add an "endpoint" or multiple "endpoint" objects to Kepware UA Server by passing a
2629
list of endpoints to be added all at once.
2730
@@ -34,13 +37,24 @@ def add_endpoint(server, DATA):
3437
RETURNS:
3538
True - If a "HTTP 201 - Created" is received from Kepware
3639
40+
List - If a "HTTP 207 - Multi-Status" is received from Kepware with a list of dict error responses for all
41+
endpoints added that failed.
42+
43+
False - If a non-expected "2xx successful" code is returned
44+
3745
EXCEPTIONS:
3846
KepHTTPError - If urllib provides an HTTPError
3947
KepURLError - If urllib provides an URLError
4048
'''
4149

4250
r = server._config_add(server.url + _create_url(), DATA)
43-
if r.code == 201: return True
51+
if r.code == 201: return True
52+
elif r.code == 207:
53+
errors = []
54+
for item in r.payload:
55+
if item['code'] != 201:
56+
errors.append(item)
57+
return errors
4458
else: return False
4559

4660
def del_endpoint(server, endpoint):

kepconfig/admin/user_groups.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
r""":mod:`user_groups` exposes an API to allow modifications (add, delete, modify) to
88
user_groups within the Kepware Administration User Manager through the Kepware Configuration API
99
"""
10+
from typing import Union
11+
12+
1013
USERGROUPS_ROOT = '/admin/server_usergroups'
1114
ENABLE_PROPERTY = 'libadminsettings.USERMANAGER_GROUP_ENABLED'
1215

@@ -22,7 +25,7 @@ def _create_url(user_group = None):
2225
else:
2326
return '{}/{}'.format(USERGROUPS_ROOT,user_group)
2427

25-
def add_user_group(server, DATA):
28+
def add_user_group(server, DATA) -> Union[bool, list]:
2629
'''Add a "user_group" or multiple "user_group" objects to Kepware User Manager by passing a
2730
list of user groups to be added all at once.
2831
@@ -35,13 +38,24 @@ def add_user_group(server, DATA):
3538
RETURNS:
3639
True - If a "HTTP 201 - Created" is received from Kepware
3740
41+
List - If a "HTTP 207 - Multi-Status" is received from Kepware with a list of dict error responses for all
42+
user groups added that failed.
43+
44+
False - If a non-expected "2xx successful" code is returned
45+
3846
EXCEPTIONS:
3947
KepHTTPError - If urllib provides an HTTPError
4048
KepURLError - If urllib provides an URLError
4149
'''
4250

4351
r = server._config_add(server.url + _create_url(), DATA)
4452
if r.code == 201: return True
53+
elif r.code == 207:
54+
errors = []
55+
for item in r.payload:
56+
if item['code'] != 201:
57+
errors.append(item)
58+
return errors
4559
else: return False
4660

4761
def del_user_group(server, user_group):

kepconfig/admin/users.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
r""":mod:`users` exposes an API to allow modifications (add, delete, modify) to
88
users within the Kepware Administration User Management through the Kepware Configuration API
99
"""
10+
from typing import Union
11+
12+
1013
USERS_ROOT = '/admin/server_users'
1114
ENABLE_PROPERTY = 'libadminsettings.USERMANAGER_USER_ENABLED'
1215

@@ -22,7 +25,7 @@ def _create_url(user = None):
2225
else:
2326
return '{}/{}'.format(USERS_ROOT,user)
2427

25-
def add_user(server, DATA):
28+
def add_user(server, DATA) -> Union[bool, list]:
2629
'''Add a "user" or multiple "user" objects to Kepware User Manager by passing a
2730
list of users to be added all at once.
2831
@@ -35,13 +38,24 @@ def add_user(server, DATA):
3538
RETURNS:
3639
True - If a "HTTP 201 - Created" is received from Kepware
3740
41+
List - If a "HTTP 207 - Multi-Status" is received from Kepware with a list of dict error responses for all
42+
users added that failed.
43+
44+
False - If a non-expected "2xx successful" code is returned
45+
3846
EXCEPTIONS:
3947
KepHTTPError - If urllib provides an HTTPError
4048
KepURLError - If urllib provides an URLError
4149
'''
4250

4351
r = server._config_add(server.url + _create_url(), DATA)
4452
if r.code == 201: return True
53+
elif r.code == 207:
54+
errors = []
55+
for item in r.payload:
56+
if item['code'] != 201:
57+
errors.append(item)
58+
return errors
4559
else: return False
4660

4761
def del_user(server, user):

kepconfig/connection.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,6 @@ def __connect(self,request_obj):
272272
data.code = server.code
273273
data.reason = server.reason
274274
return data
275-
# if request_obj.method == 'GET':
276-
# data.payload = json.loads(codecs.decode(server.read(),'utf-8-sig'))
277-
# data.code = server.code
278-
# data.reason = server.reason
279-
# return data
280-
# else:
281-
# return server
282-
# return 'HTTP Code: {} - {}'.format(server.code,server.reason)
283275
except error.HTTPError as err:
284276
payload = json.loads(codecs.decode(err.read(),'utf-8-sig'))
285277
# print('HTTP Code: {}\n{}'.format(err.code,payload), file=sys.stderr)
@@ -289,7 +281,7 @@ def __connect(self,request_obj):
289281
raise KepError.KepURLError(err.reason, request_obj.get_full_url())
290282

291283
# Fucntion used to ensure special characters are handled in the URL
292-
# Ex. = Space will be turned to %20
284+
# Ex: Space will be turned to %20
293285
def __url_validate(self, url):
294286
parsed_url = parse.urlparse(url)
295287
updated_path = parse.quote(parsed_url.path)

kepconfig/connectivity/channel.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212

1313
import inspect
14+
from typing import Union
1415

1516
CHANNEL_ROOT = '/project/channels'
1617

@@ -26,7 +27,7 @@ def _create_url(channel = None):
2627
else:
2728
return '{}/{}'.format(CHANNEL_ROOT,channel)
2829

29-
def add_channel(server, DATA):
30+
def add_channel(server, DATA) -> Union[bool, list]:
3031
'''Add a "channel" or multiple "channel" objects to Kepware. Can be used to pass children of a channel object
3132
such as devices and tags/tag groups. This allows you to create a channel, it's devices and tags
3233
all in one function, if desired.
@@ -41,14 +42,26 @@ def add_channel(server, DATA):
4142
4243
RETURNS:
4344
True - If a "HTTP 201 - Created" is received from Kepware
45+
46+
List - If a "HTTP 207 - Multi-Status" is received from Kepware with a list of dict error responses for all
47+
channels added that failed.
48+
49+
False - If a non-expected "2xx successful" code is returned
50+
4451
4552
EXCEPTIONS:
4653
KepHTTPError - If urllib provides an HTTPError
4754
KepURLError - If urllib provides an URLError
4855
'''
4956

5057
r = server._config_add(server.url + _create_url(), DATA)
51-
if r.code == 201: return True
58+
if r.code == 201: return True
59+
elif r.code == 207:
60+
errors = []
61+
for item in r.payload:
62+
if item['code'] != 201:
63+
errors.append(item)
64+
return errors
5265
else: return False
5366

5467
def del_channel(server, channel):

kepconfig/connectivity/device.py

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

12+
from typing import Union
1213
import kepconfig
1314
from . import channel
1415
import inspect
@@ -27,7 +28,7 @@ def _create_url(device = None):
2728
else:
2829
return '{}/{}'.format(DEVICE_ROOT,device)
2930

30-
def add_device(server, dev_channel, DATA):
31+
def add_device(server, dev_channel, DATA) -> Union[bool, list]:
3132
'''Add a "device" object to a channel in Kepware. Can be used to pass children of a device object
3233
such as tags and tag groups. This allows you to create a device and tags
3334
all in one function, if desired.
@@ -44,14 +45,25 @@ def add_device(server, dev_channel, DATA):
4445
4546
RETURNS:
4647
True - If a "HTTP 201 - Created" is received from Kepware
48+
49+
List - If a "HTTP 207 - Multi-Status" is received from Kepware with a list of dict error responses for all
50+
devices added that failed.
51+
52+
False - If a non-expected "2xx successful" code is returned
4753
4854
EXCEPTIONS:
4955
KepHTTPError - If urllib provides an HTTPError
5056
KepURLError - If urllib provides an URLError
5157
'''
5258

5359
r = server._config_add(server.url + channel._create_url(dev_channel) + _create_url(), DATA)
54-
if r.code == 201: return True
60+
if r.code == 201: return True
61+
elif r.code == 207:
62+
errors = []
63+
for item in r.payload:
64+
if item['code'] != 201:
65+
errors.append(item)
66+
return errors
5567
else: return False
5668

5769
def del_device(server, device_path):

0 commit comments

Comments
 (0)