forked from Tanganelli/CoAPthon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_resources.py
53 lines (37 loc) · 1.7 KB
/
example_resources.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
import time
from coapthon2.resources.resource import Resource
__author__ = 'Giacomo Tanganelli'
__version__ = "2.0"
class Storage(Resource):
def __init__(self, name="StorageResource"):
super(Storage, self).__init__(name, visible=True, observable=True, allow_children=True)
self.payload = "Storage Resource for PUT, POST and DELETE"
def render_GET(self, request, query=None):
return self.payload
def render_POST(self, request, payload=None, query=None):
q = "?" + "&".join(query)
res = Child()
return {"Payload": payload, "Location-Query": q, "Resource": res}
class Child(Resource):
def __init__(self, name="ChildResource"):
super(Child, self).__init__(name, visible=True, observable=True, allow_children=True)
self.payload = ""
def render_GET(self, request, query=None):
return self.payload
def render_PUT(self, request, payload=None, query=None):
return payload
def render_POST(self, request, payload=None, query=None):
q = "?" + "&".join(query)
res = Child()
return {"Payload": payload, "Location-Query": q, "Resource": res}
def render_DELETE(self, request, query=None):
return True
class Separate(Resource):
def __init__(self, name="Separate"):
super(Separate, self).__init__(name, visible=True, observable=True, allow_children=True)
self.payload = "Separate"
def render_GET(self, request, query=None):
return {"Payload": self.payload, "ETag": self.etag, "Separate": True, "Callback": self.render_GET_separate}
def render_GET_separate(self, request, query=None):
time.sleep(5)
return {"Payload": self.payload, "ETag": self.etag}