Skip to main content

A framework to build web (json-rpc) APIs.

Project description

webapi

A framework to build web (json-rpc) APIs.

Quick Start

Defining messages and a service:

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)

Building a wsgi application:

API = webapi.api([ExampleService], 'exampleapi', 'v1')

routes = [
    webapp2.Route('/rpc', webapi.rpc_handler(API))
]
app = webapp2.WSGIApplication(routes)

The api should be available at /rpc.

A method exampleapi.examplepackage.examplemethod should be available to callers.

Exporting a discovery application

Define in python:

API = webapi.api([TestService], 'testapi', 'v1')

api_directory_app = webapi.APIDirectory([API])

Register with app.yaml to match the path /discovery/.*.

Using the javascript client.

Using the basic client

Include the compiled script.

Installing:

var RPC_URL = '/rpc';

webapi.client.install(new webapi.RpcClient(RPC_URL));

Calling a method:

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)
});

Using the directory client

Include the compiled script.

Installing:

var RPC_URL = '/rpc';

webapi.client.install(new webapi.DiscoveryClient());

Loading an api (from a directory):

webapi.client.load(apiName, apiVersion, root)

Loading a rest description (from a json object):

webapi.client.loadDescription(rawRestDescription)

After loading an api its methods will be exported to the webapi namespace. For example:

var request = webapi.apiname.package.method();

Executing an rpc method (manually):

var request = webapi.client.rpc(methodId, params);

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

webapi-0.1.tar.gz (20.0 kB view details)

Uploaded Source

File details

Details for the file webapi-0.1.tar.gz.

File metadata

  • Download URL: webapi-0.1.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for webapi-0.1.tar.gz
Algorithm Hash digest
SHA256 3c489bd26d3d0b14cf1ffa5eac4cfd86c3a1b839b767cdc72913365f493317d8
MD5 8feb45cbc4e3c82d77c93deb1b6cbd96
BLAKE2b-256 7f825357a60827f21b4aa4ec28f97397bcb408910a3ab4f3312b0a3306aa372f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page