From 7961fd76de4a0c1d1ab940550f3efbfec71fdc1d Mon Sep 17 00:00:00 2001 From: xoliqberdiyev Date: Thu, 23 Apr 2026 17:36:43 +0500 Subject: [PATCH] add grpc client --- core/generated/auto_pb2.py | 45 +++++++++++++++ core/generated/auto_pb2_grpc.py | 97 +++++++++++++++++++++++++++++++++ core/services/grpc/auto.py | 48 ++++++++++++++++ requirements.txt | 4 ++ 4 files changed, 194 insertions(+) create mode 100644 core/generated/auto_pb2.py create mode 100644 core/generated/auto_pb2_grpc.py create mode 100644 core/services/grpc/auto.py diff --git a/core/generated/auto_pb2.py b/core/generated/auto_pb2.py new file mode 100644 index 0000000..0cd42f6 --- /dev/null +++ b/core/generated/auto_pb2.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +# Generated by the protocol buffer compiler. DO NOT EDIT! +# NO CHECKED-IN PROTOBUF GENCODE +# source: auto.proto +# Protobuf Python Version: 6.31.1 +"""Generated protocol buffer code.""" +from google.protobuf import descriptor as _descriptor +from google.protobuf import descriptor_pool as _descriptor_pool +from google.protobuf import runtime_version as _runtime_version +from google.protobuf import symbol_database as _symbol_database +from google.protobuf.internal import builder as _builder +_runtime_version.ValidateProtobufRuntimeVersion( + _runtime_version.Domain.PUBLIC, + 6, + 31, + 1, + '', + 'auto.proto' +) +# @@protoc_insertion_point(imports) + +_sym_db = _symbol_database.Default() + + +from google.protobuf import timestamp_pb2 as google_dot_protobuf_dot_timestamp__pb2 + + +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\nauto.proto\x12\x04\x61uto\x1a\x1fgoogle/protobuf/timestamp.proto\"\xba\x01\n\x12\x41utoAvgCostRequest\x12\r\n\x05\x62rand\x18\x01 \x01(\t\x12\x11\n\tcondition\x18\x02 \x01(\t\x12\r\n\x05model\x18\x03 \x01(\t\x12\x14\n\x0c\x63omplication\x18\x04 \x01(\t\x12\x34\n\x10manufacture_date\x18\x05 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12\x18\n\x10\x64istance_covered\x18\x06 \x01(\t\x12\r\n\x05\x63olor\x18\x07 \x01(\t\"$\n\x13\x41\x64ImageListResponse\x12\r\n\x05image\x18\x01 \x01(\t\"t\n\x0e\x41\x64ListResponse\x12\n\n\x02id\x18\x01 \x01(\t\x12)\n\x06images\x18\x02 \x03(\x0b\x32\x19.auto.AdImageListResponse\x12\r\n\x05title\x18\x03 \x01(\t\x12\r\n\x05price\x18\x04 \x01(\t\x12\r\n\x05model\x18\x05 \x01(\t\"J\n\x13\x41utoAvgCostResponse\x12\x10\n\x08\x61vg_cost\x18\x01 \x01(\x01\x12!\n\x03\x61\x64s\x18\x02 \x03(\x0b\x32\x14.auto.AdListResponse2X\n\x12\x41utoAvgCostService\x12\x42\n\x0b\x41utoAvgCost\x12\x18.auto.AutoAvgCostRequest\x1a\x19.auto.AutoAvgCostResponseb\x06proto3') + +_globals = globals() +_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) +_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'auto_pb2', _globals) +if not _descriptor._USE_C_DESCRIPTORS: + DESCRIPTOR._loaded_options = None + _globals['_AUTOAVGCOSTREQUEST']._serialized_start=54 + _globals['_AUTOAVGCOSTREQUEST']._serialized_end=240 + _globals['_ADIMAGELISTRESPONSE']._serialized_start=242 + _globals['_ADIMAGELISTRESPONSE']._serialized_end=278 + _globals['_ADLISTRESPONSE']._serialized_start=280 + _globals['_ADLISTRESPONSE']._serialized_end=396 + _globals['_AUTOAVGCOSTRESPONSE']._serialized_start=398 + _globals['_AUTOAVGCOSTRESPONSE']._serialized_end=472 + _globals['_AUTOAVGCOSTSERVICE']._serialized_start=474 + _globals['_AUTOAVGCOSTSERVICE']._serialized_end=562 +# @@protoc_insertion_point(module_scope) diff --git a/core/generated/auto_pb2_grpc.py b/core/generated/auto_pb2_grpc.py new file mode 100644 index 0000000..9bf866b --- /dev/null +++ b/core/generated/auto_pb2_grpc.py @@ -0,0 +1,97 @@ +# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! +"""Client and server classes corresponding to protobuf-defined services.""" +import grpc +import warnings + +from core.generated import auto_pb2 as auto__pb2 + +GRPC_GENERATED_VERSION = '1.80.0' +GRPC_VERSION = grpc.__version__ +_version_not_supported = False + +try: + from grpc._utilities import first_version_is_lower + _version_not_supported = first_version_is_lower(GRPC_VERSION, GRPC_GENERATED_VERSION) +except ImportError: + _version_not_supported = True + +if _version_not_supported: + raise RuntimeError( + f'The grpc package installed is at version {GRPC_VERSION},' + + ' but the generated code in auto_pb2_grpc.py depends on' + + f' grpcio>={GRPC_GENERATED_VERSION}.' + + f' Please upgrade your grpc module to grpcio>={GRPC_GENERATED_VERSION}' + + f' or downgrade your generated code using grpcio-tools<={GRPC_VERSION}.' + ) + + +class AutoAvgCostServiceStub(object): + """Missing associated documentation comment in .proto file.""" + + def __init__(self, channel): + """Constructor. + + Args: + channel: A grpc.Channel. + """ + self.AutoAvgCost = channel.unary_unary( + '/auto.AutoAvgCostService/AutoAvgCost', + request_serializer=auto__pb2.AutoAvgCostRequest.SerializeToString, + response_deserializer=auto__pb2.AutoAvgCostResponse.FromString, + _registered_method=True) + + +class AutoAvgCostServiceServicer(object): + """Missing associated documentation comment in .proto file.""" + + def AutoAvgCost(self, request, context): + """Missing associated documentation comment in .proto file.""" + context.set_code(grpc.StatusCode.UNIMPLEMENTED) + context.set_details('Method not implemented!') + raise NotImplementedError('Method not implemented!') + + +def add_AutoAvgCostServiceServicer_to_server(servicer, server): + rpc_method_handlers = { + 'AutoAvgCost': grpc.unary_unary_rpc_method_handler( + servicer.AutoAvgCost, + request_deserializer=auto__pb2.AutoAvgCostRequest.FromString, + response_serializer=auto__pb2.AutoAvgCostResponse.SerializeToString, + ), + } + generic_handler = grpc.method_handlers_generic_handler( + 'auto.AutoAvgCostService', rpc_method_handlers) + server.add_generic_rpc_handlers((generic_handler,)) + server.add_registered_method_handlers('auto.AutoAvgCostService', rpc_method_handlers) + + + # This class is part of an EXPERIMENTAL API. +class AutoAvgCostService(object): + """Missing associated documentation comment in .proto file.""" + + @staticmethod + def AutoAvgCost(request, + target, + options=(), + channel_credentials=None, + call_credentials=None, + insecure=False, + compression=None, + wait_for_ready=None, + timeout=None, + metadata=None): + return grpc.experimental.unary_unary( + request, + target, + '/auto.AutoAvgCostService/AutoAvgCost', + auto__pb2.AutoAvgCostRequest.SerializeToString, + auto__pb2.AutoAvgCostResponse.FromString, + options, + channel_credentials, + insecure, + call_credentials, + compression, + wait_for_ready, + timeout, + metadata, + _registered_method=True) diff --git a/core/services/grpc/auto.py b/core/services/grpc/auto.py new file mode 100644 index 0000000..9ad5198 --- /dev/null +++ b/core/services/grpc/auto.py @@ -0,0 +1,48 @@ +import grpc +from google.protobuf.timestamp_pb2 import Timestamp +from datetime import datetime + +from config.env import env +from core.generated import auto_pb2, auto_pb2_grpc + + +def get_auto_avg_cost( + brand, + condition, + model, + complication, + manufacture_date: datetime, + distance_covered, + color +): + # url = f"{env.str('RPC_IP')}:{env.str('RPC_PORT')}" + # channel = grpc.insecure_channel(url) + channel = grpc.insecure_channel("192.168.1.120:50051") + stub = auto_pb2_grpc.AutoAvgCostServiceStub(channel) + + ts = Timestamp() + ts.FromDatetime(manufacture_date) + + response = stub.AutoAvgCost(auto_pb2.AutoAvgCostRequest( + brand=brand, + condition=condition, + model=model, + complication=complication, + manufacture_date=ts, + distance_covered=distance_covered, + color=color, + )) + + return { + "avg_cost": response.avg_cost, + "ads": [ + { + "id": ad.id, + "title": ad.title, + "price": ad.price, + "model": ad.model, + "images": [img.image for img in ad.images], + } + for ad in response.ads + ] + } diff --git a/requirements.txt b/requirements.txt index f43fadb..9c56fdb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -46,3 +46,7 @@ boto3 # !NOTE: on-websocket # websockets # channels-redis + +grpcio>=1.62.0 +grpcio-tools>=1.62.0 +protobuf>=4.25.0 \ No newline at end of file