Skip to main content

GRPC support for Muffin framework.

Project description

Muffin-GRPC – GRPC support for Muffin framework.

Features:

  • Automatically build proto files and python helpers for them;

  • Automatically connect to default channel;

  • Automatically create and run GRPC server from your services;

Tests Status PYPI Version

Requirements

  • python >= 3.7

Installation

Muffin-GRPC should be installed using pip:

pip install muffin-grpc

Usage

Setup the plugin and connect it into your app:

from muffin import Application
from muffin_grpc import Plugin as GRPC

# Create Muffin Application
app = Application('example')

# Initialize the plugin
# As alternative: grpc = GRPC(app, **options)
grpc = GRPC(default_channel='server:50051')
grpc.setup(app)

Lets build a simple helloworld service, with the proto:

syntax = "proto3";

package helloworld;

service Greeter {
    rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
    string name = 1;
}

message HelloReply {
    string message = 1;
}

Put it somewhere and add the file into the grpc plugin:

grpc.add_proto('project_name/proto/helloworld.proto')

Run the command to build proto files:

$ muffin project_name grpc_build

The command will build the files:

  • project_name/proto/helloworld_pb2.py - with the proto’s messages

  • project_name/proto/helloworld_pb2_grpc.py - with the proto’s GRPC services

  • project_name/proto/helloworld.py - with the messages and services together

  • project_name/proto/__init__.py - to make the build directory a package

Let’s implement the Greeter service:

from .proto.helloworld import GreeterServicer, HelloRequest, HelloReply

# Connect the service to GRPC server
@grpc.add_to_server
class Greeter(GreeterServicer):

    async def SayHello(self, request: HelloRequest,
                    context: grpc_aio.ServicerContext) -> HelloReply:
        return HelloReply(message='Hello, %s!' % request.name)

Run the server with the command:

$ muffin package_name grpc_server

The server is working and accepts GRPC request, let’s start building a client

from .proto.helloworld import GreeterStub, HelloRequest

@app.route('/')
async def index(request):
    name = request.url.query.get('name') or 'anonymous'
    try:
        async with grpc.channel() as channel:
            stub = GreeterStub(channel)
            response = await stub.SayHello(
                HelloRequest(name=request.url.query['name']), timeout=10)
            message = response.message

    except AioRpcError as exc:
        message = exc.details()

    return message

The / endpoint will make a request to the GRPC server and return a message from the server.

Configuration options

Name

Default value

Desctiption

build_dir

None

A directory to build proto files

server_listen

"[::]:50051"

Server address

ssl_server

False

Enable SSL for server

ssl_server_params

None

SSL Server Params

ssl_client

False

Enable SSL for client

ssl_client_params

None

SSL Client Params

default_channel

localhost:50051

Default Client Channel Address

default_channel_options

{}

GRPC options for the default channel

You are able to provide the options when you are initiliazing the plugin:

grpc.setup(app, server_listen='localhost:40000')

Or setup it from Muffin.Application configuration using the GRPC_ prefix:

GRPC_SERVER_LISTERN = 'locahost:40000'

Muffin.Application configuration options are case insensitive

CLI Commands

$ muffin project_name grpc_build --help

usage: muffin grpc_build [-h]

Build registered proto files.

optional arguments:
-h, --help  show this help message and exit
$ muffin project_name grpc_server --help

usage: muffin grpc_server [-h]

Start GRPC server with the registered endpoints.

optional arguments:
-h, --help  show this help message and exit

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/muffin-grpc/issues

Contributing

Development of Muffin-GRPC happens at: https://github.com/klen/muffin-grpc

Contributors

  • klen (Kirill Klenov)

License

Licensed under a MIT license.

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

muffin-grpc-0.3.4.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

muffin_grpc-0.3.4-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file muffin-grpc-0.3.4.tar.gz.

File metadata

  • Download URL: muffin-grpc-0.3.4.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for muffin-grpc-0.3.4.tar.gz
Algorithm Hash digest
SHA256 b6be2baecb74f232bc2ff009631c8ec9e65c031cc11b340282fdbae038a37662
MD5 72a1442d53af84b8c4c7e01fc72a51d9
BLAKE2b-256 da3ab9addb321791c09fc191eb28e7cac0b4752bc5f2d81ba5a28119fb0a2a0f

See more details on using hashes here.

File details

Details for the file muffin_grpc-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: muffin_grpc-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for muffin_grpc-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 86243db237988c43171414c7c39f04fce30d4dbca7a88f5bf14fdeeda65631cb
MD5 5065dba1c0b25f89701031c7913d53ce
BLAKE2b-256 ece49eb322ecb1595b32dfa257e351bf2d314bba136d0b8b674dd337a8fb3c5f

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