forked from Tanganelli/CoAPthon3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exampleresources.py
281 lines (211 loc) · 10.6 KB
/
exampleresources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import time
from coapthon import defines
from coapthon.resources.resource import Resource
__author__ = 'Giacomo Tanganelli'
class BasicResource(Resource):
def __init__(self, name="BasicResource", coap_server=None):
super(BasicResource, self).__init__(name, coap_server, visible=True,
observable=True, allow_children=True)
self.payload = "Basic Resource"
self.resource_type = "rt1"
self.content_type = "text/plain"
self.interface_type = "if1"
def render_GET(self, request):
return self
def render_PUT(self, request):
self.edit_resource(request)
return self
def render_POST(self, request):
res = self.init_resource(request, BasicResource())
return res
def render_DELETE(self, request):
return True
class Storage(Resource):
def __init__(self, name="StorageResource", coap_server=None):
super(Storage, self).__init__(name, coap_server, visible=True, observable=True, allow_children=True)
self.payload = "Storage Resource for PUT, POST and DELETE"
def render_GET(self, request):
return self
def render_POST(self, request):
res = self.init_resource(request, BasicResource())
return res
class Child(Resource):
def __init__(self, name="ChildResource", coap_server=None):
super(Child, self).__init__(name, coap_server, visible=True, observable=True, allow_children=True)
self.payload = ""
def render_GET(self, request):
return self
def render_PUT(self, request):
self.payload = request.payload
return self
def render_POST(self, request):
res = BasicResource()
res.location_query = request.uri_query
res.payload = request.payload
return res
def render_DELETE(self, request):
return True
class Separate(Resource):
def __init__(self, name="Separate", coap_server=None):
super(Separate, self).__init__(name, coap_server, visible=True, observable=True, allow_children=True)
self.payload = "Separate"
self.max_age = 60
def render_GET(self, request):
return self, self.render_GET_separate
def render_GET_separate(self, request):
time.sleep(5)
return self
def render_POST(self, request):
return self, self.render_POST_separate
def render_POST_separate(self, request):
self.payload = request.payload
return self
def render_PUT(self, request):
return self, self.render_PUT_separate
def render_PUT_separate(self, request):
self.payload = request.payload
return self
def render_DELETE(self, request):
return self, self.render_DELETE_separate
def render_DELETE_separate(self, request):
return True
class Long(Resource):
def __init__(self, name="Long", coap_server=None):
super(Long, self).__init__(name, coap_server, visible=True, observable=True, allow_children=True)
self.payload = "Long Time"
def render_GET(self, request):
time.sleep(10)
return self
class Big(Resource):
def __init__(self, name="Big", coap_server=None):
super(Big, self).__init__(name, coap_server, visible=True, observable=True, allow_children=True)
self.payload = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sollicitudin fermentum ornare. " \
"Cras accumsan tellus quis dui lacinia eleifend. Proin ultrices rutrum orci vitae luctus. " \
"Nullam malesuada pretium elit, at aliquam odio vehicula in. Etiam nec maximus elit. " \
"Etiam at erat ac ex ornare feugiat. Curabitur sed malesuada orci, id aliquet nunc. Phasellus " \
"nec leo luctus, blandit lorem sit amet, interdum metus. Duis efficitur volutpat magna, ac " \
"ultricies nibh aliquet sit amet. Etiam tempor egestas augue in hendrerit. Nunc eget augue " \
"ultricies, dignissim lacus et, vulputate dolor. Nulla eros odio, fringilla vel massa ut, " \
"facilisis cursus quam. Fusce faucibus lobortis congue. Fusce consectetur porta neque, id " \
"sollicitudin velit maximus eu. Sed pharetra leo quam, vel finibus turpis cursus ac. " \
"Aenean ac nisi massa. Cras commodo arcu nec ante tristique ullamcorper. Quisque eu hendrerit" \
" urna. Cras fringilla eros ut nunc maximus, non porta nisl mollis. Aliquam in rutrum massa." \
" Praesent tristique turpis dui, at ultricies lorem fermentum at. Vivamus sit amet ornare neque, " \
"a imperdiet nisl. Quisque a iaculis libero, id tempus lacus. Aenean convallis est non justo " \
"consectetur, a hendrerit enim consequat. In accumsan ante a egestas luctus. Etiam quis neque " \
"nec eros vestibulum faucibus. Nunc viverra ipsum lectus, vel scelerisque dui dictum a. Ut orci " \
"enim, ultrices a ultrices nec, pharetra in quam. Donec accumsan sit amet eros eget fermentum." \
"Vivamus ut odio ac odio malesuada accumsan. Aenean vehicula diam at tempus ornare. Phasellus " \
"dictum mauris a mi consequat, vitae mattis nulla fringilla. Ut laoreet tellus in nisl efficitur," \
" a luctus justo tempus. Fusce finibus libero eget velit finibus iaculis. Morbi rhoncus purus " \
"vel vestibulum ullamcorper. Sed ac metus in urna fermentum feugiat. Nulla nunc diam, sodales " \
"aliquam mi id, varius porta nisl. Praesent vel nibh ac turpis rutrum laoreet at non odio. " \
"Phasellus ut posuere mi. Suspendisse malesuada velit nec mauris convallis porta. Vivamus " \
"sed ultrices sapien, at cras amet."
def render_GET(self, request):
return self
def render_POST(self, request):
if request.payload is not None:
self.payload = request.payload
return self
class voidResource(Resource):
def __init__(self, name="Void"):
super(voidResource, self).__init__(name)
class XMLResource(Resource):
def __init__(self, name="XML"):
super(XMLResource, self).__init__(name)
self.value = 0
self.payload = (defines.Content_types["application/xml"], "<value>"+str(self.value)+"</value>")
def render_GET(self, request):
return self
class MultipleEncodingResource(Resource):
def __init__(self, name="MultipleEncoding"):
super(MultipleEncodingResource, self).__init__(name)
self.value = 0
self.payload = str(self.value)
self.content_type = [defines.Content_types["application/xml"], defines.Content_types["application/json"]]
def render_GET(self, request):
if request.accept == defines.Content_types["application/xml"]:
self.payload = (defines.Content_types["application/xml"], "<value>"+str(self.value)+"</value>")
elif request.accept == defines.Content_types["application/json"]:
self.payload = (defines.Content_types["application/json"], "{'value': '"+str(self.value)+"'}")
elif request.accept == defines.Content_types["text/plain"]:
self.payload = (defines.Content_types["text/plain"], str(self.value))
return self
def render_PUT(self, request):
self.edit_resource(request)
return self
def render_POST(self, request):
res = self.init_resource(request, MultipleEncodingResource())
return res
class ETAGResource(Resource):
def __init__(self, name="ETag"):
super(ETAGResource, self).__init__(name)
self.count = 0
self.payload = "ETag resource"
self.etag = str(self.count)
def render_GET(self, request):
return self
def render_POST(self, request):
self.payload = request.payload
self.count += 1
self.etag = str(self.count)
return self
def render_PUT(self, request):
self.payload = request.payload
return self
class AdvancedResource(Resource):
def __init__(self, name="Advanced"):
super(AdvancedResource, self).__init__(name)
self.payload = "Advanced resource"
def render_GET_advanced(self, request, response):
response.payload = self.payload
response.max_age = 20
response.code = defines.Codes.CONTENT.number
return self, response
def render_POST_advanced(self, request, response):
self.payload = request.payload
from coapthon.messages.response import Response
assert(isinstance(response, Response))
response.payload = "Response changed through POST"
response.code = defines.Codes.CREATED.number
return self, response
def render_PUT_advanced(self, request, response):
self.payload = request.payload
from coapthon.messages.response import Response
assert(isinstance(response, Response))
response.payload = "Response changed through PUT"
response.code = defines.Codes.CHANGED.number
return self, response
def render_DELETE_advanced(self, request, response):
response.payload = "Response deleted"
response.code = defines.Codes.DELETED.number
return True, response
class AdvancedResourceSeparate(Resource):
def __init__(self, name="Advanced"):
super(AdvancedResourceSeparate, self).__init__(name)
self.payload = "Advanced resource"
def render_GET_advanced(self, request, response):
return self, response, self.render_GET_separate
def render_POST_advanced(self, request, response):
return self, response, self.render_POST_separate
def render_PUT_advanced(self, request, response):
return self, response, self.render_PUT_separate
def render_DELETE_advanced(self, request, response):
return self, response, self.render_DELETE_separate
def render_GET_separate(self, request, response):
time.sleep(5)
response.payload = self.payload
response.max_age = 20
return self, response
def render_POST_separate(self, request, response):
self.payload = request.payload
response.payload = "Response changed through POST"
return self, response
def render_PUT_separate(self, request, response):
self.payload = request.payload
response.payload = "Response changed through PUT"
return self, response
def render_DELETE_separate(self, request, response):
response.payload = "Response deleted"
return True, response