Skip to main content

Bridge between Flask and gRPC definitions.

Project description

flask-grpc-bridge

Bridge between Flask and gRPC. flask_grpc_bridge allows you to define RPC services and methods using protobufs, but serve them using Flask. The resulting Flask endpoints can accept either JSON or binary representations of the request messages, and return the corresponding response message.

Who is this for?

Two cases that I've run across in various jobs:

  • Existing Flask services that want some stronger typing and validation on the incoming requests, or that are being called from strongly-typed languages (like golang), and want to use a shared schema, or don't want to deal with JSON requests and responses.
  • Existing gRPC services that want to be called from web browsers (or other systems that might not have good protobuf support). These clients need to be able to pass JSON requests and receive JSON responses, but they don't want to rewrite the service logic.

Example usage

Given a gRPC service definition (from the gRPC documentation):

// hello_world.proto
syntax = "proto3";

// The greeter service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

You can set up a regular Flask app, and add a bridge for the gRPC service:

app = Flask(__name__)
bridge = Bridge(app, hello_world_pb2, "Greeter")

Then write the implementation and decorate it:

@bridge.rpc()
def SayHello(req: HelloRequest) -> HelloReply:
    message = "Hello, " + (req.name or "world")
    resp = HelloReply(message=message)
    return resp

This will register a /Greeter/SayHello/ route with the Flask app.

  • When the endpoint receives a JSON request, it will be converted to a HelloRequest message and passed to the SayHello function. The returned HelloReply message will be converted to JSON and returned in the response.
  • If the endpoint receives a request with Content-Type: application/protobuf header, it will treat the body as a serialized protobuf, and deserialize it as a HelloRequest message. Similarly, the returned HelloReply will be serialized and returned (using the same Content-Type header).

Future work

  • Short term:
    • Better error handling and messaging.
    • More options for protobuf <-> JSON conversion (e.g. control over the ignore_unknown_fields flag, http methods)
  • Long term:

Project details


Download files

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

Source Distribution

flask_grpc_bridge-0.0.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

flask_grpc_bridge-0.0.1-py2.py3-none-any.whl (5.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file flask_grpc_bridge-0.0.1.tar.gz.

File metadata

  • Download URL: flask_grpc_bridge-0.0.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for flask_grpc_bridge-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d4c3c168ec81000b5e40b84eb79c4ce8f947a45a45ffb6bbc526de71a1794560
MD5 2e88d6c62fbdf2e9ca1135e64c974c7b
BLAKE2b-256 6763ead5051a104d50f6cb19ed464f1da9a2986679cd44b2ae153581fde4dc24

See more details on using hashes here.

File details

Details for the file flask_grpc_bridge-0.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for flask_grpc_bridge-0.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7ee6d527fa6c09bc13a3b16b5f5fd6374169d7d5344052968700e5ae5f825892
MD5 ec287e3ee4bf81664934f9eb972739f5
BLAKE2b-256 2adfc44d85e1bb073ec1a04e89235f2841360874f6a4e1dab4e867604c668500

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