Skip to content

katsko/apium

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Apium

Python JSON-RPC Framework

Install

pip install apium-dev

Example

Create RPC method

Each method must be in path: api/<method_name>/method.py

Create file api/hello/method.py:

import apium
from apium import af


class Hello(apium.Method):
    name = af.Str(required=True)
    age = af.Int(default=18)

    def execute(self):
        return {'msg': f'Hi {self.name}, you are {self.age}'}

Settings web-framework

Associate URL to handler.

Django

Edit urls.py in your django project:

from django.urls import path
from apium.handlers.django.handler import api_handler

urlpatterns = [
    # ... your other path, e. g. 'admin/'
    path('api/', api_handler),
]

Flask

#!/usr/bin/env python
from flask import Flask
from apium.handlers.flask.handler import api_handler


app = Flask(__name__)
app.add_url_rule('/api/', view_func=api_handler, methods=['POST'])
app.run('localhost', 8080, debug=True)

JSON-RPC request

First example (without age)

JSON:

{
    "jsonrpc":"2.0",
    "id": 1,
    "method": "hello",
    "params": {
        "name": "John"
    }
}

Send request:

curl http://127.0.0.1:8080/api/ \
    -X POST \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc": "2.0", "id": 1, "method": "hello", "params": {"name": "John"}}'

Response:

{"jsonrpc": "2.0", "id": 1, "result": {"msg": "Hi John, you are 18"}}

Second example (with age)

JSON:

{
    "jsonrpc":"2.0",
    "id": 1,
    "method": "hello",
    "params": {
        "name": "Smith",
        "age": 20
    }
}

Send request:

curl http://127.0.0.1:8080/api/ \
    -X POST \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc": "2.0", "id": 1, "method": "hello", "params": {"name": "Smith", "age": 20}}'

Response:

{"jsonrpc": "2.0", "id": 1, "result": {"msg": "Hi Smith, you are 20"}}

About

Python JSON-RPC Framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages