grpc-requests: gRPC for Humans

0.1.21 · active · verified Mon Apr 13

grpc-requests is a Python library that simplifies interaction with gRPC services, especially those that implement gRPC reflection. It aims to provide a `requests`-like API for gRPC, abstracting away much of the boilerplate associated with standard gRPC client code, allowing for more dynamic interaction without pre-generated stubs. The current version is 0.1.21, with a regular release cadence addressing compatibility, bug fixes, and new features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a synchronous gRPC client using `grpc-requests` to interact with a server via reflection. It connects to a specified endpoint, retrieves available service names, and then makes a `SayHello` RPC call to a 'helloworld.Greeter' service. This requires a running gRPC server with reflection enabled and the 'helloworld.Greeter' service implemented.

import os
from grpc_requests import Client

# Replace with your gRPC server endpoint (e.g., 'localhost:50051')
# Ensure your gRPC server has reflection enabled.
GRPC_SERVER_ENDPOINT = os.environ.get('GRPC_SERVER_ENDPOINT', 'localhost:50051')

# Example: If your server requires TLS
# client = Client.get_by_endpoint(GRPC_SERVER_ENDPOINT, ssl=True)

client = Client.get_by_endpoint(GRPC_SERVER_ENDPOINT)

print(f"Connected to gRPC server at {GRPC_SERVER_ENDPOINT}")

# Assuming a 'helloworld.Greeter' service with a 'SayHello' method
# and that the server supports reflection and this service.
try:
    service_names = client.service_names
    if 'helloworld.Greeter' in service_names:
        print(f"Available services: {service_names}")
        request_data = {"name": "World"}
        response = client.request("helloworld.Greeter", "SayHello", request_data)
        print(f"Response from SayHello: {response}")
    else:
        print("helloworld.Greeter service not found on the server.")
except Exception as e:
    print(f"Error interacting with gRPC server: {e}")
    print("Ensure a gRPC server with reflection is running at the specified endpoint and 'helloworld.Greeter' service is available.")

view raw JSON →