|
14 | 14 | import asyncio
|
15 | 15 |
|
16 | 16 | import aiocoap.resource as resource
|
| 17 | +from aiocoap.numbers.contentformat import ContentFormat |
17 | 18 | import aiocoap
|
18 | 19 |
|
| 20 | +class Welcome(resource.Resource): |
| 21 | + representations = { |
| 22 | + ContentFormat.TEXT: b"Welcome to the demo server", |
| 23 | + ContentFormat.LINKFORMAT: b"</.well-known/core>,ct=40", |
| 24 | + # ad-hoc for application/xhtml+xml;charset=utf-8 |
| 25 | + ContentFormat(65000): |
| 26 | + b'<html xmlns="http://www.w3.org/1999/xhtml">' |
| 27 | + b'<head><title>aiocoap demo</title></head>' |
| 28 | + b'<body><h1>Welcome to the aiocoap demo server!</h1>' |
| 29 | + b'<ul><li><a href="time">Current time</a></li>' |
| 30 | + b'<li><a href="whoami">Report my network address</a></li>' |
| 31 | + b'</ul></body></html>', |
| 32 | + } |
| 33 | + |
| 34 | + default_representation = ContentFormat.TEXT |
| 35 | + |
| 36 | + async def render_get(self, request): |
| 37 | + cf = self.default_representation if request.opt.accept is None else request.opt.accept |
| 38 | + try: |
| 39 | + return aiocoap.Message(payload=self.representations[cf], content_format=cf) |
| 40 | + except KeyError: |
| 41 | + raise aiocoap.error.UnsupportedContentFormat |
19 | 42 |
|
20 | 43 | class BlockResource(resource.Resource):
|
21 | 44 | """Example resource which supports the GET and PUT methods. It sends large
|
@@ -117,6 +140,7 @@ async def main():
|
117 | 140 |
|
118 | 141 | root.add_resource(['.well-known', 'core'],
|
119 | 142 | resource.WKCResource(root.get_resources_as_linkheader))
|
| 143 | + root.add_resource([], Welcome()) |
120 | 144 | root.add_resource(['time'], TimeResource())
|
121 | 145 | root.add_resource(['other', 'block'], BlockResource())
|
122 | 146 | root.add_resource(['other', 'separate'], SeparateLargeResource())
|
|
0 commit comments