Skip to content

Commit 95d537b

Browse files
author
William Yang
committed
fix: 适配 API 新的统一响应格式 { success: true, data: T }
Changes: - client.py: _handle_response() 自动解包 data 字段 - device_flow.py: 兼容新响应格式,使用 data.get('data', data) 解包 - 更新测试以使用新的 API 响应格式 Compatible with API changes in bootcs-api commit 0926481
1 parent 7ff81b1 commit 95d537b

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

bootcs/api/client.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _handle_response(self, response: requests.Response) -> Dict[str, Any]:
7272
response: The HTTP response.
7373
7474
Returns:
75-
Parsed JSON response data.
75+
Parsed JSON response data (unwrapped from { success, data } envelope).
7676
7777
Raises:
7878
APIError: If the response indicates an error.
@@ -95,6 +95,11 @@ def _handle_response(self, response: requests.Response) -> Dict[str, Any]:
9595
status_code=response.status_code
9696
)
9797

98+
# API returns { success: true, data: {...} } format
99+
# Unwrap and return just the data portion for convenience
100+
if "data" in data:
101+
return data["data"]
102+
98103
return data
99104

100105
def post(self, path: str, data: Dict[str, Any], timeout: int = 30) -> Dict[str, Any]:

bootcs/auth/device_flow.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,16 @@ def start_device_flow() -> DeviceCodeResponse:
7373
message=error.get("message", "Failed to start device flow")
7474
)
7575

76-
# API returns data directly (not wrapped in success/data)
76+
# API returns { success: true, data: {...} } format
77+
# Unwrap the data portion
78+
payload = data.get("data", data)
79+
7780
return DeviceCodeResponse(
78-
device_code=data["deviceCode"],
79-
user_code=data["userCode"],
80-
verification_uri=data["verificationUri"],
81-
expires_in=data["expiresIn"],
82-
interval=data.get("interval", 5),
81+
device_code=payload["deviceCode"],
82+
user_code=payload["userCode"],
83+
verification_uri=payload["verificationUri"],
84+
expires_in=payload["expiresIn"],
85+
interval=payload.get("interval", 5),
8386
)
8487
except requests.RequestException as e:
8588
raise DeviceFlowError(
@@ -153,12 +156,15 @@ def poll_for_token(
153156
message=error.get("message", "Authentication failed")
154157
)
155158

156-
if response.ok and "token" in data:
157-
# Success! API returns data directly
158-
return TokenResponse(
159-
token=data["token"],
160-
user=data.get("user", {})
161-
)
159+
if response.ok:
160+
# Success! API returns { success: true, data: {...} } format
161+
# Check for data.token or direct token field
162+
payload = data.get("data", data)
163+
if "token" in payload:
164+
return TokenResponse(
165+
token=payload["token"],
166+
user=payload.get("user", {})
167+
)
162168

163169
except requests.RequestException as e:
164170
# Network error, retry

tests/unit/test_api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def test_post_success(self, mock_post):
3939
"""Test successful POST request."""
4040
mock_response = MagicMock()
4141
mock_response.ok = True
42-
mock_response.json.return_value = {"submissionId": "123", "status": "EVALUATING"}
42+
mock_response.json.return_value = {
43+
"success": True,
44+
"data": {"submissionId": "123", "status": "EVALUATING"}
45+
}
4346
mock_post.return_value = mock_response
4447

4548
client = APIClient(token="token")

tests/unit/test_auth.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,14 @@ def test_start_device_flow_success(self, mock_post):
9696
mock_response = MagicMock()
9797
mock_response.ok = True
9898
mock_response.json.return_value = {
99-
"deviceCode": "abc123",
100-
"userCode": "ABCD-1234",
101-
"verificationUri": "https://github.com/login/device",
102-
"expiresIn": 900,
103-
"interval": 5,
99+
"success": True,
100+
"data": {
101+
"deviceCode": "abc123",
102+
"userCode": "ABCD-1234",
103+
"verificationUri": "https://github.com/login/device",
104+
"expiresIn": 900,
105+
"interval": 5,
106+
}
104107
}
105108
mock_post.return_value = mock_response
106109

0 commit comments

Comments
 (0)