A Django app to conduct Web-based polls.
Project description
Pyrpc is a Django app to handle JSON Remote Procedure Calls using Django Rest Framework.
Installation
pip install pyrpc-django
Quick Setup
Add rest_framework pyrpcto your INSTALLED_APPS setting like this:
INSTALLED_APPS = [ ... 'rest_framework', 'pyrpc', ]Add the safe_method decorator to methods in your app.
from pyrpc.decorators import safe_method
@safe_method
def return_cat_string(*args, **kwargs):
"""
Returns a concatenated string.
Extended description of function.
@param args: List of strings
@returns: Concatenated string
"""
result = ''
for arg in args:
result = result + arg
return result
class Library():
@safe_method
def sum_two_numbers(self, operand1=0, operand2=0):
"""
Returns the sum of two numbers.
Extended description of function.
@param operand1: Can be any float number.
@param operand2: Can be any float number.
@returns: operand1 + operand2.
"""
return operand1 + operand2
@safe_method
def multiply_two_numbers(self, operand1=0, operand2=0):
"""
Returns the product of two numbers.
Extended description of function.
@param operand1: Can be any float number.
@param operand2: Can be any float number.
@returns: operand1 * operand2.
"""
return operand1 * operand2
Add pyrpc urls to urls.py
from django.urls import path
from django.conf.urls import include
from pyrpc.urls import urls as pyrpc_urls
urlpatterns = [
path('api/', include(pyrpc_urls)),
]
Start the server: python manage.py runserver
Sending Requests
Using the above example, POST the folowing JSON to 127.0.0.1:8000/api/methods/.
{
"id": 1,
"jsonrpc": "2.0",
"method": "sum_two_numbers",
"params": {
"args": [],
"kwargs": {
"operand1": 5,
"operand2": 6
}
}
}
A JSON response should be returned similar to the folowing:
{
"id": 1,
"jsonrpc": "2.0",
"result": 11
}
Returning a List of Methods
Using the previous example, send a GET request to 127.0.0.1:8000/api/methods.
A list of methods and their descriptions shold be returned as follows:
[
{
"name": "multiply_two_numbers",
"kwargs": {
"operand1": "Can be any float number.",
"operand2": "Can be any float number."
},
"description": [
"Returns the product of two numbers.",
"Extended description of function."
],
"returns": "operand1 * operand2."
},
{
"name": "sum_two_numbers",
"kwargs": {
"operand1": "Can be any float number.",
"operand2": "Can be any float number."
},
"description": [
"Returns the sum of two numbers.",
"Extended description of function."
],
"returns": "operand1 + operand2."
}
]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pyrpc-django-1.0.tar.gz.
File metadata
- Download URL: pyrpc-django-1.0.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
745405767d4273590771ea947a9f35d1598300ae727ad6065a911e3bca810a7e
|
|
| MD5 |
a382b60581308bb39c6ebb264ec2e99c
|
|
| BLAKE2b-256 |
f9c6316b97cf6ff85628f26c6ac0f3f6043a2691c7f1d30208fc37a38476821a
|