构建web(json-rpc)api的框架。
webapi的Python项目详细描述
webapi
构建web(json-rpc)api的框架。
快速启动
定义消息和服务:
class TestRequest(messages.Message): string = messages.StringField(1) class TestResponse(messages.Message): string = messages.StringField(1) @webapi.package('examplepackage') class ExampleService(object): @webapi.method(TestRequest, TestResponse, 'examplemethod') def test(self, request): """ an identity function""" assert isinstance(request, TestRequest) return TestResponse(string=request.string)
构建wsgi应用程序:
API = webapi.api([ExampleService], 'exampleapi', 'v1') routes = [ webapp2.Route('/rpc', webapi.rpc_handler(API)) ] app = webapp2.WSGIApplication(routes)
api应该在/rpc上可用。
方法exampleAPI.examplePackage.exampleMethod应可供调用方使用。
导出发现应用程序
用python定义:
API = webapi.api([TestService], 'testapi', 'v1') api_directory_app = webapi.APIDirectory([API])
向app.yaml注册以匹配路径/discovery/*。
使用javascript客户端。
使用基本客户端
包括编译的脚本。
安装:
var RPC_URL = '/rpc'; webapi.client.install(new webapi.RpcClient(RPC_URL));
调用方法:
var METHOD = 'exampleapi.examplepackage.examplemethod' var req = webapi.client.rpc(METHOD, {'string': 'test'}); req.addCallback(function(resp){ // this should return "test" console.log(resp) }); req.addErrback(function(resp){ // jsonrpc error objects can be handled here console.error(resp) });
使用目录客户端
包括编译的脚本。
安装:
var RPC_URL = '/rpc'; webapi.client.install(new webapi.DiscoveryClient());
加载API(从目录):
webapi.client.load(apiName, apiVersion, root)
加载rest描述(从json对象):
webapi.client.loadDescription(rawRestDescription)
加载api之后,其方法将导出到webapi命名空间。例如:
var request = webapi.apiname.package.method();
执行rpc方法(手动):
var request = webapi.client.rpc(methodId, params);