Programmatically adding endpoints

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():
    return "foo"

def myf2():
     return "bar"

d = {
  "f1": f1
  "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:
    name = "my_service"

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

I have a hacky solution like:

class MyService:
    name = "my_service"

def wrapper(f):
    def func(self, *args, **kwargs):
        return f(*args, **kwargs)
    return func

def add_rpc_endpoints_from_dict(registry):
    for name, f in registry.items():
        g = wrapper(f)
        setattr(MyService, name, g)
        rpc(g)

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

Thanks
Alex

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():
    return "foo"

def myf2():
     return "bar"

d = {
  "f1": f1
  "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:
    name = "my_service"

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

I have a hacky solution like:

class MyService:
    name = "my_service"

def wrapper(f):
    def func(self, *args, **kwargs):
        return f(*args, **kwargs)
    return func

def add_rpc_endpoints_from_dict(registry):
    for name, f in registry.items():
        g = wrapper(f)
        setattr(MyService, name, g)
        rpc(g)

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

Thanks
Alex

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():
    return "foo"

def myf2():
     return "bar"

d = {
  "f1": f1
  "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:
    name = "my_service"

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

I have a hacky solution like:

class MyService:
    name = "my_service"

def wrapper(f):
    def func(self, *args, **kwargs):
        return f(*args, **kwargs)
    return func

def add_rpc_endpoints_from_dict(registry):
    for name, f in registry.items():
        g = wrapper(f)
        setattr(MyService, name, g)
        rpc(g)

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

Thanks
Alex