muffin-grpc


Namemuffin-grpc JSON
Version 0.6.0 PyPI version JSON
download
home_pagehttps://github.com/klen/muffin-grpc
SummaryGRPC support for Muffin framework.
upload_time2023-05-18 05:44:37
maintainer
docs_urlNone
authorKirill Klenov
requires_python>=3.8,<4.0
licenseMIT
keywords grpc muffin asyncio asgi web
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Muffin-GRPC
############

.. _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;

.. _badges:

.. image:: https://github.com/klen/muffin-grpc/workflows/tests/badge.svg
    :target: https://github.com/klen/muffin-grpc/actions
    :alt: Tests Status

.. image:: https://img.shields.io/pypi/v/muffin-grpc
    :target: https://pypi.org/project/muffin-grpc/
    :alt: PYPI Version

.. _contents:

.. contents::

.. _requirements:

Requirements
=============

- python >= 3.8

.. note:: The plugin supports only asyncio evenloop (not trio)

.. _installation:

Installation
=============

**Muffin-GRPC** should be installed using pip: ::

    pip install muffin-grpc

.. _usage:

Usage
=====

Setup the plugin and connect it into your app:

.. code-block:: python

    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:

.. code-block:: python

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


Run the command to build proto files:

.. code-block:: shell

   $ 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

.. note:: Muffin-GRPC fixes python imports automatically

Let's implement the Greeter service:

.. code-block:: python

    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:

.. code-block:: shell

   $ muffin package_name grpc_server

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

.. code-block:: python

    from .proto.helloworld import GreeterStub, HelloRequest

    @app.route('/')
    async def index(request):
        name = request.url.query.get('name') or 'anonymous'
        try:
            async with grpc.get_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:

.. code-block:: python

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

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

.. code-block:: python

   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


.. _bugtracker:

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:

Contributing
============

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


Contributors
=============

* klen_ (Kirill Klenov)

.. _license:

License
========

Licensed under a `MIT license`_.

.. _links:


