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.2.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

flask_grpc_bridge-0.0.2-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.2.tar.gz.

File metadata

  • Download URL: flask_grpc_bridge-0.0.2.tar.gz
  • Upload date:
  • Size: 4.7 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.2.tar.gz
Algorithm Hash digest
SHA256 bfe67dad2775b3cc22f03528744bb0c5dbb049242897e5ff34df275dcdf9979c
MD5 6444412c7c82951c957758e3c85bcbb6
BLAKE2b-256 20495601be01a10f1202b18427901c65e8c728c06e0c376562325e978e27a45c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_grpc_bridge-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e877f2278ecd4fc181377bed9d71bb93830819dfa522c2ba9c514f993927f3db
MD5 9a7d1b3fcd07bf8c1856accf2204c70a
BLAKE2b-256 430409e3f02f514077a7e34855718109133d4d2c39c1f9be0565097f6d3f7e64

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