aws / chalice

Python Serverless Microframework for AWS
Apache License 2.0
10.61k stars 1.01k forks source link

accessing uri params for websockets api (question) #1471

Open fullstackdev-online opened 4 years ago

fullstackdev-online commented 4 years ago

How to access uri query string parameters for websockets API with chalice?

E.g. my gateway uri has extra paramet(s) like wss://my-example-gateway.com/api/?id=some-id&param=value

In nodejs I'd access the id and param values using event.queryStringParameters like in the code below: exports.handler = async (event) => {
id = event.queryStringParameters.id, param = event.queryStringParameters.param, // ... do other stuff };

but in chalice on_ws_connect event argument (type WebsocketEvent) I only see

        self.domain_name = request_context['domainName']
        self.stage = request_context['stage']
        self.connection_id = request_context['connectionId']
        self.body = event_dict.get('body')
ivandjuricic commented 4 years ago

Is something like this you were looking for?

@app.on_ws_connect()
def connect(event):
    params = event.to_dict()["queryStringParameters"]
    print('Params: %s' % params)
jamesls commented 4 years ago

Yeah, it looks like this isn't mapped right now. We currently use a single event class between all the websocket handlers (connect, message, disconnect), but the params such as queryString, headers, etc is only available on the connect handler. The on_ws_message handler has requestContext, body, isBase64Encoded whereas the on_ws_connect has:

headers, multiValueHeaders, queryStringParameters, multiValueQueryStringParameters, requestContext, isBase64Encoded.

While you can use @ivandjuricic's suggestion of event.to_dict() to get access to the underlying event dictionary, I think the long-term fix here should be to have separate event classes for connect, message, and disconnect. That way we can map the additional parameters into the connect handler.

This is technically a breaking change, but given that this is an experimental API I think now's a good time to update this.

Thoughts? /cc @stealthycoin

fullstackdev-online commented 4 years ago

this sounds good and fully makes sense and would be cleaner and more pythonic, though the workaround from @ivandjuricic is also easy to implement. So I'd vote for the enhancement, but it's definitely not a blocker.

Ankushpandey-ti commented 1 year ago

Hey guys any update on this ?

robmoss2k commented 7 months ago

Still no update?