Skip to content

Commit

Permalink
Allow setting opts from Message construction
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Nov 17, 2016
1 parent 684ccdd commit ef2e45e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
9 changes: 8 additions & 1 deletion aiocoap/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ class Message(object):
* :attr:`prepath`, :attr:`postpath`: Not sure, will probably go away when
resources are overhauled. Non-roundtrippable.
Options can be given as further keyword arguments at message construction
time. This feature is experimental, as future message parameters could
collide with options.
"""

def __init__(self, *, mtype=None, mid=None, code=None, payload=b'', token=b'', uri=None):
def __init__(self, *, mtype=None, mid=None, code=None, payload=b'', token=b'', uri=None, **kwargs):
self.version = 1
if mtype is None:
# leave it unspecified for convenience, sending functions will know what to do
Expand Down Expand Up @@ -106,6 +110,9 @@ def __init__(self, *, mtype=None, mid=None, code=None, payload=b'', token=b'', u
if uri:
self.set_request_uri(uri)

for k, v in kwargs.items():
setattr(self.opt, k, v)

def __repr__(self):
return "<aiocoap.Message at %#x: %s %s (ID %r, token %r) remote %s%s%s>"%(
id(self),
Expand Down
4 changes: 1 addition & 3 deletions clientGET-observe.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
async def main():
protocol = await Context.create_client_context()

request = Message(code=GET)
request.set_request_uri('coap://localhost/time')
request.opt.observe = 0
request = Message(code=GET, uri='coap://localhost/time', observe=0)

pr = protocol.request(request)

Expand Down
3 changes: 1 addition & 2 deletions clientGET.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
async def main():
protocol = await Context.create_client_context()

request = Message(code=GET)
request.set_request_uri('coap://localhost/time')
request = Message(code=GET, uri='coap://localhost/time')

try:
response = await protocol.request(request).response
Expand Down
18 changes: 8 additions & 10 deletions examples/aiocoap-fileserver
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ class FileServer(Resource):

async def render_get(self, request):
if request.opt.uri_path == ('.well-known', 'core'):
m = aiocoap.Message(payload=b"</>;ct=40")
m.opt.content_format = 40
return m
return aiocoap.Message(payload=b"</>;ct=40", content_format=40)

path = self.request_to_localpath(request)
try:
Expand All @@ -76,9 +74,7 @@ class FileServer(Resource):
response += "<%s/>;ct=40,"%f
else:
response += "<%s>,"%f
m = aiocoap.Message(payload=response[:-1].encode('utf8'))
m.opt.content_format = 40
return m
return aiocoap.Message(payload=response[:-1].encode('utf8'), content_format=40)

async def render_get_file(self, request, path):
block_in = request.opt.block2 or aiocoap.optiontypes.BlockOption.BlockwiseTuple(0, 0, 6)
Expand All @@ -90,10 +86,12 @@ class FileServer(Resource):
guessed_type, _ = mimetypes.guess_type(str(path))

block_out = aiocoap.optiontypes.BlockOption.BlockwiseTuple(block_in.block_number, len(data) > block_in.size, block_in.size_exponent)
response = aiocoap.Message(payload=data[:block_in.size])
response.opt.block2 = block_out
response.opt.content_format = aiocoap.numbers.media_types_rev.get(guessed_type, 0 if guessed_type is not None and guessed_type.startswith('text/') else 42)
return response
return aiocoap.Message(
payload=data[:block_in.size],
block2=block_out,
content_format=aiocoap.numbers.media_types_rev.get(guessed_type,
0 if guessed_type is not None and guessed_type.startswith('text/') else 42),
)

async def main(path, verbosity=0):
log = logging.getLogger('fileserver')
Expand Down
7 changes: 2 additions & 5 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def __init__(self):
"transfer.\n" + "0123456789\n" * 100).encode("ascii")

async def render_get(self, request):
response = aiocoap.Message(payload=self.content)
return response
return aiocoap.Message(payload=self.content)

async def render_put(self, request):
print('PUT payload: %s' % request.payload)
Expand Down Expand Up @@ -103,9 +102,7 @@ async def render_get(self, request):
# data = []
# self.root.generate_resource_list(data, "")
# payload = ",".join(data).encode('utf-8')
# response = aiocoap.Message(payload=payload)
# response.opt.content_format = 40
# return response
# return aiocoap.Message(payload=payload, content_format=40)

# logging setup

Expand Down

0 comments on commit ef2e45e

Please sign in to comment.