Does nameko provide a sort of RequestContext

Hi,

I was wondering if nameko provided anything like flask's g object
<http://flask.pocoo.org/docs/0.12/api/#flask.g> to get the current Request
Context. Basically, when a request comes in, I would like to see the
current user tied to the request from elsewhere in the application. And I
would prefer to not have to pass that user_id all around my application.

In flask you can do something like:

@app.route('/')def hello_world():

    g.user_id = 1

    return 'Hello, World!'@app.route('/')

# And then somewhere else in the app you can do

def process_current_user():

    current_user = g.user_id

    # Do something with current_user

Indeed it does. The "context_data" dictionary on the WorkerContext should
be used for this purpose. It is serialised and passed between services, so
can be used for contextual data like this.

Common uses are for the language/locale of the call and user ID or session
token. You'll also find the "call id stack" in there, which Nameko uses to
track chains of calls between multiple services.

By way of example I've put a toy authentication example in a gist
<https://gist.github.com/mattbennett/8a67956ea78d4481d5ea9072e770ba4b&gt;\.

Note that it's the responsibility of the Entrypoints and
DependencyProviders in use to populate and/or re-transmit the context data.
The @rpc entrypoint extracts it from AMQP message "application headers",
and the RpcProxy re-encodes it into request message headers. The @http
entrypoint has support for extracting context data from http request headers
<https://github.com/nameko/nameko/blob/v2.5.3/nameko/web/handlers.py#L43&gt;
but you have to implement the method in the server yourself. If you're
using HTTP to communicate between services, you'll probably want to
implement a DependencyProvider that adds the context data as headers when
it makes a request.

···

On Thursday, April 13, 2017 at 7:33:53 PM UTC+1, di...@clearmetal.com wrote:

Hi,

I was wondering if nameko provided anything like flask's g object
<http://flask.pocoo.org/docs/0.12/api/#flask.g&gt; to get the current
Request Context. Basically, when a request comes in, I would like to see
the current user tied to the request from elsewhere in the application.
And I would prefer to not have to pass that user_id all around my
application.

In flask you can do something like:

@app.route('/')def hello_world():

    g.user_id = 1

    return 'Hello, World!'@app.route('/')

# And then somewhere else in the app you can do

def process_current_user():

    current_user = g.user_id

    # Do something with current_user