Skip to content

Commit bdbd275

Browse files
committed
chore: grpc client implemented
1 parent 3f8fce0 commit bdbd275

17 files changed

Lines changed: 449 additions & 10 deletions

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
PROTO_DIR := proto
2+
OUT_DIR := app/grpc_client_impl/proto
3+
PROTOC_CMD := python -m grpc_tools.protoc
4+
5+
.PHONY: all clean
6+
7+
all:
8+
$(PROTOC_CMD) -I$(PROTO_DIR) --proto_path=$(PROTO_DIR) \
9+
--python_out=$(OUT_DIR) --pyi_out=$(OUT_DIR) --grpc_python_out=$(OUT_DIR) \
10+
$(PROTO_DIR)/*.proto
11+
12+
clean:
13+
rm -rf $(OUT_DIR)/*.py $(OUT_DIR)/*.pyi

app/grpc_client_impl/__init__.py

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import grpc
2+
3+
import app.grpc_client_impl.proto.get_cpu_system_usage_pb2 as grpc_system_proto_contracts
4+
import app.grpc_client_impl.proto.get_cpu_system_usage_pb2_grpc as grpc_system_proto_client
5+
import app.grpc_client_impl.proto.get_cpu_user_usage_pb2 as grpc_user_proto_contracts
6+
import app.grpc_client_impl.proto.get_cpu_user_usage_pb2_grpc as grpc_user_proto_client
7+
8+
9+
class GRPCClient:
10+
def __init__(self, channel: grpc.Channel):
11+
self.channel = channel
12+
self.system_stub = grpc_system_proto_client.GetCpuSystemUsageServiceStub(channel)
13+
self.user_stub = grpc_user_proto_client.GetCpuUserUsageServiceStub(channel)
14+
15+
def get_cpu_system_usage(self, date_from, date_to) -> grpc_system_proto_contracts.GetCpuSystemUsageResponse:
16+
request = grpc_system_proto_contracts.GetCpuSystemUsageRequest(date_from=date_from, date_to=date_to)
17+
return self.system_stub.GetCpuSystemUsage(request)
18+
19+
def get_cpu_user_usage(self, date_from, date_to) -> grpc_user_proto_contracts.GetCpuUserUsageResponse:
20+
request = grpc_user_proto_contracts.GetCpuUserUsageRequest(date_from=date_from, date_to=date_to)
21+
return self.user_stub.GetCpuUserUsage(request)
22+
23+
class GRPCClientSingleton:
24+
_instance = None
25+
26+
@classmethod
27+
def get_instance(cls):
28+
if cls._instance is None:
29+
channel = grpc.insecure_channel("localhost:50051")
30+
cls._instance = GRPCClient(channel)
31+
return cls._instance

app/grpc_client_impl/proto/__init__.py

Whitespace-only changes.

app/grpc_client_impl/proto/get_cpu_system_usage_pb2.py

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from google.protobuf.internal import containers as _containers
2+
from google.protobuf import descriptor as _descriptor
3+
from google.protobuf import message as _message
4+
from typing import ClassVar as _ClassVar, Iterable as _Iterable, Mapping as _Mapping, Optional as _Optional, Union as _Union
5+
6+
DESCRIPTOR: _descriptor.FileDescriptor
7+
8+
class GetCpuSystemUsageRequest(_message.Message):
9+
__slots__ = ("date_from", "date_to")
10+
DATE_FROM_FIELD_NUMBER: _ClassVar[int]
11+
DATE_TO_FIELD_NUMBER: _ClassVar[int]
12+
date_from: int
13+
date_to: int
14+
def __init__(self, date_from: _Optional[int] = ..., date_to: _Optional[int] = ...) -> None: ...
15+
16+
class CpuUsage(_message.Message):
17+
__slots__ = ("cpu", "avg_usage", "max_usage", "min_usage")
18+
CPU_FIELD_NUMBER: _ClassVar[int]
19+
AVG_USAGE_FIELD_NUMBER: _ClassVar[int]
20+
MAX_USAGE_FIELD_NUMBER: _ClassVar[int]
21+
MIN_USAGE_FIELD_NUMBER: _ClassVar[int]
22+
cpu: str
23+
avg_usage: float
24+
max_usage: float
25+
min_usage: float
26+
def __init__(self, cpu: _Optional[str] = ..., avg_usage: _Optional[float] = ..., max_usage: _Optional[float] = ..., min_usage: _Optional[float] = ...) -> None: ...
27+
28+
class GetCpuSystemUsageResponse(_message.Message):
29+
__slots__ = ("usages",)
30+
USAGES_FIELD_NUMBER: _ClassVar[int]
31+
usages: _containers.RepeatedCompositeFieldContainer[CpuUsage]
32+
def __init__(self, usages: _Optional[_Iterable[_Union[CpuUsage, _Mapping]]] = ...) -> None: ...
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
2+
"""Client and server classes corresponding to protobuf-defined services."""
3+
import grpc
4+
import warnings
5+
6+
import app.grpc_client_impl.proto.get_cpu_system_usage_pb2 as get__cpu__system__usage__pb2
7+
8+
GRPC_GENERATED_VERSION = '1.71.0'
9+
GRPC_VERSION = grpc.__version__
10+
_version_not_supported = False
11+
12+
try:
13+
from grpc._utilities import first_version_is_lower
14+
_version_not_supported = first_version_is_lower(GRPC_VERSION, GRPC_GENERATED_VERSION)
15+
except ImportError:
16+
_version_not_supported = True
17+
18+
if _version_not_supported:
19+
raise RuntimeError(
20+
f'The grpc package installed is at version {GRPC_VERSION},'
21+
+ f' but the generated code in get_cpu_system_usage_pb2_grpc.py depends on'
22+
+ f' grpcio>={GRPC_GENERATED_VERSION}.'
23+
+ f' Please upgrade your grpc module to grpcio>={GRPC_GENERATED_VERSION}'
24+
+ f' or downgrade your generated code using grpcio-tools<={GRPC_VERSION}.'
25+
)
26+
27+
28+
class GetCpuSystemUsageServiceStub(object):
29+
"""Missing associated documentation comment in .proto file."""
30+
31+
def __init__(self, channel):
32+
"""Constructor.
33+
34+
Args:
35+
channel: A grpc.Channel.
36+
"""
37+
self.GetCpuSystemUsage = channel.unary_unary(
38+
'/cpu_system_usage.GetCpuSystemUsageService/GetCpuSystemUsage',
39+
request_serializer=get__cpu__system__usage__pb2.GetCpuSystemUsageRequest.SerializeToString,
40+
response_deserializer=get__cpu__system__usage__pb2.GetCpuSystemUsageResponse.FromString,
41+
_registered_method=True)
42+
43+
44+
class GetCpuSystemUsageServiceServicer(object):
45+
"""Missing associated documentation comment in .proto file."""
46+
47+
def GetCpuSystemUsage(self, request, context):
48+
"""Missing associated documentation comment in .proto file."""
49+
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
50+
context.set_details('Method not implemented!')
51+
raise NotImplementedError('Method not implemented!')
52+
53+
54+
def add_GetCpuSystemUsageServiceServicer_to_server(servicer, server):
55+
rpc_method_handlers = {
56+
'GetCpuSystemUsage': grpc.unary_unary_rpc_method_handler(
57+
servicer.GetCpuSystemUsage,
58+
request_deserializer=get__cpu__system__usage__pb2.GetCpuSystemUsageRequest.FromString,
59+
response_serializer=get__cpu__system__usage__pb2.GetCpuSystemUsageResponse.SerializeToString,
60+
),
61+
}
62+
generic_handler = grpc.method_handlers_generic_handler(
63+
'cpu_system_usage.GetCpuSystemUsageService', rpc_method_handlers)
64+
server.add_generic_rpc_handlers((generic_handler,))
65+
server.add_registered_method_handlers('cpu_system_usage.GetCpuSystemUsageService', rpc_method_handlers)
66+
67+
68+
# This class is part of an EXPERIMENTAL API.
69+
class GetCpuSystemUsageService(object):
70+
"""Missing associated documentation comment in .proto file."""
71+
72+
@staticmethod
73+
def GetCpuSystemUsage(request,
74+
target,
75+
options=(),
76+
channel_credentials=None,
77+
call_credentials=None,
78+
insecure=False,
79+
compression=None,
80+
wait_for_ready=None,
81+
timeout=None,
82+
metadata=None):
83+
return grpc.experimental.unary_unary(
84+
request,
85+
target,
86+
'/cpu_system_usage.GetCpuSystemUsageService/GetCpuSystemUsage',
87+
get__cpu__system__usage__pb2.GetCpuSystemUsageRequest.SerializeToString,
88+
get__cpu__system__usage__pb2.GetCpuSystemUsageResponse.FromString,
89+
options,
90+
channel_credentials,
91+
insecure,
92+
call_credentials,
93+
compression,
94+
wait_for_ready,
95+
timeout,
96+
metadata,
97+
_registered_method=True)

app/grpc_client_impl/proto/get_cpu_user_usage_pb2.py

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from google.protobuf.internal import containers as _containers
2+
from google.protobuf import descriptor as _descriptor
3+
from google.protobuf import message as _message
4+
from typing import ClassVar as _ClassVar, Iterable as _Iterable, Mapping as _Mapping, Optional as _Optional, Union as _Union
5+
6+
DESCRIPTOR: _descriptor.FileDescriptor
7+
8+
class GetCpuUserUsageRequest(_message.Message):
9+
__slots__ = ("date_from", "date_to")
10+
DATE_FROM_FIELD_NUMBER: _ClassVar[int]
11+
DATE_TO_FIELD_NUMBER: _ClassVar[int]
12+
date_from: int
13+
date_to: int
14+
def __init__(self, date_from: _Optional[int] = ..., date_to: _Optional[int] = ...) -> None: ...
15+
16+
class CpuUsage(_message.Message):
17+
__slots__ = ("cpu", "avg_usage", "max_usage", "min_usage")
18+
CPU_FIELD_NUMBER: _ClassVar[int]
19+
AVG_USAGE_FIELD_NUMBER: _ClassVar[int]
20+
MAX_USAGE_FIELD_NUMBER: _ClassVar[int]
21+
MIN_USAGE_FIELD_NUMBER: _ClassVar[int]
22+
cpu: str
23+
avg_usage: float
24+
max_usage: float
25+
min_usage: float
26+
def __init__(self, cpu: _Optional[str] = ..., avg_usage: _Optional[float] = ..., max_usage: _Optional[float] = ..., min_usage: _Optional[float] = ...) -> None: ...
27+
28+
class GetCpuUserUsageResponse(_message.Message):
29+
__slots__ = ("usages",)
30+
USAGES_FIELD_NUMBER: _ClassVar[int]
31+
usages: _containers.RepeatedCompositeFieldContainer[CpuUsage]
32+
def __init__(self, usages: _Optional[_Iterable[_Union[CpuUsage, _Mapping]]] = ...) -> None: ...
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
2+
"""Client and server classes corresponding to protobuf-defined services."""
3+
import grpc
4+
import warnings
5+
6+
import app.grpc_client_impl.proto.get_cpu_user_usage_pb2 as get__cpu__user__usage__pb2
7+
8+
GRPC_GENERATED_VERSION = '1.71.0'
9+
GRPC_VERSION = grpc.__version__
10+
_version_not_supported = False
11+
12+
try:
13+
from grpc._utilities import first_version_is_lower
14+
_version_not_supported = first_version_is_lower(GRPC_VERSION, GRPC_GENERATED_VERSION)
15+
except ImportError:
16+
_version_not_supported = True
17+
18+
if _version_not_supported:
19+
raise RuntimeError(
20+
f'The grpc package installed is at version {GRPC_VERSION},'
21+
+ f' but the generated code in get_cpu_user_usage_pb2_grpc.py depends on'
22+
+ f' grpcio>={GRPC_GENERATED_VERSION}.'
23+
+ f' Please upgrade your grpc module to grpcio>={GRPC_GENERATED_VERSION}'
24+
+ f' or downgrade your generated code using grpcio-tools<={GRPC_VERSION}.'
25+
)
26+
27+
28+
class GetCpuUserUsageServiceStub(object):
29+
"""Missing associated documentation comment in .proto file."""
30+
31+
def __init__(self, channel):
32+
"""Constructor.
33+
34+
Args:
35+
channel: A grpc.Channel.
36+
"""
37+
self.GetCpuUserUsage = channel.unary_unary(
38+
'/cpu_user_usage.GetCpuUserUsageService/GetCpuUserUsage',
39+
request_serializer=get__cpu__user__usage__pb2.GetCpuUserUsageRequest.SerializeToString,
40+
response_deserializer=get__cpu__user__usage__pb2.GetCpuUserUsageResponse.FromString,
41+
_registered_method=True)
42+
43+
44+
class GetCpuUserUsageServiceServicer(object):
45+
"""Missing associated documentation comment in .proto file."""
46+
47+
def GetCpuUserUsage(self, request, context):
48+
"""Missing associated documentation comment in .proto file."""
49+
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
50+
context.set_details('Method not implemented!')
51+
raise NotImplementedError('Method not implemented!')
52+
53+
54+
def add_GetCpuUserUsageServiceServicer_to_server(servicer, server):
55+
rpc_method_handlers = {
56+
'GetCpuUserUsage': grpc.unary_unary_rpc_method_handler(
57+
servicer.GetCpuUserUsage,
58+
request_deserializer=get__cpu__user__usage__pb2.GetCpuUserUsageRequest.FromString,
59+
response_serializer=get__cpu__user__usage__pb2.GetCpuUserUsageResponse.SerializeToString,
60+
),
61+
}
62+
generic_handler = grpc.method_handlers_generic_handler(
63+
'cpu_user_usage.GetCpuUserUsageService', rpc_method_handlers)
64+
server.add_generic_rpc_handlers((generic_handler,))
65+
server.add_registered_method_handlers('cpu_user_usage.GetCpuUserUsageService', rpc_method_handlers)
66+
67+
68+
# This class is part of an EXPERIMENTAL API.
69+
class GetCpuUserUsageService(object):
70+
"""Missing associated documentation comment in .proto file."""
71+
72+
@staticmethod
73+
def GetCpuUserUsage(request,
74+
target,
75+
options=(),
76+
channel_credentials=None,
77+
call_credentials=None,
78+
insecure=False,
79+
compression=None,
80+
wait_for_ready=None,
81+
timeout=None,
82+
metadata=None):
83+
return grpc.experimental.unary_unary(
84+
request,
85+
target,
86+
'/cpu_user_usage.GetCpuUserUsageService/GetCpuUserUsage',
87+
get__cpu__user__usage__pb2.GetCpuUserUsageRequest.SerializeToString,
88+
get__cpu__user__usage__pb2.GetCpuUserUsageResponse.FromString,
89+
options,
90+
channel_credentials,
91+
insecure,
92+
call_credentials,
93+
compression,
94+
wait_for_ready,
95+
timeout,
96+
metadata,
97+
_registered_method=True)

0 commit comments

Comments
 (0)