-
Notifications
You must be signed in to change notification settings - Fork 8
Dev/clientlist #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Dev/clientlist #47
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ae12627
feat: update tweet get
6d8418f
feat: update tweet get
6adc2d6
feat: add tweet hub client
9d04d15
feat: add tweet from queue
08e7b34
feat: format kol
c8b06ab
feat: update consume & flash
69d730a
feat: update consume & flash
d09de68
feat: update consume & flash
4a7835b
feat: update consume & flash
096ad10
feat: update consume & flash
4943650
feat: update consume & flash
a6d593d
feat: update consume & flash
9e44c5c
feat: update consume & flash
45b28f9
feat: update consume & flash
c6486b5
feat: update consume & flash
c163167
feat: mypy
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/sunagent-ext/src/sunagent_ext/tweet/tweet_hub_client.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # kol_sdk.py | ||
| import json | ||
| import logging | ||
| from datetime import datetime | ||
| from typing import Any, Dict, List | ||
|
|
||
| import requests | ||
| from requests.adapters import HTTPAdapter | ||
| from urllib3.util.retry import Retry | ||
|
|
||
| logger = logging.getLogger("sunagent-ext") | ||
|
|
||
| DEFAULT_TIMEOUT = 10 # 秒 | ||
|
|
||
|
|
||
| class TweetHubClient: | ||
| """ | ||
| 轻量级 requests 封装,支持: | ||
| 1. 创建 KOL 列表 | ||
| 2. 删除 KOL 列表 | ||
| 3. 自动重试 + 超时 | ||
| 4. 中文友好(ensure_ascii=False) | ||
| """ | ||
|
|
||
| def __init__(self, base_url: str, agent_id: str = "hub", timeout: int = DEFAULT_TIMEOUT): | ||
| """ | ||
| :param base_url: 不含尾巴 / ,例:http://127.0.0.1:8084/api/sun | ||
| :param agent_id: 默认 agent_id | ||
| :param timeout: 单次请求超时 | ||
| """ | ||
| self.base_url = base_url.rstrip("/") | ||
| self.agent_id = agent_id | ||
| self.timeout = timeout | ||
| self._session = requests.Session() | ||
|
|
||
| # 重试策略:3 次、backoff、状态码 5xx/429 | ||
| retry = Retry( | ||
| total=3, | ||
| backoff_factor=0.3, | ||
| status_forcelist=[429, 500, 502, 503, 504], | ||
| allowed_methods={"POST", "DELETE"}, | ||
| ) | ||
| self._session.mount("http://", HTTPAdapter(max_retries=retry)) | ||
| self._session.mount("https://", HTTPAdapter(max_retries=retry)) | ||
|
|
||
| # ---------- 内部方法 ---------- | ||
| def _request(self, method: str, endpoint: str, payload: Dict[str, Any]) -> Dict[str, Any]: | ||
| url = f"{self.base_url}{endpoint}" | ||
| # 自动注入 agent_id | ||
| payload.setdefault("agent_id", self.agent_id) | ||
| try: | ||
| resp = self._session.request( | ||
| method=method, | ||
| url=url, | ||
| json=payload, # requests 会自动用 utf-8 编码 | ||
| timeout=self.timeout, | ||
| ) | ||
| resp.raise_for_status() | ||
| return resp.json() # type: ignore[no-any-return] | ||
| except requests.exceptions.RequestException as e: | ||
| logger.error(f"[KolClient] {method} {url} error: {e}") | ||
| raise | ||
|
|
||
| # ---------- 业务接口 ---------- | ||
| def create_kol(self, kol_list: List[str], agent_id: str | None = None) -> Dict[str, Any]: | ||
| """ | ||
| 批量创建/绑定 KOL | ||
| :param kol_list: twitter_id 列表 | ||
| :param agent_id: 可选,不传使用实例默认值 | ||
| """ | ||
| payload = {"kol_list": kol_list, "agent_id": agent_id or self.agent_id} | ||
| return self._request("POST", "/kol", payload) | ||
|
|
||
| def delete_kol(self, kol_list: List[str], agent_id: str | None = None) -> Dict[str, Any]: | ||
| """ | ||
| 批量删除/解绑 KOL | ||
| """ | ||
| payload = {"kol_list": kol_list, "agent_id": agent_id or self.agent_id} | ||
| return self._request("DELETE", "/kol", payload) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.