Skip to content

Latest commit

 

History

History
118 lines (82 loc) · 3.85 KB

README.md

File metadata and controls

118 lines (82 loc) · 3.85 KB

gRPC-python-boilerplate

Overview

This repository provides a boilerplate setup for a gRPC server implemented in Python. The goal is to offer a structured and easy-to-use template to quickly get started with gRPC development.

Features

  • gRPC server implementation in Python
  • Example .proto file defining service and messages
  • Basic server and client implementation
  • Instructions for generating gRPC code from .proto files
  • Docker support for containerized deployment

Prerequisites

  • Python 3.7+
  • pip (Python package installer)
  • gRPC tools (grpcio, grpcio-tools)
  • Docker (optional, for containerization)

Getting Started

  1. Clone the Repository
git clone https://github.com/yourusername/gRPC-python-boilerplate.git
cd gRPC-python-boilerplate
  1. Install Dependencies

    python -m venv venv
    source venv/bin/activate  # On Windows, use venv\Scripts\activate
    pip install -r requirements.txt

Genarate product_pb2.py and product_pb2_grpc.py :

python3 -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. protos/product.proto

product_pb2.py:

This file contains the classes for the message types defined in .proto file. It includes:

  • Classes for Messages : For each message type in .proto file, there will be a corresponding class in product_pb2.py. These classes are used to serialize and deserialize the message types to and from binary format.
  • Serialization Methods : Methods to serialize messages to binary format and parse messages from binary format.
  • Descriptors : Metadata about the message types, including fields and their types.

For example, based on product.proto, product_pb2.py will include classes like product, productId, etc.

product_pb2_grpc.py:

This file contains the classes and methods for the gRPC service defined in .proto file. It includes:

  • Stub Classes : These are client-side classes that contain methods corresponding to the RPC methods defined in the service. You use these stubs to call the RPC methods from the client code.
  • Servicer Classes : These are server-side classes that you subclass to implement the RPC methods. You define the logic of your RPC methods in these subclasses.
  • Server Registration Functions : Functions to add your service implementations to a gRPC server.

For example, based on product.proto, product_pb2_grpc.py will include:

  • productServiceStub class: For the client to call RPC methods.
  • productServiceServicer class: For the server to implement RPC methods.
  • add_productServiceServicer_to_server function: To register the service implementation with a gRPC server.

usage :

URL : localhost:5001

create product :

{
"description":"t-shirt",
"price":1000,
"productName":"t-shirt nike",
"status":"active",
"tax":10
}

create cart item :

{
  "products":[
    {
      "productId":
      "ec630b78-19af-4969-8b39-12235e10e82b"
    }   
  ],
  "quantity":2
}

get cart item :

{
 "cartItemId":"50ba8e1a-c043-4c29-b87a-61b894b57480"
}