Skip to main content

Python JSON-RPC Framework

Project description

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"}}

Project details


Download files

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

Source Distribution

apium-dev-0.1.1.tar.gz (7.3 kB view details)

Uploaded Source

File details

Details for the file apium-dev-0.1.1.tar.gz.

File metadata

  • Download URL: apium-dev-0.1.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.8

File hashes

Hashes for apium-dev-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c3b7ef0ca51e77fbd85af2bb661143aac69ec52ed52098853495f206ede8cc45
MD5 c9ac002664cdf0c83f35004b49f23dbb
BLAKE2b-256 9ee23f0ea49a3101b77f8ff76fabb484452f5fab88bef8ec092ed41e15d3eb0b

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