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

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


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.3.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyease_grpc-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.15

File hashes

Hashes for pyease_grpc-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 006a6c0d3f381a9cd8f30888c3967632645618359ae1984b1ee48e30a855269c
MD5 362a49247b703fa209f2cc7d17a607e4
BLAKE2b-256 f0006d2d6cb297a77f0359f7aba6ad2c8f5d2191ff99ad9280230a2c8e3bf5b1

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