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=(
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
85 def child_resource(response):
86 """Return child resource."""
87 return ciotdm.childResource(response)
90 def child_resource_first(response):
91 """Return child resource on top of dictionary."""
92 return ciotdm.childResourceFirst(response)
96 """Return resource ID."""
97 return ciotdm.resid(response)
100 def parent_id(response):
101 """Return parent ID."""
102 return ciotdm.parent(response)
106 """Return resourceName."""
107 resource_name = ciotdm.name(response)
108 if resource_name is None:
109 raise AssertionError('Cannot find this resource')
114 """Return whole resource in text."""
118 def last_modified_time(response):
119 """Return resource lastModifiedTime."""
120 return ciotdm.lastModifiedTime(response)
123 def state_tag(response):
124 """Return resource state tag."""
125 return ciotdm.stateTag(response)
128 def current_number_of_instances(response):
129 """Return current number of instances."""
130 return ciotdm.currentNumberOfInstances(response)
133 def current_byte_size(response):
134 """Return current byte size."""
135 return ciotdm.currentByteSize(response)
138 def max_number_of_instances(response):
139 """Return max number of instances."""
140 return ciotdm.maxNumberOfInstances(response)
143 def content(response):
144 """Return content child from response."""
145 return ciotdm.content(response)
148 def max_byte_size(response):
149 """Return max byte size."""
150 return ciotdm.maxByteSize(response)
153 def status_code(response):
154 """Return resource status_code."""
155 return response.status_code
159 """Return resource in json format."""
160 return response.json()
163 def elapsed(response):
164 """Return resource elapsed."""
165 return response.elapsed.total_seconds()
168 def location(response):
169 """Return response content-location."""
170 return response.headers['Content-Location']
173 def kill_the_tree(host, cseid, username, password):
174 """Delete the whole tree."""
175 connection = ciotdm.connect(host, base=cseid,
176 auth=(username, password), protocol="http")
180 def check_response(response, operation):
181 """Check whether the connection is none."""
183 raise AssertionError('Cannot %s this resource') % (operation)
184 elif hasattr(response, 'status_code'):
185 if response.status_code < 200 or response.status_code > 299:
186 raise AssertionError(
187 'Cannot %s this resource [%d] : %s' %
188 (operation, response.status_code, response.text))