mwasilak / txThings

CoAP library for Twisted
94 stars 43 forks source link

How to handle dynamic URI & query string? #27

Closed engrjislam closed 4 years ago

engrjislam commented 4 years ago

I am recently working with a project which requires to handle dynamic URI. Anyway, it requires two dynamic variables. The possible way to take these two variables are:

I am able to pass one of them path variable but I am not able to pass both neither in path variables nor in query string.

The following code can handle just one variable:

import sys
import datetime

from twisted.internet import defer
from twisted.internet import reactor
from twisted.python import log

import txthings.resource as resource
import txthings.coap as coap

class ExploreResource(resource.CoAPResource):
    """Can handle 1 dynamic path variable ()"""
    def __init__(self, pathVar1):
        resource.CoAPResource.__init__(self)
        self.pathVar1 = pathVar1

    def render_POST(self, request):
        # todo with self.pathVar1
        payload = "%s added successfully!" % self.pathVar1
        response = coap.Message(code=coap.CONTENT, payload=payload)
        return defer.succeed(response)

class ExploreDynamicResource(resource.CoAPResource):        
    def getChild(self, pathVar1, request):
        return ExploreResource(pathVar1)

# start log to the console
log.startLogging(sys.stdout)

# coap://<host>:<port>
root = resource.CoAPResource()

explore_discovery = ExploreDynamicResource()
root.putChild('explore', explore_discovery)

endpoint = resource.Endpoint(root)

reactor.listenUDP(coap.COAP_PORT, coap.Coap(endpoint))  # , interface="::")
reactor.run()

What I need to do in server code to get both VAR_1 and VAR_2? Any Idea? I appreciate your valuable comments & suggestions.

Thanks in advance!