构建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);

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java 安卓 XMLPullParser错误   Java响应网格布局   Java中使用递归的不同方法   java变量可能尚未初始化(边加权图)   java如何将这样的文件插入MySQL数据库   java云端点:不允许实体类型的数组或集合   java(编译器或jvm)是否以不同方式处理类的静态最终成员?如果是,怎么做   java如何从lambda表达式返回新的非抽象映射?   java JDK 7支持的最大Spring版本是什么   我们如何从java类生成DTD   java在我的例子中BindingResult、FieldErrors或GlobalErrors是否可以为null?   java[LibGDX][GWT]读取文件外观时出错。HTML格式的json[序列化异常]   字典Java不可修改密钥集映射   java Admob在firebase sdk之后显示测试广告,但不显示真实广告