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. To run the server you need to have docker installed.
> 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.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73f9e562aa0b7e342abff48471488961f00e56c7fcf925851852de54aafdc629 |
|
MD5 | 1c34d7eff6568969c8f1c6e66cc3342b |
|
BLAKE2b-256 | 1a89a65cb5a6aa6628a1c5d87ddc2ca7060d944b8d04b7a8a523e0c39736c42d |