.. _klen: https://github.com/klen
.. _Muffin: https://github.com/klen/muffin
.. _MIT license: http://opensource.org/licenses/MIT

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/klen/muffin-grpc",
    "name": "muffin-grpc",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "grpc,muffin,asyncio,asgi,web",
    "author": "Kirill Klenov",
    "author_email": "horneds@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/ef/7e/a9c07ac38e36d2c66ee14db606f8e4562ba1a63f137daba2d5129a423b8d/muffin_grpc-0.6.0.tar.gz",
    "platform": null,
    "description": "Muffin-GRPC\n############\n\n.. _description:\n\nMuffin-GRPC -- GRPC support for Muffin_ framework.\n\nFeatures:\n\n- Automatically build proto files and python helpers for them;\n- Automatically connect to default channel;\n- Automatically create and run GRPC server from your services;\n\n.. _badges:\n\n.. image:: https://github.com/klen/muffin-grpc/workflows/tests/badge.svg\n    :target: https://github.com/klen/muffin-grpc/actions\n    :alt: Tests Status\n\n.. image:: https://img.shields.io/pypi/v/muffin-grpc\n    :target: https://pypi.org/project/muffin-grpc/\n    :alt: PYPI Version\n\n.. _contents:\n\n.. contents::\n\n.. _requirements:\n\nRequirements\n=============\n\n- python >= 3.8\n\n.. note:: The plugin supports only asyncio evenloop (not trio)\n\n.. _installation:\n\nInstallation\n=============\n\n**Muffin-GRPC** should be installed using pip: ::\n\n    pip install muffin-grpc\n\n.. _usage:\n\nUsage\n=====\n\nSetup the plugin and connect it into your app:\n\n.. code-block:: python\n\n    from muffin import Application\n    from muffin_grpc import Plugin as GRPC\n\n    # Create Muffin Application\n    app = Application('example')\n\n    # Initialize the plugin\n    # As alternative: grpc = GRPC(app, **options)\n    grpc = GRPC(default_channel='server:50051')\n    grpc.setup(app)\n\n\nLets build a simple helloworld service, with the proto: ::\n\n    syntax = \"proto3\";\n\n    package helloworld;\n\n    service Greeter {\n        rpc SayHello (HelloRequest) returns (HelloReply) {}\n    }\n\n    message HelloRequest {\n        string name = 1;\n    }\n\n    message HelloReply {\n        string message = 1;\n    }\n\nPut it somewhere and add the file into the grpc plugin:\n\n.. code-block:: python\n\n   grpc.add_proto('project_name/proto/helloworld.proto')\n\n\nRun the command to build proto files:\n\n.. code-block:: shell\n\n   $ muffin project_name grpc_build\n\nThe command will build the files:\n\n- ``project_name/proto/helloworld_pb2.py`` - with the proto's messages\n- ``project_name/proto/helloworld_pb2_grpc.py`` - with the proto's GRPC services\n- ``project_name/proto/helloworld.py`` - with the messages and services together\n- ``project_name/proto/__init__.py`` - to make the build directory a package\n\n.. note:: Muffin-GRPC fixes python imports automatically\n\nLet's implement the Greeter service:\n\n.. code-block:: python\n\n    from .proto.helloworld import GreeterServicer, HelloRequest, HelloReply\n\n    # Connect the service to GRPC server\n    @grpc.add_to_server\n    class Greeter(GreeterServicer):\n\n        async def SayHello(self, request: HelloRequest,\n                        context: grpc_aio.ServicerContext) -> HelloReply:\n            return HelloReply(message='Hello, %s!' % request.name)\n\n\nRun the server with the command:\n\n.. code-block:: shell\n\n   $ muffin package_name grpc_server\n\nThe server is working and accepts GRPC request, let's start building a client\n\n.. code-block:: python\n\n    from .proto.helloworld import GreeterStub, HelloRequest\n\n    @app.route('/')\n    async def index(request):\n        name = request.url.query.get('name') or 'anonymous'\n        try:\n            async with grpc.get_channel() as channel:\n                stub = GreeterStub(channel)\n                response = await stub.SayHello(\n                    HelloRequest(name=request.url.query['name']), timeout=10)\n                message = response.message\n\n        except AioRpcError as exc:\n            message = exc.details()\n\n        return message\n\nThe ``/`` endpoint will make a request to the GRPC server and return a message\nfrom the server.\n\n\nConfiguration options\n----------------------\n\n=========================== ======================================= ===========================\nName                        Default value                           Desctiption\n--------------------------- --------------------------------------- ---------------------------\n**build_dir**               ``None``                                A directory to build proto files\n**server_listen**           ``\"[::]:50051\"``                        Server address\n**ssl_server**              ``False``                               Enable SSL for server\n**ssl_server_params**       ``None``                                SSL Server Params\n**ssl_client**              ``False``                               Enable SSL for client\n**ssl_client_params**       ``None``                                SSL Client Params\n**default_channel**         ``localhost:50051``                     Default Client Channel Address\n**default_channel_options** ``{}``                                  GRPC options for the default channel\n=========================== ======================================= ===========================\n\nYou are able to provide the options when you are initiliazing the plugin:\n\n.. code-block:: python\n\n    grpc.setup(app, server_listen='localhost:40000')\n\nOr setup it from ``Muffin.Application`` configuration using the ``GRPC_`` prefix:\n\n.. code-block:: python\n\n   GRPC_SERVER_LISTERN = 'locahost:40000'\n\n``Muffin.Application`` configuration options are case insensitive\n\nCLI Commands\n------------\n\n::\n\n    $ muffin project_name grpc_build --help\n\n    usage: muffin grpc_build [-h]\n\n    Build registered proto files.\n\n    optional arguments:\n    -h, --help  show this help message and exit\n\n::\n\n    $ muffin project_name grpc_server --help\n\n    usage: muffin grpc_server [-h]\n\n    Start GRPC server with the registered endpoints.\n\n    optional arguments:\n    -h, --help  show this help message and exit\n\n\n.. _bugtracker:\n\nBug tracker\n===========\n\nIf you have any suggestions, bug reports or\nannoyances please report them to the issue tracker\nat https://github.com/klen/muffin-grpc/issues\n\n.. _contributing:\n\nContributing\n============\n\nDevelopment of Muffin-GRPC happens at: https://github.com/klen/muffin-grpc\n\n\nContributors\n=============\n\n* klen_ (Kirill Klenov)\n\n.. _license:\n\nLicense\n========\n\nLicensed under a `MIT license`_.\n\n.. _links:\n\n\n.. _klen: https://github.com/klen\n.. _Muffin: https://github.com/klen/muffin\n.. _MIT license: http://opensource.org/licenses/MIT\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "GRPC support for Muffin framework.",
    "version": "0.6.0",
    "project_urls": {
        "Homepage": "https://github.com/klen/muffin-grpc",
        "Repository": "https://github.com/klen/muffin-grpc"
    },
    "split_keywords": [
        "grpc",
        "muffin",
        "asyncio",
        "asgi",
        "web"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "682caccc6ca8c0b4d48fba8c907e34fa061bc691a3f7a1ec49925a51f3cfcff8",
                "md5": "91da076c7b04b42bed414dbebcc0c8a3",
                "sha256": "b2df1427c59755443acde7754d08794f508cd02df8cec9e7203ea23274ccf0d8"
            },
            "downloads": -1,
            "filename": "muffin_grpc-0.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "91da076c7b04b42bed414dbebcc0c8a3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 10098,
            "upload_time": "2023-05-18T05:44:35",
            "upload_time_iso_8601": "2023-05-18T05:44:35.623638Z",
            "url": "https://files.pythonhosted.org/packages/68/2c/accc6ca8c0b4d48fba8c907e34fa061bc691a3f7a1ec49925a51f3cfcff8/muffin_grpc-0.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ef7ea9c07ac38e36d2c66ee14db606f8e4562ba1a63f137daba2d5129a423b8d",
                "md5": "de79519416735c8e4dd9478bdccf8675",
                "sha256": "be44d1c3b2ee7f6dc59666f414f6ef63b1c2ff1ea114597b2d4af21972ed5fea"
            },
            "downloads": -1,
            "filename": "muffin_grpc-0.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "de79519416735c8e4dd9478bdccf8675",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 11185,
            "upload_time": "2023-05-18T05:44:37",
            "upload_time_iso_8601": "2023-05-18T05:44:37.461281Z",
            "url": "https://files.pythonhosted.org/packages/ef/7e/a9c07ac38e36d2c66ee14db606f8e4562ba1a63f137daba2d5129a423b8d/muffin_grpc-0.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-18 05:44:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "klen",
    "github_project": "muffin-grpc",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "muffin-grpc"
}
        
Elapsed time: 0.09404s