# DependencyProvider get\_dependency specific to request

**URL:** <https://discourse.nameko.io/t/dependencyprovider-get-dependency-specific-to-request/190>\
**Category:** googlegroup\
**Created:** [April 19, 2017, 1:29am UTC](https://discourse.nameko.io/t/dependencyprovider-get-dependency-specific-to-request/190 "2017-04-19T01:29:52Z")\
**Posts on this page:** 4\
**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 19, 2017, 1:29am UTC](https://discourse.nameko.io/t/dependencyprovider-get-dependency-specific-to-request/190/1 "2017-04-19T01:29:52Z")

</div>

I have a use case where we have isolated databases for each client. That  
means, when a request from a specific client comes in, I want to get access  
to that client's specific database.

Let's say I have a database DependencyProvider (similar to  
nameko\_sqlalchemy), where the \*setup \*function instantiates all of my  
connections to all of the client databases. I'm not entirely clear how I  
can alter the worker\_ctx in \*get\_dependency \*so that I can return the  
connection for a specific client. It seems like I somehow need to alter  
worker\_ctx so that I end up doing something like:

class Database(DependencyProvider):

&nbsp;&nbsp;&nbsp;&nbsp;def setup(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#Start all client connections

&nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;client = worker\_ctx['client\_id']  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session\_cls = sessionmaker(bind=self.connections[client])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session = session\_cls()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.session[worker\_ctx] = session  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return session  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
Do you have any suggestions for how to do this?

Thanks!

---

<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 19, 2017, 7:26am UTC](https://discourse.nameko.io/t/dependencyprovider-get-dependency-specific-to-request/190/2 "2017-04-19T07:26:32Z")

</div>

You've got the right approach.

The WorkerContext has a `context_data` dict for carrying request-contextual  
data like `session_id`, `locale` etc between calls. If you put the  
`client_id` in there, it'll be available to all DependencyProviders in the  
current service. It will also pass along between service calls if your  
inter-service DependencyProviders and Entrypoints pass it along. See my  
reply to your question here  
\<[Redirecting to Google Groups](https://groups.google.com/forum/#!topic/nameko-dev/Jz2V0llSFJ0&gt); for more.

> **···**
>
> On Wednesday, April 19, 2017 at 2:29:52 AM UTC+1, di...@clearmetal.com wrote:
> 
> > I have a use case where we have isolated databases for each client. That  
> > means, when a request from a specific client comes in, I want to get access  
> > to that client's specific database.
> > 
> > Let's say I have a database DependencyProvider (similar to  
> > nameko\_sqlalchemy), where the \*setup \*function instantiates all of my  
> > connections to all of the client databases. I'm not entirely clear how I  
> > can alter the worker\_ctx in \*get\_dependency \*so that I can return the  
> > connection for a specific client. It seems like I somehow need to alter  
> > worker\_ctx so that I end up doing something like:
> > 
> > class Database(DependencyProvider):
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def setup(self):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#Start all client connections
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;client = worker\_ctx['client\_id']  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session\_cls = sessionmaker(bind=self.connections[client])  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session = session\_cls()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.session[worker\_ctx] = session  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return session  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
> > Do you have any suggestions for how to do this?
> > 
> > Thanks!

---

<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 22, 2017, 8:55pm UTC](https://discourse.nameko.io/t/dependencyprovider-get-dependency-specific-to-request/190/3 "2017-04-22T20:55:34Z")

</div>

Hi Matt,

Thanks for the response. That's a solution I hadn't thought of, thanks for  
posting it.

I'm curious whether you can achieve the same functionality with "logging  
in" with a session. Can you alter the context data when you are making a  
call from one service to another? For example, could I do something similar  
to the following:

class Service1(object):  
&nbsp;&nbsp;&nbsp;&nbsp;name = 'service\_1'  
&nbsp;&nbsp;&nbsp;&nbsp;service\_2 = RpcProxy('service\_2')

&nbsp;&nbsp;&nbsp;&nbsp;@rpc  
&nbsp;&nbsp;&nbsp;&nbsp;def client\_endpoint(self, client\_id):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data = {"data": "blah"}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# SOME HOW PASS IN CONTEXT DATA HERE ??  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# self.worker\_ctx.context\_data['client\_id'] = client\_id  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.service\_2.write\_to\_database(client\_id, data)

class Service2(object):  
&nbsp;&nbsp;&nbsp;&nbsp;name = 'service\_2'  
&nbsp;&nbsp;&nbsp;&nbsp;session = Session(Base)

&nbsp;&nbsp;&nbsp;&nbsp;@rpc  
&nbsp;&nbsp;&nbsp;&nbsp;def write\_to\_database(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session = self.session # Uses context\_data from client\_endpoint to  
get session specific to client

> **···**
>
> On Wednesday, April 19, 2017 at 12:26:32 AM UTC-7, Matt Yule-Bennett wrote:
> 
> > You've got the right approach.
> > 
> > The WorkerContext has a `context_data` dict for carrying  
> > request-contextual data like `session_id`, `locale` etc between calls. If  
> > you put the `client_id` in there, it'll be available to all  
> > DependencyProviders in the current service. It will also pass along between  
> > service calls if your inter-service DependencyProviders and Entrypoints  
> > pass it along. See my reply to your question here  
> > \<[Redirecting to Google Groups](https://groups.google.com/forum/#!topic/nameko-dev/Jz2V0llSFJ0&gt); for more.
> > 
> > On Wednesday, April 19, 2017 at 2:29:52 AM UTC+1, di...@clearmetal.com \> wrote:
> > 
> > > I have a use case where we have isolated databases for each client. That  
> > > means, when a request from a specific client comes in, I want to get access  
> > > to that client's specific database.
> > > 
> > > Let's say I have a database DependencyProvider (similar to  
> > > nameko\_sqlalchemy), where the \*setup \*function instantiates all of my  
> > > connections to all of the client databases. I'm not entirely clear how I  
> > > can alter the worker\_ctx in \*get\_dependency \*so that I can return the  
> > > connection for a specific client. It seems like I somehow need to alter  
> > > worker\_ctx so that I end up doing something like:
> > > 
> > > class Database(DependencyProvider):
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;def setup(self):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#Start all client connections
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;client = worker\_ctx['client\_id']  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session\_cls = sessionmaker(bind=self.connections[client])  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session = session\_cls()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.session[worker\_ctx] = session  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return session  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
> > > Do you have any suggestions for how to do this?
> > > 
> > > Thanks!

---

<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 22, 2017, 9:34pm UTC](https://discourse.nameko.io/t/dependencyprovider-get-dependency-specific-to-request/190/4 "2017-04-22T21:34:03Z")

</div>

Yes, you can do exactly this.

The only caveat is that service classes can't access the worker context  
directly -- you'd need to modify it via a DependencyProvider (or use one to  
expose it directly).

With this pattern, we sometimes use a context manager around the RPC proxy  
call to set context data for that call and remove it again afterwards.

> **···**
>
> On Saturday, April 22, 2017 at 9:55:34 PM UTC+1, di...@clearmetal.com wrote:
> 
> > Hi Matt,
> > 
> > Thanks for the response. That's a solution I hadn't thought of, thanks  
> > for posting it.
> > 
> > I'm curious whether you can achieve the same functionality with "logging  
> > in" with a session. Can you alter the context data when you are making a  
> > call from one service to another? For example, could I do something similar  
> > to the following:
> > 
> > class Service1(object):  
> > &nbsp;&nbsp;&nbsp;&nbsp;name = 'service\_1'  
> > &nbsp;&nbsp;&nbsp;&nbsp;service\_2 = RpcProxy('service\_2')
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > &nbsp;&nbsp;&nbsp;&nbsp;def client\_endpoint(self, client\_id):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data = {"data": "blah"}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# SOME HOW PASS IN CONTEXT DATA HERE ??  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# self.worker\_ctx.context\_data['client\_id'] = client\_id  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.service\_2.write\_to\_database(client\_id, data)
> > 
> > class Service2(object):  
> > &nbsp;&nbsp;&nbsp;&nbsp;name = 'service\_2'  
> > &nbsp;&nbsp;&nbsp;&nbsp;session = Session(Base)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > &nbsp;&nbsp;&nbsp;&nbsp;def write\_to\_database(self):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session = self.session # Uses context\_data from client\_endpoint to  
> > get session specific to client
> > 
> > On Wednesday, April 19, 2017 at 12:26:32 AM UTC-7, Matt Yule-Bennett wrote:
> > 
> > > You've got the right approach.
> > > 
> > > The WorkerContext has a `context_data` dict for carrying  
> > > request-contextual data like `session_id`, `locale` etc between calls. If  
> > > you put the `client_id` in there, it'll be available to all  
> > > DependencyProviders in the current service. It will also pass along between  
> > > service calls if your inter-service DependencyProviders and Entrypoints  
> > > pass it along. See my reply to your question here  
> > > \<[Redirecting to Google Groups](https://groups.google.com/forum/#!topic/nameko-dev/Jz2V0llSFJ0&gt); for  
> > > more.
> > > 
> > > On Wednesday, April 19, 2017 at 2:29:52 AM UTC+1, di...@clearmetal.com \>\> wrote:
> > > 
> > > > I have a use case where we have isolated databases for each client.  
> > > > That means, when a request from a specific client comes in, I want to get  
> > > > access to that client's specific database.
> > > > 
> > > > Let's say I have a database DependencyProvider (similar to  
> > > > nameko\_sqlalchemy), where the \*setup \*function instantiates all of my  
> > > > connections to all of the client databases. I'm not entirely clear how I  
> > > > can alter the worker\_ctx in \*get\_dependency \*so that I can return the  
> > > > connection for a specific client. It seems like I somehow need to alter  
> > > > worker\_ctx so that I end up doing something like:
> > > > 
> > > > class Database(DependencyProvider):
> > > > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;def setup(self):  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#Start all client connections
> > > > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;client = worker\_ctx['client\_id']  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session\_cls = sessionmaker(bind=self.connections[client])  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session = session\_cls()  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.session[worker\_ctx] = session  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return session  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
> > > > Do you have any suggestions for how to do this?
> > > > 
> > > > Thanks!
