Skip to main content

A Python RPC Framework

Project description

# Features
* Easy registration of methods
* Custom Exception and Options
* Includes plenty testing helpers

# Installation
On most systems, its a matter of
```bash
pip install qrpc
```

# Quickstart
## A Simple Example
This is a realy simple example of how to create your own API using QRpc

### Server

```python
from qrpc.server import Server

server = Server()


@server.registe("service1/hello")
def hello(name=None):
if name:
return "hello " + name
return "hello anonymous"


@server.registe("service1/add")
def add(x, y):
return x + y


server.run('127.0.0.1', 8080)
```


### Client

```python
from qrpc.client import RpcClient
from qrpc.client import ServerProxy

server = ServerProxy('http://localhost:8080/v1/batch')
rpc = RpcClient(server=server)
result = rpc.service1.hello.call(name='ycc')
print(result.data)

```
# Core content
## Request


## Response(Result)
The response of an rpc request has three attributes that user should concern, rpc_code, data, message.

The rpc_code indicates if the rpc request has been successfully received, understood, and accepted. 0 and all the positive numbers are **reserved code**, and can't be used by user.

The data is the result of an rpc method.

The message provides some helpful information.


## Exceptions
You can define an RPCException and raise it when you want to tell the caller there is something wrong in some rpc method. For example a division rpc method and the second argument of a division is zero.

```python
# server
from qrpc.exceptions import RPCFaultException
@server.registe("service/div")
def test_div(x, y):
if y == 0:
raise RPCFaultException(
code=99, # Use any code in your as you like except reserved code.
message="ZeroDivisionError: integer division or modulo by zero"
)
return x / y

# client
div_result = rpc.service.div.call(x=1, y=0)
```

The QRpc will catch the exception and wrap it in reponse.




#Lazy Call and Evaluation

RPC call are lazy--the act of creating an rpc call doesn't send the network request to server. You can stack call together all day long, and the framework won't actually send the network request until one of the calls is evaluated. You can get detail from the following example:

```python
add_result = rpc.service.add.call(x=1, y=2)
dict_result = rpc.service.dictionary.call(dictionary={"test_key": "test_value"})
hello_result = rpc.service.hello.call(name='world')
print (add_result.data) # only one network request
print (dict_result.data)
print (hello_result.data)
```

Though this looks like sending three rpc call request, in fact it only send one network request, at the "add_result.data" line. An rpc call is just added into a job list when it is constructed. The real network request will be executed when any of the 'rcp call' in the job list is evaluated. The framework evaluates all the rpc call in the job list at one time. So only send one network request.You can evaluate an rpc call by get any attribute of the rcp result.

In the last example, there are three rpc calls in the job list. The three rpc calls are evaluated at one network request when you get the data of add_result.
So dict_result.data or hello_result.data won't cause any network request.

In general, the result of an rpc call isn't fetched from the server until you ask them.

# Adcanced Usage
# TODO

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

qrpc-0.1.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qrpc-0.1.0-py2-none-any.whl (12.8 kB view details)

Uploaded Python 2

File details

Details for the file qrpc-0.1.0.tar.gz.

File metadata

  • Download URL: qrpc-0.1.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for qrpc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e8e4e65561a90a218a4ed3e79a704ab562f676bd999df957d6bf797807a27d77
MD5 c582bc99ecc2dc23d883dca71f0938bf
BLAKE2b-256 7127f9fc24fc4304d574209b3a89dc3b70d723029d5820622ad5567f7943f456

See more details on using hashes here.

File details

Details for the file qrpc-0.1.0-py2-none-any.whl.

File metadata

  • Download URL: qrpc-0.1.0-py2-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for qrpc-0.1.0-py2-none-any.whl
Algorithm Hash digest
SHA256 c89632ca1fc6bd7a0bde14fe1ec96882a5e90cb1e2568ebf9b2e43f67db65e4a
MD5 c041363cdcb561fa0ab15aaac60bd7c1
BLAKE2b-256 4bb2ecbb9d566110bc81dcffd1f96f275dcf323b2fdb6f5317b8ae0c3fceb550

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page