pyrpc-django 1.0

Creator: codyrutscher

Last updated:

Add to Cart

Description:

pyrpcdjango 1.0

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

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.