# Programmatically adding endpoints

**URL:** <https://discourse.nameko.io/t/programmatically-adding-endpoints/162>\
**Category:** googlegroup\
**Created:** [November 28, 2016, 11:10am UTC](https://discourse.nameko.io/t/programmatically-adding-endpoints/162 "2016-11-28T11:10:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adwhit](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@adwhit](https://discourse.nameko.io/u/adwhit)\
**Post date:** [November 28, 2016, 11:10am UTC](https://discourse.nameko.io/t/programmatically-adding-endpoints/162/1 "2016-11-28T11:10:53Z")

</div>

Hi, thanks for making a great library.

Question: Is there a way to programmatically add endpoints to services?

e.g. suppose I have a large dict of (endpoint, function) pairs

def myf1():  
&nbsp;&nbsp;&nbsp;&nbsp;return "foo"

def myf2():  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "bar"

d = {  
&nbsp;&nbsp;"f1": f1  
&nbsp;&nbsp;"f2": f2  
}

I would like to register these functions with a service programmatically in  
a way that is functionally equivalent to:

from nameko.rpc import rpc

class MyService:  
&nbsp;&nbsp;&nbsp;&nbsp;name = "my\_service"

&nbsp;&nbsp;&nbsp;&nbsp;@rpc  
&nbsp;&nbsp;&nbsp;&nbsp;def f1(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "foo"  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;@rpc  
&nbsp;&nbsp;&nbsp;&nbsp;def f2(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "bar"

I have a hacky solution like:

class MyService:  
&nbsp;&nbsp;&nbsp;&nbsp;name = "my\_service"

def wrapper(f):  
&nbsp;&nbsp;&nbsp;&nbsp;def func(self, \*args, \*\*kwargs):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return f(\*args, \*\*kwargs)  
&nbsp;&nbsp;&nbsp;&nbsp;return func

def add\_rpc\_endpoints\_from\_dict(registry):  
&nbsp;&nbsp;&nbsp;&nbsp;for name, f in registry.items():  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g = wrapper(f)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setattr(MyService, name, g)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rpc(g)

Is there a better way? Or a better way to solve this problem in general?

Thanks  
Alex

---

<div class="post-metadata">

**Author:** ![David\_Szotten](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/david_szotten/32/6_2.png) [@David\_Szotten](https://discourse.nameko.io/u/David_Szotten)\
**Post date:** [November 28, 2016, 3:36pm UTC](https://discourse.nameko.io/t/programmatically-adding-endpoints/162/2 "2016-11-28T15:36:11Z")

</div>

Hi Alex,

Not sure i understand the use-case (why is it easier to put the functions  
into a dict than onto a class?)

nameko expects services to be classes, so any programmatic approach would  
be similar to what you've got (you could also create the whole class  
dynamically) which seems fine

Best,  
David

> **···**
>
> On Monday, 28 November 2016 11:10:53 UTC, adwhit wrote:
> 
> > Hi, thanks for making a great library.
> > 
> > Question: Is there a way to programmatically add endpoints to services?
> > 
> > e.g. suppose I have a large dict of (endpoint, function) pairs
> > 
> > def myf1():  
> > &nbsp;&nbsp;&nbsp;&nbsp;return "foo"
> > 
> > def myf2():  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "bar"
> > 
> > d = {  
> > &nbsp;&nbsp;"f1": f1  
> > &nbsp;&nbsp;"f2": f2  
> > }
> > 
> > I would like to register these functions with a service programmatically  
> > in a way that is functionally equivalent to:
> > 
> > from nameko.rpc import rpc
> > 
> > class MyService:  
> > &nbsp;&nbsp;&nbsp;&nbsp;name = "my\_service"
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > &nbsp;&nbsp;&nbsp;&nbsp;def f1(self):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "foo"  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > &nbsp;&nbsp;&nbsp;&nbsp;def f2(self):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "bar"
> > 
> > I have a hacky solution like:
> > 
> > class MyService:  
> > &nbsp;&nbsp;&nbsp;&nbsp;name = "my\_service"
> > 
> > def wrapper(f):  
> > &nbsp;&nbsp;&nbsp;&nbsp;def func(self, \*args, \*\*kwargs):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return f(\*args, \*\*kwargs)  
> > &nbsp;&nbsp;&nbsp;&nbsp;return func
> > 
> > def add\_rpc\_endpoints\_from\_dict(registry):  
> > &nbsp;&nbsp;&nbsp;&nbsp;for name, f in registry.items():  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g = wrapper(f)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setattr(MyService, name, g)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rpc(g)
> > 
> > Is there a better way? Or a better way to solve this problem in general?
> > 
> > Thanks  
> > Alex

---

<div class="post-metadata">

**Author:** ![adwhit](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@adwhit](https://discourse.nameko.io/u/adwhit)\
**Post date:** [November 28, 2016, 4:36pm UTC](https://discourse.nameko.io/t/programmatically-adding-endpoints/162/3 "2016-11-28T16:36:13Z")

</div>

Hi David,

Thanks for the reply.

The use case is that there are a largeish number of different functions  
defined in various parts of the codebase, which are collected together in a  
"Registry" object along with metadata. It would be nice to add endpoints  
using this registry rather than typing them out again (DRY etc).

E.g. using Flask I would iterate over the registry and use app.add\_url\_rule  
for each entry.

The above method is pretty hacky but seems to work fine so I'll probably  
just use that.

Thanks,  
Alex

> **···**
>
> On Monday, 28 November 2016 15:36:12 UTC, David Szotten wrote:
> 
> > Hi Alex,
> > 
> > Not sure i understand the use-case (why is it easier to put the functions  
> > into a dict than onto a class?)
> > 
> > nameko expects services to be classes, so any programmatic approach would  
> > be similar to what you've got (you could also create the whole class  
> > dynamically) which seems fine
> > 
> > Best,  
> > David
> > 
> > On Monday, 28 November 2016 11:10:53 UTC, adwhit wrote:
> > 
> > > Hi, thanks for making a great library.
> > > 
> > > Question: Is there a way to programmatically add endpoints to services?
> > > 
> > > e.g. suppose I have a large dict of (endpoint, function) pairs
> > > 
> > > def myf1():  
> > > &nbsp;&nbsp;&nbsp;&nbsp;return "foo"
> > > 
> > > def myf2():  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "bar"
> > > 
> > > d = {  
> > > &nbsp;&nbsp;"f1": f1  
> > > &nbsp;&nbsp;"f2": f2  
> > > }
> > > 
> > > I would like to register these functions with a service programmatically  
> > > in a way that is functionally equivalent to:
> > > 
> > > from nameko.rpc import rpc
> > > 
> > > class MyService:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;name = "my\_service"
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > > &nbsp;&nbsp;&nbsp;&nbsp;def f1(self):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "foo"  
> > > &nbsp;&nbsp;&nbsp;&nbsp;  
> > > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > > &nbsp;&nbsp;&nbsp;&nbsp;def f2(self):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "bar"
> > > 
> > > I have a hacky solution like:
> > > 
> > > class MyService:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;name = "my\_service"
> > > 
> > > def wrapper(f):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;def func(self, \*args, \*\*kwargs):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return f(\*args, \*\*kwargs)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;return func
> > > 
> > > def add\_rpc\_endpoints\_from\_dict(registry):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;for name, f in registry.items():  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g = wrapper(f)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setattr(MyService, name, g)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rpc(g)
> > > 
> > > Is there a better way? Or a better way to solve this problem in general?
> > > 
> > > Thanks  
> > > Alex
