1 """This library should work with ciotdm, both work for iotdm project."""
5 def connect_to_iotdm(host, user, password, prot="http"):
6 """According to protocol, connect to iotdm."""
7 return ciotdm.connect(host, base="InCSE1", auth=(user, password), protocol=prot)
10 def modify_headers_origin(connection, new_origin):
11 """Replace the headers origin with the neworigin to test ACP."""
12 connection.modify_headers_origin(new_origin)
15 def create_resource(connection, parent, restype, attribute=None):
16 """Create resource without command."""
17 connection.create(parent, restype, attribute)
18 check_response(connection.response, "create")
19 return connection.response
22 def create_resource_with_command(connection, parent, restype, command, attribute=None):
23 """According to command in the header, create the resource."""
24 connection.create_with_command(parent, restype, command, attribute)
25 check_response(connection.response, "create")
26 return connection.response
29 def create_subscription(connection, parent, ip, port):
30 """Create subscription."""
31 uri = "http://%s:%d" % (ip, int(port))
35 {"notificationURI": uri, "notificationContentType": "wholeResource"},
37 check_response(connection.response, "create")
38 return connection.response
41 def retrieve_resource(connection, resid):
42 """Retrieve resource according to resourceID."""
43 connection.retrieve(resid)
44 check_response(connection.response, "retrieve")
45 return connection.response
48 def retrieve_resource_with_command(connection, resid, command):
49 """According to command, retrieve source with the resourceID."""
50 connection.retrieve_with_command(resid, command)
51 check_response(connection.response, "retrieve")
52 return connection.response
55 def update_resource(connection, resid, restype, attr):
56 """According to resourceID, update resource."""
57 connection.update(resid, restype, attr)
58 check_response(connection.response, "update")
59 return connection.response
62 def update_resource_with_command(connection, resid, restype, command, attr):
63 """According to command, update resource with resourceID."""
64 connection.update_with_command(resid, restype, command, attr)
65 check_response(connection.response, "update")
66 return connection.response
69 def delete_resource(connection, resid):
70 """According to resourceID, delete the resource."""
71 connection.delete(resid)
72 check_response(connection.response, "delete")
73 return connection.response
76 def delete_resource_with_command(connection, resid, command):
77 """According to command, delete the resource with resourceID."""
78 connection.delete_with_command(resid, command)
79 check_response(connection.response, "delete")
80 return connection.response
83 def child_resource(response):
84 """Return child resource."""
85 return ciotdm.childResource(response)
88 def child_resource_first(response):
89 """Return child resource on top of dictionary."""
90 return ciotdm.childResourceFirst(response)
94 """Return resource ID."""
95 return ciotdm.resid(response)
98 def parent_id(response):
99 """Return parent ID."""
100 return ciotdm.parent(response)
104 """Return resourceName."""
105 resource_name = ciotdm.name(response)
106 if resource_name is None:
107 raise AssertionError("Cannot find this resource")
112 """Return whole resource in text."""
116 def last_modified_time(response):
117 """Return resource lastModifiedTime."""
118 return ciotdm.lastModifiedTime(response)
121 def state_tag(response):
122 """Return resource state tag."""
123 return ciotdm.stateTag(response)
126 def current_number_of_instances(response):
127 """Return current number of instances."""
128 return ciotdm.currentNumberOfInstances(response)
131 def current_byte_size(response):
132 """Return current byte size."""
133 return ciotdm.currentByteSize(response)
136 def max_number_of_instances(response):
137 """Return max number of instances."""
138 return ciotdm.maxNumberOfInstances(response)
141 def content(response):
142 """Return content child from response."""
143 return ciotdm.content(response)
146 def max_byte_size(response):
147 """Return max byte size."""
148 return ciotdm.maxByteSize(response)
151 def status_code(response):
152 """Return resource status_code."""
153 return response.status_code
157 """Return resource in json format."""
158 return response.json()
161 def elapsed(response):
162 """Return resource elapsed."""
163 return response.elapsed.total_seconds()
166 def location(response):
167 """Return response content-location."""
168 return response.headers["Content-Location"]
171 def kill_the_tree(host, cseid, username, password):
172 """Delete the whole tree."""
173 connection = ciotdm.connect(
174 host, base=cseid, auth=(username, password), protocol="http"
179 def check_response(response, operation):
180 """Check whether the connection is none."""
182 raise AssertionError("Cannot %s this resource") % (operation)
183 elif hasattr(response, "status_code"):
184 if response.status_code < 200 or response.status_code > 299:
185 raise AssertionError(
186 "Cannot %s this resource [%d] : %s"
187 % (operation, response.status_code, response.text)