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
You need to have a basic understanding of how gRPC works.
An example server and client can be found in the example
folder.
> cd example
> docker compose up
It runs a grpc server and creates an envoy proxy for grpc-web access.
The grpc server address: localhost:50050
The envoy proxy address: http://localhost:8080
Now let's check the example/server
folder for the abc.proto
file:
// file: example/server/abc.proto
syntax = "proto3";
package pyease.sample.v1;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloResponse);
rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);
rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);
rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string reply = 1;
}
To make a request to SayHello
:
from pyease_grpc import Protobuf, RpcSession, RpcUri
protobuf = Protobuf.from_file("example/server/abc.proto")
session = RpcSession(protobuf)
response = session.request(
RpcUri(
"http://localhost:8080",
package="pyease.sample.v1",
service="Greeter",
method="SayHello",
),
{
"name": "world"
},
)
response.raise_for_status()
result = response.single
print(result['reply'])
The session.request
accepts request data as a dict
and returns the response as a dict
.
gRPC-web currently supports 2 RPC modes: Unary RPCs, Server-side Streaming RPCs. Client-side and Bi-directional streaming is not currently supported.
Descriptor
The FileDescriptorSet
is the faster way to load Protobuf.
To generate FileDescriptorSet
as json:
$ pyease-grpc -I example/server example/server/abc.proto --output abc_fds.json
Now you can use this descriptor file directly to create a Protobuf
instance.
# Use the descriptor directly to create protobuf instance
protobuf = Protobuf.restore_file('abc_fds.json')
# You can even create the session directly from descriptor
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
Hashes for pyease_grpc-1.5.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a550d56ddeb45f1cf7269ff54c5c15ad169b24ed1136e085e5ec43babe2a757 |
|
MD5 | 28daf10461b1ddcde513feadc9b7ce72 |
|
BLAKE2b-256 | fd701acba633ebde50279759849da8b6bd01c4be08e34833a39906d50e3d2999 |