npk-student-grpc 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Котёнок Ильназик
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,31 @@
1
+ Metadata-Version: 2.4
2
+ Name: npk-student-grpc
3
+ Version: 0.1.0
4
+ Summary: Shared gRPC client infrastructure for NPK Student App
5
+ License-Expression: MIT
6
+ Requires-Python: >=3.10
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: grpcio<2,>=1.85.4
10
+ Requires-Dist: npk-student-contracts<0.2.0,>=0.1.0
11
+ Dynamic: license-file
12
+
13
+ # NPK Student gRPC
14
+
15
+ ## Русский
16
+
17
+ Клиентская gRPC-инфраструктура NPK Student App для Rust и Python под лицензией MIT.
18
+
19
+ ```sh
20
+ cargo add npk-student-grpc
21
+ python -m pip install npk-student-grpc
22
+ ```
23
+
24
+ ## English
25
+
26
+ gRPC client infrastructure for NPK Student App in Rust and Python. MIT license.
27
+
28
+ ```sh
29
+ cargo add npk-student-grpc
30
+ python -m pip install npk-student-grpc
31
+ ```
@@ -0,0 +1,19 @@
1
+ # NPK Student gRPC
2
+
3
+ ## Русский
4
+
5
+ Клиентская gRPC-инфраструктура NPK Student App для Rust и Python под лицензией MIT.
6
+
7
+ ```sh
8
+ cargo add npk-student-grpc
9
+ python -m pip install npk-student-grpc
10
+ ```
11
+
12
+ ## English
13
+
14
+ gRPC client infrastructure for NPK Student App in Rust and Python. MIT license.
15
+
16
+ ```sh
17
+ cargo add npk-student-grpc
18
+ python -m pip install npk-student-grpc
19
+ ```
@@ -0,0 +1,24 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "npk-student-grpc"
7
+ version = "0.1.0"
8
+ description = "Shared gRPC client infrastructure for NPK Student App"
9
+
10
+ readme = "README.md"
11
+
12
+ license = "MIT"
13
+ license-files = ["LICENSE"]
14
+
15
+ requires-python = ">=3.10"
16
+ dependencies = ["grpcio>=1.85.4,<2", "npk-student-contracts>=0.1.0,<0.2.0"]
17
+
18
+ [tool.setuptools.packages.find]
19
+ where = ["python"]
20
+
21
+ [tool.pyright]
22
+ venvPath = "."
23
+ venv = ".venv"
24
+ extraPaths = ["../contracts/generated/python", "python"]
@@ -0,0 +1,4 @@
1
+ from .client import ClientConfig, GrpcClientFactory
2
+ from .registry import SERVICES, Service, find_service
3
+
4
+ __all__ = ["SERVICES", "ClientConfig", "GrpcClientFactory", "Service", "find_service"]
@@ -0,0 +1,94 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ from collections.abc import Callable
5
+ from dataclasses import dataclass
6
+ from typing import Protocol, TypeVar
7
+ from urllib.parse import urlsplit
8
+
9
+ import grpc
10
+
11
+ from .registry import find_service
12
+
13
+ ClientT = TypeVar("ClientT")
14
+ RequestT = TypeVar("RequestT")
15
+ ResponseT = TypeVar("ResponseT")
16
+
17
+
18
+ @dataclass(frozen=True)
19
+ class ClientConfig:
20
+ request_timeout: float = 10.0
21
+ max_message_size: int = 4 * 1024 * 1024
22
+
23
+
24
+ class _ClosableChannel(Protocol):
25
+ async def close(self, grace: float | None = None) -> None: ...
26
+
27
+
28
+ async def _close_channel(channel: _ClosableChannel) -> None:
29
+ await channel.close()
30
+
31
+
32
+ class GrpcClientFactory:
33
+ def __init__(self, options: ClientConfig | None = None) -> None:
34
+ self._options: ClientConfig = options if options is not None else ClientConfig()
35
+ self._channels: dict[str, grpc.aio.Channel] = {}
36
+
37
+ def register(self, token: str, url: str) -> None:
38
+ _ = find_service(token)
39
+ if token in self._channels:
40
+ raise ValueError(f"gRPC service is already registered: {token}")
41
+
42
+ endpoint = urlsplit(url)
43
+ if (
44
+ endpoint.scheme not in ("http", "https")
45
+ or not endpoint.hostname
46
+ or endpoint.username is not None
47
+ or endpoint.path not in ("", "/")
48
+ or endpoint.query
49
+ or endpoint.fragment
50
+ ):
51
+ raise ValueError("expected an http(s) gRPC endpoint")
52
+
53
+ port = endpoint.port or (443 if endpoint.scheme == "https" else 80)
54
+ host = endpoint.hostname
55
+
56
+ target = f"[{host}]:{port}" if ":" in host else f"{host}:{port}"
57
+ size = self._options.max_message_size
58
+ options = (
59
+ ("grpc.max_receive_message_length", size),
60
+ ("grpc.max_send_message_length", size),
61
+ )
62
+
63
+ if endpoint.scheme == "https":
64
+ channel = grpc.aio.secure_channel(
65
+ target, grpc.ssl_channel_credentials(), options
66
+ )
67
+ else:
68
+ channel = grpc.aio.insecure_channel(target, options)
69
+ self._channels[token] = channel
70
+
71
+ def register_from_env(self, token: str) -> None:
72
+ self.register(token, os.environ[find_service(token).env])
73
+
74
+ def channel(self, token: str) -> grpc.aio.Channel:
75
+ return self._channels[token]
76
+
77
+ def client(
78
+ self,
79
+ token: str,
80
+ create: Callable[[grpc.aio.Channel], ClientT],
81
+ ) -> ClientT:
82
+ return create(self.channel(token))
83
+
84
+ async def call(
85
+ self,
86
+ rpc: grpc.aio.UnaryUnaryMultiCallable[RequestT, ResponseT],
87
+ request: RequestT,
88
+ ) -> ResponseT:
89
+ return await rpc(request, timeout=self._options.request_timeout)
90
+
91
+ async def close(self) -> None:
92
+ while self._channels:
93
+ _, channel = self._channels.popitem()
94
+ await _close_channel(channel)
@@ -0,0 +1,21 @@
1
+ from dataclasses import dataclass
2
+
3
+ from .registry_generated import ENTRIES
4
+
5
+
6
+ @dataclass(frozen=True)
7
+ class Service:
8
+ token: str
9
+ name: str
10
+ proto_path: str
11
+ env: str
12
+
13
+
14
+ SERVICES = tuple(Service(*entry) for entry in ENTRIES)
15
+
16
+
17
+ def find_service(token: str) -> Service:
18
+ for service in SERVICES:
19
+ if service.token == token:
20
+ return service
21
+ raise KeyError(token)
@@ -0,0 +1,9 @@
1
+ # Generated from NPK Student contracts. Do not edit.
2
+ ENTRIES = (
3
+ (
4
+ "DEMO_PACKAGE",
5
+ "npk_student.demo.v1.DemoService",
6
+ "npk_student_contracts/demo/v1/demo.proto",
7
+ "DEMO_GRPC_URL",
8
+ ),
9
+ )
@@ -0,0 +1,31 @@
1
+ Metadata-Version: 2.4
2
+ Name: npk-student-grpc
3
+ Version: 0.1.0
4
+ Summary: Shared gRPC client infrastructure for NPK Student App
5
+ License-Expression: MIT
6
+ Requires-Python: >=3.10
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: grpcio<2,>=1.85.4
10
+ Requires-Dist: npk-student-contracts<0.2.0,>=0.1.0
11
+ Dynamic: license-file
12
+
13
+ # NPK Student gRPC
14
+
15
+ ## Русский
16
+
17
+ Клиентская gRPC-инфраструктура NPK Student App для Rust и Python под лицензией MIT.
18
+
19
+ ```sh
20
+ cargo add npk-student-grpc
21
+ python -m pip install npk-student-grpc
22
+ ```
23
+
24
+ ## English
25
+
26
+ gRPC client infrastructure for NPK Student App in Rust and Python. MIT license.
27
+
28
+ ```sh
29
+ cargo add npk-student-grpc
30
+ python -m pip install npk-student-grpc
31
+ ```
@@ -0,0 +1,13 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ python/npk_student_grpc/__init__.py
5
+ python/npk_student_grpc/client.py
6
+ python/npk_student_grpc/py.typed
7
+ python/npk_student_grpc/registry.py
8
+ python/npk_student_grpc/registry_generated.py
9
+ python/npk_student_grpc.egg-info/PKG-INFO
10
+ python/npk_student_grpc.egg-info/SOURCES.txt
11
+ python/npk_student_grpc.egg-info/dependency_links.txt
12
+ python/npk_student_grpc.egg-info/requires.txt
13
+ python/npk_student_grpc.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ grpcio<2,>=1.85.4
2
+ npk-student-contracts<0.2.0,>=0.1.0
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+