Easy gRPC-web client in python
Project description
pyease-grpc
Easy to use gRPC-web client in python
Tutorial
This package provides a requests
like interface to make calls to gRPC-Web servers.
Installation
Install the package using:
$ pip install pyease-grpc
Run the following to check if it has been installed correctly:
$ pyease-grpc --version
Introduction
To use it, you need to have a basic understanding of how gRPC works.
Let's run the server first. There is an example project here: https://github.com/dipu-bd/grpc-web-example
Clone it and follow the instructions on the README.md to start the server.
Let' check the example
folder for the time_service.proto
file:
// file: time_service.proto
syntax = "proto3";
package smpl.time.api.v1;
option go_package = "github.com/kostyay/grpc-web-example/api/time/v1";
message GetCurrentTimeRequest {
}
message GetCurrentTimeResponse {
string current_time = 1;
}
service TimeService {
rpc GetCurrentTime(GetCurrentTimeRequest) returns (GetCurrentTimeResponse);
}
Suppose the gRPC web server is running at http://localhost:8080
.
To make a request to GetCurrentTime
:
from pyease_grpc import Protobuf, RpcSession
from pyease_grpc.rpc_uri import RpcUri
protobuf = Protobuf.from_file('time_service.proto')
session = RpcSession(protobuf)
response = session.request(
RpcUri('http://localhost:8080', package='smpl.time.api.v1',
service='TimeService', method='GetCurrentTime'),
{},
)
response.raise_for_status()
result = response.single
print(result['currentTime'])
The session.request
accepts a dict
as input and returns the response as dict
.
Descriptor
A more convenient way to use the client is directly using the FileDescriptorSet
.
To generate FileDescriptorSet
as json:
$ pyease-grpc -I example example/time_service.proto --output example/descriptor.json
Now you can use this descriptor file directly to create Protobuf
instance.
import json
with open('descriptor.json', 'r') as f:
descriptor = json.load(f)
# Use the descriptor directly to create protobuf instance
protobuf = Protobuf.restore(descriptor)
# You can even create the session directly
session = RpcSession.from_descriptor(descriptor)
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 Distributions
Built Distribution
File details
Details for the file pyease_grpc-1.2.1-py3-none-any.whl
.
File metadata
- Download URL: pyease_grpc-1.2.1-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec22286c82db816c747a55527829999da04c5ea188d509c55e744a5008a5db90 |
|
MD5 | 3893109d7627846238f1958ec7f30472 |
|
BLAKE2b-256 | 65e86a76d3451d38da5361e14115e9b18a4cbaa259672cbda5395591cf665bbe |