# Dynamic dependency Injection from @event\_handler method

**URL:** <https://discourse.nameko.io/t/dynamic-dependency-injection-from-event-handler-method/228>\
**Category:** googlegroup\
**Created:** [December 21, 2017, 10:58pm UTC](https://discourse.nameko.io/t/dynamic-dependency-injection-from-event-handler-method/228 "2017-12-21T22:58:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yevgeniy\_Viller](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@Yevgeniy\_Viller](https://discourse.nameko.io/u/Yevgeniy_Viller)\
**Post date:** [December 21, 2017, 10:58pm UTC](https://discourse.nameko.io/t/dynamic-dependency-injection-from-event-handler-method/228/1 "2017-12-21T22:58:44Z")

</div>

Hey guys,

We are trying to implement data pipeline, where services communicate with  
each other using nameko.  
One of the services query config service and passes the config object using  
dispatch() method.

Other method needs to parse the config and use it to connect to the outside  
dependency.

#Some storage service (google-storage, s3 etc....)  
from storage import \*  
from nameko.events import EventDispatcher, event\_handler  
from nameko.extensions import DependencyProvider  
from nameko.rpc import rpc

class Storage(DependencyProvider):

&nbsp;&nbsp;&nbsp;&nbsp;def \_\_init\_\_(self, json\_key, project\_name, bucket\_name):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.json\_key = json\_key  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.project\_name = project\_name  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.bucket\_name = bucket\_name

&nbsp;&nbsp;&nbsp;&nbsp;def setup(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;credentials = service\_account.Credentials.from\_service\_account\_file(self.json\_key)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.storage\_client = storage.Client(project=self.project\_name, credentials=credentials)

&nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bucket = self.storage\_client.bucket(self.bucket\_name)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return bucket

class SendConfig:  
&nbsp;&nbsp;&nbsp;&nbsp;name = "send\_config"

&nbsp;&nbsp;&nbsp;&nbsp;dispatch = EventDispatcher()  
&nbsp;&nbsp;&nbsp;&nbsp;#dispatch config  
&nbsp;&nbsp;&nbsp;&nbsp;@rpc  
&nbsp;&nbsp;&nbsp;&nbsp;def dispatch\_method(self, payload):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.dispatch('config', payload)

class ListFiles:

&nbsp;&nbsp;&nbsp;&nbsp;name = "list\_files"

&nbsp;&nbsp;&nbsp;&nbsp;def list\_files(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for blob in self.bucket.list\_blobs():  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return blob.path  
&nbsp;&nbsp;&nbsp;&nbsp;# get config and try to connect to the storage  
&nbsp;&nbsp;&nbsp;&nbsp;@event\_handler("send\_config", 'pid\_config')

&nbsp;&nbsp;&nbsp;&nbsp;def print\_config(self, payload):  
&nbsp;&nbsp;&nbsp;&nbsp;# Dependency ingestion  
&nbsp;&nbsp;&nbsp;&nbsp;self.bucket = Storage(payload["json\_key"], payload["project\_name"], payload["bucket"])  
&nbsp;&nbsp;&nbsp;&nbsp;# printing the results from storage  
&nbsp;&nbsp;&nbsp;&nbsp;print self.list\_log\_files()

When I run it, I know that setup method is not fired hence it doesn't  
return the bucket from get\_dependency(). Is there a way to start injection  
manually with received config?

Thanks,  
Yevgeniy

---

<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:** [December 22, 2017, 12:04pm UTC](https://discourse.nameko.io/t/dynamic-dependency-injection-from-event-handler-method/228/2 "2017-12-22T12:04:38Z")

</div>

DependencyProviders must be declared on the service class, not in service  
methods. Dependency injection happens when a new worker is spawned, before  
the service method is executed.

If you need to dynamically configure your storage client based on a payload  
received by another service method, you'll need to do it inside the service  
method.

> **···**
>
> On Thursday, December 21, 2017 at 10:58:44 PM UTC, Yevgeniy Viller wrote:
> 
> > Hey guys,
> > 
> > We are trying to implement data pipeline, where services communicate with  
> > each other using nameko.  
> > One of the services query config service and passes the config object  
> > using dispatch() method.
> > 
> > Other method needs to parse the config and use it to connect to the  
> > outside dependency.
> > 
> > #Some storage service (google-storage, s3 etc....)  
> > from storage import \*  
> > from nameko.events import EventDispatcher, event\_handler  
> > from nameko.extensions import DependencyProvider  
> > from nameko.rpc import rpc
> > 
> > class Storage(DependencyProvider):
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def \_\_init\_\_(self, json\_key, project\_name, bucket\_name):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.json\_key = json\_key  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.project\_name = project\_name  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.bucket\_name = bucket\_name
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def setup(self):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;credentials = service\_account.Credentials.from\_service\_account\_file(self.json\_key)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.storage\_client = storage.Client(project=self.project\_name, credentials=credentials)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bucket = self.storage\_client.bucket(self.bucket\_name)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return bucket
> > 
> > class SendConfig:  
> > &nbsp;&nbsp;&nbsp;&nbsp;name = "send\_config"
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;dispatch = EventDispatcher()  
> > &nbsp;&nbsp;&nbsp;&nbsp;#dispatch config  
> > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > &nbsp;&nbsp;&nbsp;&nbsp;def dispatch\_method(self, payload):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.dispatch('config', payload)
> > 
> > class ListFiles:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;name = "list\_files"
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def list\_files(self):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for blob in self.bucket.list\_blobs():  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return blob.path  
> > &nbsp;&nbsp;&nbsp;&nbsp;# get config and try to connect to the storage  
> > &nbsp;&nbsp;&nbsp;&nbsp;@event\_handler("send\_config", 'pid\_config')
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def print\_config(self, payload):  
> > &nbsp;&nbsp;&nbsp;&nbsp;# Dependency ingestion  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.bucket = Storage(payload["json\_key"], payload["project\_name"], payload["bucket"])  
> > &nbsp;&nbsp;&nbsp;&nbsp;# printing the results from storage  
> > &nbsp;&nbsp;&nbsp;&nbsp;print self.list\_log\_files()
> > 
> > When I run it, I know that setup method is not fired hence it doesn't  
> > return the bucket from get\_dependency(). Is there a way to start injection  
> > manually with received config?
> > 
> > Thanks,  
> > Yevgeniy

---

<div class="post-metadata">

**Author:** ![Yevgeniy\_Viller](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@Yevgeniy\_Viller](https://discourse.nameko.io/u/Yevgeniy_Viller)\
**Post date:** [January 2, 2018, 5:09pm UTC](https://discourse.nameko.io/t/dynamic-dependency-injection-from-event-handler-method/228/3 "2018-01-02T17:09:01Z")

</div>

Thanks, Matt.

> **···**
>
> On Friday, December 22, 2017 at 7:04:38 AM UTC-5, Matt Yule-Bennett wrote:
> 
> > DependencyProviders must be declared on the service class, not in service  
> > methods. Dependency injection happens when a new worker is spawned, before  
> > the service method is executed.
> > 
> > If you need to dynamically configure your storage client based on a  
> > payload received by another service method, you'll need to do it inside the  
> > service method.
> > 
> > On Thursday, December 21, 2017 at 10:58:44 PM UTC, Yevgeniy Viller wrote:
> > 
> > > Hey guys,
> > > 
> > > We are trying to implement data pipeline, where services communicate with  
> > > each other using nameko.  
> > > One of the services query config service and passes the config object  
> > > using dispatch() method.
> > > 
> > > Other method needs to parse the config and use it to connect to the  
> > > outside dependency.
> > > 
> > > #Some storage service (google-storage, s3 etc....)  
> > > from storage import \*  
> > > from nameko.events import EventDispatcher, event\_handler  
> > > from nameko.extensions import DependencyProvider  
> > > from nameko.rpc import rpc
> > > 
> > > class Storage(DependencyProvider):
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;def \_\_init\_\_(self, json\_key, project\_name, bucket\_name):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.json\_key = json\_key  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.project\_name = project\_name  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.bucket\_name = bucket\_name
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;def setup(self):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;credentials = service\_account.Credentials.from\_service\_account\_file(self.json\_key)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.storage\_client = storage.Client(project=self.project\_name, credentials=credentials)
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bucket = self.storage\_client.bucket(self.bucket\_name)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return bucket
> > > 
> > > class SendConfig:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;name = "send\_config"
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;dispatch = EventDispatcher()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;#dispatch config  
> > > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > > &nbsp;&nbsp;&nbsp;&nbsp;def dispatch\_method(self, payload):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.dispatch('config', payload)
> > > 
> > > class ListFiles:
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;name = "list\_files"
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;def list\_files(self):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for blob in self.bucket.list\_blobs():  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return blob.path  
> > > &nbsp;&nbsp;&nbsp;&nbsp;# get config and try to connect to the storage  
> > > &nbsp;&nbsp;&nbsp;&nbsp;@event\_handler("send\_config", 'pid\_config')
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;def print\_config(self, payload):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;# Dependency ingestion  
> > > &nbsp;&nbsp;&nbsp;&nbsp;self.bucket = Storage(payload["json\_key"], payload["project\_name"], payload["bucket"])  
> > > &nbsp;&nbsp;&nbsp;&nbsp;# printing the results from storage  
> > > &nbsp;&nbsp;&nbsp;&nbsp;print self.list\_log\_files()
> > > 
> > > When I run it, I know that setup method is not fired hence it doesn't  
> > > return the bucket from get\_dependency(). Is there a way to start injection  
> > > manually with received config?
> > > 
> > > Thanks,  
> > > Yevgeniy
