Skip to main content

Easy gRPC-web client in python

Project description

pyease-grpc

Build Release PyPI version Python version GitHub License Downloads

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.

Source: https://github.com/grpc/grpc-web#streaming-support

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pyease_grpc-1.5.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file pyease_grpc-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: pyease_grpc-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.10.1 urllib3/1.26.13 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for pyease_grpc-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 73f9e562aa0b7e342abff48471488961f00e56c7fcf925851852de54aafdc629
MD5 1c34d7eff6568969c8f1c6e66cc3342b
BLAKE2b-256 1a89a65cb5a6aa6628a1c5d87ddc2ca7060d944b8d04b7a8a523e0c39736c42d

See more details on using hashes here.

Supported by

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