# Does nameko provide a sort of RequestContext

**URL:** <https://discourse.nameko.io/t/does-nameko-provide-a-sort-of-requestcontext/187>\
**Category:** googlegroup\
**Created:** [April 13, 2017, 6:33pm UTC](https://discourse.nameko.io/t/does-nameko-provide-a-sort-of-requestcontext/187 "2017-04-13T18:33:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Diego](https://discourse.nameko.io/u/Diego)\
**Post date:** [April 13, 2017, 6:33pm UTC](https://discourse.nameko.io/t/does-nameko-provide-a-sort-of-requestcontext/187/1 "2017-04-13T18:33:53Z")

</div>

Hi,

I was wondering if nameko provided anything like flask's g object  
\<[http://flask.pocoo.org/docs/0.12/api/#flask.g](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():

&nbsp;&nbsp;&nbsp;&nbsp;g.user\_id = 1

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

# And then somewhere else in the app you can do

def process\_current\_user():

&nbsp;&nbsp;&nbsp;&nbsp;current\_user = g.user\_id

&nbsp;&nbsp;&nbsp;&nbsp;# Do something with current\_user

---

<div class="post-metadata">

**Author:** ![mattbennett](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/mattbennett/32/13_2.png) [@mattbennett](https://discourse.nameko.io/u/mattbennett)\
**Post date:** [April 14, 2017, 4:22pm UTC](https://discourse.nameko.io/t/does-nameko-provide-a-sort-of-requestcontext/187/2 "2017-04-14T16:22:09Z")

</div>

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;\](https://gist.github.com/mattbennett/8a67956ea78d4481d5ea9072e770ba4b&gt;%5C).

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](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](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():
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;g.user\_id = 1
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;return 'Hello, World!'@app.route('/')
> > 
> > # And then somewhere else in the app you can do
> > 
> > def process\_current\_user():
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;current\_user = g.user\_id
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;# Do something with current\_user
