Skip to content

Commit 2348a1d

Browse files
authored
feat: add pull_kvs method (#10)
* feat: add pull_kvs method * feat: add pull_kvs method
1 parent 72b17a0 commit 2348a1d

File tree

7 files changed

+103
-5
lines changed

7 files changed

+103
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,5 @@ cython_debug/
160160
#.idea/
161161

162162
.vscode
163-
.ruff_cache
163+
.ruff_cache
164+
examples/test*

bk_bscp/client.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# We undertake not to change the open source license (MIT license) applicable
1515
# to the current version of the project delivered to anyone in the future.
1616
"""The client for bscp feed-server"""
17+
1718
import json
1819
import logging
1920
import random
@@ -36,7 +37,7 @@
3637
)
3738
from bk_bscp.grpc_lib.core.base import base_pb2
3839
from bk_bscp.grpc_lib.feed_server import feed_server_pb2, feed_server_pb2_grpc
39-
from bk_bscp.models import KeyValuePair, KeyValueUpdatedEvent, WatchAppInputParams
40+
from bk_bscp.models import AppOptions, KeyValuePair, KeyValueUpdatedEvent, Release, WatchAppInputParams
4041
from bk_bscp.utils import get_fingerprint
4142

4243
logger = logging.getLogger("bk_bscp")
@@ -153,6 +154,42 @@ def do_handshake(self):
153154
raise BscpClientHandshakeError("Unable to handshake", e) from e
154155
logger.debug("Handshake success, api_version: %s", response.api_version)
155156

157+
def pull_kvs(self, app: str, match: List[str], app_options: Optional[AppOptions] = None) -> Release:
158+
"""Pull key-value Release from the server.
159+
:param app: The app name.
160+
:param match: The key.
161+
:param app_options: the app option params.
162+
:return: A kv Release.
163+
:raises BscpClientGetError: If get failed.
164+
"""
165+
total_labels = {**self.labels}
166+
uid = self._fingerprint
167+
168+
if app_options is not None:
169+
uid = app_options.uid or self._fingerprint
170+
total_labels = {**self.labels, **(app_options.labels or {})}
171+
match = [*match, *(app_options.match or [])]
172+
173+
msg = feed_server_pb2.PullKvMetaReq(
174+
match=match,
175+
biz_id=self.biz_id,
176+
app_meta=feed_server_pb2.AppMeta(
177+
uid=uid,
178+
app=app,
179+
labels=total_labels,
180+
),
181+
)
182+
try:
183+
response = self.stub.PullKvMeta(msg, metadata=self._get_req_metadata())
184+
except grpc.RpcError as e:
185+
raise BscpClientGetError(f'Unable to get "{app}" release, details: {e.details()}', e) from e
186+
187+
r = Release(
188+
release_id=response.release_id,
189+
kvs=response.kv_metas,
190+
)
191+
return r
192+
156193
def get(self, app: str, key: str, labels: Optional[dict] = None) -> KeyValuePair:
157194
"""Get a key-value pair from the server.
158195

bk_bscp/models.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from dataclasses import dataclass
1717
from typing import Any, Dict, List
1818

19+
from bk_bscp.grpc_lib.feed_server import feed_server_pb2
1920
from bk_bscp.utils import dict_to_dataclass
2021

2122

@@ -73,3 +74,20 @@ class KeyValuePair:
7374
key: str
7475
type: str
7576
value: Any
77+
78+
79+
@dataclass
80+
class AppOptions:
81+
"""AppOptions options for app pull and watch"""
82+
83+
match: List[str] # Match matches config items
84+
labels: Dict[str, str] # Labels instance labels
85+
uid: str # UID instance unique uid
86+
87+
88+
@dataclass
89+
class Release:
90+
"""Release info returned by the bscp server."""
91+
92+
release_id: int
93+
kvs: List[feed_server_pb2.KvMeta]

examples/get_key.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
# We undertake not to change the open source license (MIT license) applicable
1515
# to the current version of the project delivered to anyone in the future.
1616
"""Get a key."""
17-
from bk_bscp.client import BscpClient
17+
18+
from bk_bscp.client import AppOptions, BscpClient
1819

1920
SERVER_ADDRS = ["example.com:9090"]
2021
TOKEN = "your_token"
@@ -27,6 +28,20 @@ def get_key():
2728
print(pair)
2829

2930

31+
def get_all_keys():
32+
app = AppOptions(
33+
match=[],
34+
labels={},
35+
uid="",
36+
)
37+
38+
with BscpClient(SERVER_ADDRS, TOKEN, BIZ_ID) as client:
39+
release = client.pull_kvs("app1", ["key1"], app)
40+
for kv in release.kvs:
41+
pair = client.get("app1", kv.key)
42+
print(pair)
43+
44+
3045
def get_key_with_labels():
3146
"""Get a key with labels specified."""
3247
#
@@ -42,7 +57,7 @@ def get_key_with_labels():
4257

4358

4459
def main():
45-
get_key()
60+
get_all_keys()
4661

4762

4863
if __name__ == "__main__":

readme.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ with BscpClient(server_addrs, token, biz_id) as client:
5858

5959
### 开发指南
6060

61+
安装环境
62+
63+
```bash
64+
# 安装版本, 修改{version}为版本号, 如3.18, 如果已经有版本,步骤忽略
65+
uv python install {version}
66+
67+
# 创建环境, 修改{path}为python真实路径
68+
uv venv -p {path}/python
69+
70+
# 安装依赖
71+
uv sync --frozen
72+
```
73+
6174
执行单元测试:
6275

6376
```bash

readme_en.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ For more sample code, please refer to the examples/ directory.
5858

5959
### Development Guide
6060

61+
Installation environment
62+
63+
```bash
64+
# Install version, modify {version} to real version, such as 3.18. If there is already a version, ignore this step
65+
uv python install {version}
66+
67+
# Create environment, modify {path} to the real path of python
68+
69+
uv venv -p {path}/python
70+
71+
# Install dependencies
72+
uv sync --frozen
73+
```
74+
6175
Execute unit tests:
6276

6377
```bash

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)