A cadRPC client library for Python
Project description
cadRPC client library for Python
cadRPC is an easy-to-use RPC technology. It's goal is to simplify the communication with your web API, hiding all the HTTP and JSON poppycock.
Installation
Install the cadmean-rpckit
package:
pip install cadmean-rpckit
How to use
An example is worth a thousand words. Note, you can run the examples yourself, as they call functions of the example server at testrpc.cadmean.ru.
The easiest way to call an RPC function is using a decorator.
Synchronous example:
from rpckit.decorators import RpcFunction
# You need to set this url only once in your program
RpcFunction.default_server_url = "http://testrpc.cadmean.ru"
@RpcFunction("sum")
def rpc_sum(a, b):
print(f"Calling rpc function that calculates {a}+{b}")
print(rpc_sum(5, 64))
Async/await example:
import asyncio
from rpckit.decorators import RpcFunction
# You need to set this url only once in your program
RpcFunction.default_server_url = "http://testrpc.cadmean.ru"
@RpcFunction("sum", async_call=True)
def rpc_sum(a, b):
print(f"Calling rpc function that calculates {a}+{b}")
async def main():
print(await rpc_sum(5, 64))
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
So, all you need to do to call an RPC function from python,
is to define a regular function with parameters,
that will match the ones of the remote function
and than decorate it with @RpcFunction
, that takes
remote function name as parameter.
You can add async_call=True
to the decorator, than
function call will be asynchronous, and you
will be able to use your function with async/await
.
Error handling
cadRPC function may return an error code instead of a result.
In this case this library rises FunctionException
.
It can be a system error or user defined error,
that you return from RPC function in your server.
You would probably want to handle these errors in your program:
from rpckit.decorators import RpcFunction
from rpckit.exceptions import FunctionException
from rpckit.constants import RpcErrorCode
RpcFunction.default_server_url = "http://testrpc.cadmean.ru"
@RpcFunction("notExistingFunction")
def not_existing_function():
pass
try:
not_existing_function()
except FunctionException as ex:
print(ex)
if ex.code == RpcErrorCode.FunctionNotFound:
print("Ok. We expected that.")
else:
print("Something bad happened.")
Authorization
adRPC simplifies authorization process in your web API. You just call a function on your RPC server, that will authenticate the user and return a JWT authorization ticket(access and refresh token). The client will store this ticket for you and will authorize further calls with the access token. Note, that you need to setup authorization on your server (see this readme).
Here is an example:
from rpckit.decorators import RpcFunction
RpcFunction.default_server_url = "http://testrpc.cadmean.ru"
@RpcFunction("auth")
def auth(email, password):
pass
@RpcFunction("user.get")
def get_user():
pass
auth("email@example.com", "password") # Call the authentication function, that returns the JWT tokens
user = get_user() # Call the function that requires authorization after obtaining JWT tokens
print(user)
Contributing
Feel free to submit issues or create pull requests following fork-and-pull git workflow.
See also
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
Built Distribution
File details
Details for the file cadmean-rpckit-0.2.tar.gz
.
File metadata
- Download URL: cadmean-rpckit-0.2.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aad0b9b936589ca8a4bcd469fa968f13539a585e7dac68b4774751d187323aa3 |
|
MD5 | 6795dd478096783884c822859cdcad9a |
|
BLAKE2b-256 | 5df92e69e05a4851206066d23deae265780ee5c159756bed9c71a923891d8362 |
File details
Details for the file cadmean_rpckit-0.2-py3-none-any.whl
.
File metadata
- Download URL: cadmean_rpckit-0.2-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b5e450fc17c6ce7c930cf932b8468d061eb76f74940e112168641c0606c4339 |
|
MD5 | b392a0bda8c83b2419311a4f4308f41c |
|
BLAKE2b-256 | e238f79e4ef9fa6253d4d3e2d027770299278cc83f9ac89ab0bb60afef1dc7c5 |