1 """This library should work with ciotdm, both work for iotdm project."""
5 def connect_to_iotdm(host, user, password, prot):
6 """According to protocol, connect to iotdm."""
7 return ciotdm.connect(host, base="InCSE1", auth=(
8 user, password), protocol=prot)
11 def modify_headers_origin(connection, new_origin):
12 """Replace the headers origin with the neworigin to test ACP."""
13 connection.modify_headers_origin(new_origin)
16 def create_resource(connection, parent, restype, attribute=None):
17 """Create resource without command."""
18 connection.create(parent, restype, attribute)
19 check_response(connection.response, "create")
20 return connection.response
23 def create_resource_with_command(connection, parent, restype,
24 command, attribute=None):
25 """According to command in the header, create the resource."""
26 connection.create_with_command(parent, restype,
28 check_response(connection.response, "create")
29 return connection.response
32 def create_subscription(connection, parent, ip, port):
33 """Create subscription."""
34 uri = "http://%s:%d" % (ip, int(port))
35 connection.create(parent, "subscription", {
36 "notificationURI": uri,
37 "notificationContentType": "wholeResource"})
38 check_response(connection.response, "create")
39 return connection.response
42 def retrieve_resource(connection, resid):
43 """Retrieve resource according to resourceID."""
44 connection.retrieve(resid)
45 check_response(connection.response, "retrieve")
46 return connection.response
49 def retrieve_resource_with_command(connection, resid, command):
50 """According to command, retrieve source with the resourceID."""
51 connection.retrieve_with_command(resid, command)
52 check_response(connection.response, "retrieve")
53 return connection.response
56 def update_resource(connection, resid, restype, attr):
57 """According to resourceID, update resource."""
58 connection.update(resid, restype, attr)
59 check_response(connection.response, "update")
60 return connection.response
63 def update_resource_with_command(connection, resid,
64 restype, command, attr):
65 """According to command, update resource with resourceID."""
66 connection.update_with_command(resid, restype, command, attr)
67 check_response(connection.response, "update")
68 return connection.response
71 def delete_resource(connection, resid):
72 """According to resourceID, delete the resource."""
73 connection.delete(resid)
74 check_response(connection.response, "delete")
75 return connection.response
78 def delete_resource_with_command(connection, resid, command):
79 """According to command, delete the resource with resourceID."""
80 connection.delete_with_command(resid, command)
81 check_response(connection.response, "delete")
82 return connection.response
86 """Return resource ID."""
87 return ciotdm.resid(response)
91 """Return resourceName."""
92 resource_name = ciotdm.name(response)
93 if resource_name is None:
94 raise AssertionError('Cannot find this resource')
99 """Return whole resource in text."""
103 def last_modified_time(response):
104 """Return resource lastModifiedTime."""
105 return ciotdm.lastModifiedTime(response)
108 def status_code(response):
109 """Return resource status_code."""
110 return response.status_code
114 """Return resource in json format."""
115 return response.json()
118 def elapsed(response):
119 """Return resource elapsed."""
120 return response.elapsed.total_seconds()
123 def location(response):
124 """Return response content-location."""
125 return response.headers['Content-Location']
128 def kill_the_tree(host, cseid, username, password):
129 """Delete the whole tree."""
130 connection = ciotdm.connect(host, base=cseid,
131 auth=(username, password), protocol="http")
135 def check_response(response, operation):
136 """Check whether the connection is none."""
138 raise AssertionError('Cannot %s this resource') % (operation)
139 elif hasattr(response, 'status_code'):
140 if response.status_code < 200 or response.status_code > 299:
141 raise AssertionError(
142 'Cannot %s this resource [%d] : %s' %
143 (operation, response.status_code, response.text))