zask.ext.zerorpc package¶
Module contents¶
zask.ext.zerorpc¶
Add zerorpc support to zask.
copyright: |
|
---|---|
license: | BSD, see LICENSE for more details. |
-
class
zask.ext.zerorpc.
AccessLogMiddleware
(app)[source]¶ Bases:
object
This can’t be used before initialize the logger.
-
class
zask.ext.zerorpc.
ConfigCustomHeaderMiddleware
(app)[source]¶ Bases:
zask.ext.zerorpc.ConfigEndpointMiddleware
Besides resolve the endpoint by service name, add custome header to the client.
Server side will do the validation for the access key and service version.
-
class
zask.ext.zerorpc.
ConfigEndpointMiddleware
(app)[source]¶ Bases:
zask.ext.zerorpc.ConfigMiddleware
Resolve the endpoint by service name.
-
class
zask.ext.zerorpc.
ConfigMiddleware
(app)[source]¶ Bases:
object
A middleware work with configure of zask application.
This is the base class for all the config based middlewares.
-
exception
zask.ext.zerorpc.
MissingAccessKeyException
(config_name)[source]¶ Bases:
exceptions.Exception
-
exception
zask.ext.zerorpc.
MissingMiddlewareException
(middleware)[source]¶ Bases:
exceptions.Exception
Raised when Zask tries to invoke a functionality provided by a specific middleware, but that middleware is not loaded.
-
exception
zask.ext.zerorpc.
NoSuchAccessKeyException
(access_key)[source]¶ Bases:
exceptions.Exception
-
class
zask.ext.zerorpc.
RequestChainMiddleware
(app)[source]¶ Bases:
object
Generate UUID for requests and store in greenlet’s local storage
-
class
zask.ext.zerorpc.
RequestEventMiddleware
[source]¶ Bases:
object
Exposes the request_event to the object being passed to Server() via self.get_request_event() from a service endpoint.
-
exception
zask.ext.zerorpc.
VersionNotMatchException
(access_key, request_version, server_version)[source]¶ Bases:
exceptions.Exception
-
class
zask.ext.zerorpc.
ZeroRPC
(app=None, middlewares=['header', 'uuid', 'access_log', 'event'])[source]¶ Bases:
object
This is a class used to integrate zerorpc to the Zask application.
ZeroRPC extention provides a few powful middlewares.
Take
CONFIG_ENDPOINT_MIDDLEWARE
as example, which will resolve endpoint according to the zask application configuration. To use that you can setup a ZeroRPC like this:app = Zask(__name__) app.config['ZERORPC_SOME_SERVICE'] = { '1.0': endpoint, } rpc = ZeroRPC(app, middlewares=[CONFIG_ENDPOINT_MIDDLEWARE])
Then create a server and a client:
class Srv(object): __version__ = "1.0" __service_name__ = "some_service" def hello(self): return 'world' client = rpc.Client('some_service', version='1.0') client.hello()
Application will look for
RPC_SOME_SERVICE
config. You can set a default version to make the client initialization more easier:app.config['ZERORPC_SOME_SERVICE'] = { '1.0': endpoint, '2.0': [ # set list if you have multiple endpoints endpoint1, endpoint2 ] 'default': '1.0' } client = rpc.Client('some_service') client.hello()
But if you don’t want to use the middlewares, just set
middlewares
toNone
:app = Zask(__name__) rpc = ZeroRPC(app, middlewares=None)
Or set a new context to the Server/Client during the runtime:
app = Zask(__name__) rpc = ZeroRPC(app, middlewares=[CONFIG_ENDPOINT_MIDDLEWARE]) default_context = zerorpc.Context().get_instance() srv = rpc.Server(Srv(), context=default_context) client = rpc.Client(context=default_context)
-
zask.ext.zerorpc.
access_log
(cls)[source]¶ [Deprecated] A decorator for zerorpc server class to generate access logs:
@access_log Class MySrv(Object): def foo(self) return "bar"
Every request from client will create a log:
[2014-12-18 13:33:16,433] - None - "MySrv" - "foo" - OK - 1ms
Parameters: cls – the class object