0

spyne.io的示例中:

class HelloWorldService(ServiceBase):
    @srpc(Unicode, Integer, _returns=Iterable(Unicode))
    def say_hello(name, times):
        for i in range(times):
            yield 'Hello, %s' % name

application = Application([HelloWorldService],
    tns='spyne.examples.hello',
    in_protocol=JsonDocument(validator='soft'),
    out_protocol=JsonDocument()
)

这意味着被调用的方法名称必须是 JSON 正文的唯一键:

$ curl -s http://localhost:8000/ -d '{"say_hello": {"name": "World", "times": 5}}'

我希望正文不包含方法名称,而是从 URL 中获取方法名称,就像使用 HttpRpc 一样:

$ curl -s http://localhost:8000/say_hello -d '{"name": "World", "times": 5}'

我如何定义能够处理此类请求的服务?

4

1 回答 1

1

您可以从JsonDocument. 你只需要重写一种方法:

# Tested with spyne.__version__=='2.10.9'
class warwarukDocument(JsonDocument):
    """An implementation of the json protocol
       with the method name stored in the URL,
       not the document."""

    def create_in_document(self, ctx, in_string_encoding=None):
        """ Sets ``ctx.in_document`` using ``ctx.in_string``."""
        assert ctx.transport.type.endswith('http'), \
               "This protocol requires an http transport, not %r (in %r)" \
                   % (ctx.transport.type, ctx.transport)

        super(warwarukDocument, self).create_in_document(ctx, in_string_encoding)
        # Not 100% sure where to find the URL path
        try:
            # Works for twisted
            uri = ctx.transport.req.uri
        except AttributeError:
            # Works for WSGI
            uri = ctx.transport.req['PATH_INFO']
        uri = re.sub(r'.*/', '', uri)
        ctx.in_document = { uri : ctx.in_document }


application = Application([HelloWorldService],
    tns='spyne.examples.hello',
    in_protocol=warwarukDocument(validator='soft'),
    out_protocol=JsonDocument()
)

用法:

$ curl -s http://localhost:8000/say_hello \
      -d '{"name": "World", "times": 1}' | python -m json.tool
[
    "Hello, World"
]
$ curl -s http://localhost:8000/say_goodbye \
      -d '{"name": "World", "times": 1}' | python -m json.tool
{
    "detail": null, 
    "faultcode": "Client.ResourceNotFound", 
    "faultstring": "Requested resource \"Method u'{spyne.examples.hello}say_goodbye' not found.\" not found"
}
于 2013-09-20T15:29:35.373 回答