Skip to content

Commit bc0b6c7

Browse files
committed
feat: parse V2 pagination and the fields only V2 returns
Captured real V2 responses (all seven endpoints) and diffed them against the models. V2 and V1 payloads differ more than assumed: - every paged endpoint returns a top-level pagination object {per_page, total, pages} that V2Response ignored, so callers could not build correct paging UI at all - V2Topic carries syntax/stars/thanks/supplements (and omits deleted) - V2Node carries a single avatar plus created/last_modified - nested members in a V2 topic only contain 9 fields, confirming that optional properties are required rather than defensive Adds V2Response.Pagination and V2Topic.Supplement, plus 14 tests driven by the captured payloads.
1 parent e31b2ea commit bc0b6c7

6 files changed

Lines changed: 497 additions & 3 deletions

File tree

‎Sources/V2exAPI/Models/V2Node.swift‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public struct V2Node: Identifiable, Decodable, Hashable, Sendable {
4545
public let parentNodeName: String?
4646
/// 节点创建者 ID(0 表示官方创建)
4747
public let founderId: Int?
48+
/// 头像(V2 接口返回的单一 avatar 字段)
49+
public let avatar: String?
50+
/// 节点创建时间(Unix 时间戳,秒,V2 独有)
51+
public let created: Int?
52+
/// 节点最后修改时间(Unix 时间戳,秒,V2 独有)
53+
public let lastModified: Int?
4854

4955
/// 手动构造,主要便于测试与 Mock
5056
public init(
@@ -63,7 +69,10 @@ public struct V2Node: Identifiable, Decodable, Hashable, Sendable {
6369
root: Bool? = nil,
6470
id: Int? = nil,
6571
parentNodeName: String? = nil,
66-
founderId: Int? = nil
72+
founderId: Int? = nil,
73+
avatar: String? = nil,
74+
created: Int? = nil,
75+
lastModified: Int? = nil
6776
) {
6877
self.name = name
6978
self.avatarLarge = avatarLarge
@@ -81,6 +90,9 @@ public struct V2Node: Identifiable, Decodable, Hashable, Sendable {
8190
self.id = id
8291
self.parentNodeName = parentNodeName
8392
self.founderId = founderId
93+
self.avatar = avatar
94+
self.created = created
95+
self.lastModified = lastModified
8496
}
8597

8698
enum CodingKeys: String, CodingKey {
@@ -91,5 +103,7 @@ public struct V2Node: Identifiable, Decodable, Hashable, Sendable {
91103
case avatarMini = "avatar_mini"
92104
case parentNodeName = "parent_node_name"
93105
case founderId = "founder_id"
106+
case lastModified = "last_modified"
107+
case avatar, created
94108
}
95109
}

‎Sources/V2exAPI/Models/V2Response.swift‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,46 @@ public struct V2Response<T: Decodable>: Decodable {
3030
public let message: String?
3131
/// 实际数据,可能为 `nil`
3232
public let result: T?
33+
/// 分页信息,仅分页接口(如节点主题、主题回复)会返回
34+
public let pagination: Pagination?
3335

3436
/// 手动构造响应,主要便于测试与 Mock
35-
public init(success: Bool, message: String? = nil, result: T? = nil) {
37+
public init(
38+
success: Bool,
39+
message: String? = nil,
40+
result: T? = nil,
41+
pagination: Pagination? = nil
42+
) {
3643
self.success = success
3744
self.message = message
3845
self.result = result
46+
self.pagination = pagination
47+
}
48+
}
49+
50+
extension V2Response {
51+
/// 分页信息
52+
///
53+
/// 分页接口会在响应顶层额外返回该对象,例如:
54+
/// `{"per_page": 20, "total": 372, "pages": 19}`
55+
public struct Pagination: Decodable, Hashable, Sendable {
56+
/// 每页条目数
57+
public let perPage: Int?
58+
/// 总条目数
59+
public let total: Int?
60+
/// 总页数
61+
public let pages: Int?
62+
63+
public init(perPage: Int? = nil, total: Int? = nil, pages: Int? = nil) {
64+
self.perPage = perPage
65+
self.total = total
66+
self.pages = pages
67+
}
68+
69+
enum CodingKeys: String, CodingKey {
70+
case perPage = "per_page"
71+
case total, pages
72+
}
3973
}
4074
}
4175

@@ -45,6 +79,8 @@ extension V2Response: Equatable where T: Equatable {}
4579
extension V2Response: Hashable where T: Hashable {}
4680
extension V2Response: Sendable where T: Sendable {}
4781

82+
// 合成实现会包含 pagination,因此无需额外处理
83+
4884
/// 仅用于解析错误响应体中的 `message` / `error` 字段
4985
struct V2exAPIFailure: Decodable {
5086
let message: String?

‎Sources/V2exAPI/Models/V2Topic.swift‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public struct V2Topic: Identifiable, Decodable, Hashable, Sendable {
3838
public let lastModified: Int?
3939
/// 回复数
4040
public let replies: Int?
41+
/// 正文所使用的语法标记(V2 独有,1 表示支持 Markdown)
42+
public let syntax: Int?
43+
/// 星标数(V2 独有)
44+
public let stars: Int?
45+
/// 感谢数(V2 独有)
46+
public let thanks: Int?
47+
/// 附言列表(V2 独有)
48+
public let supplements: [Supplement]?
4149

4250
/// 手动构造,主要便于测试与 Mock
4351
public init(
@@ -53,7 +61,11 @@ public struct V2Topic: Identifiable, Decodable, Hashable, Sendable {
5361
content: String? = nil,
5462
contentRendered: String? = nil,
5563
lastModified: Int? = nil,
56-
replies: Int? = nil
64+
replies: Int? = nil,
65+
syntax: Int? = nil,
66+
stars: Int? = nil,
67+
thanks: Int? = nil,
68+
supplements: [Supplement]? = nil
5769
) {
5870
self.id = id
5971
self.node = node
@@ -68,6 +80,10 @@ public struct V2Topic: Identifiable, Decodable, Hashable, Sendable {
6880
self.contentRendered = contentRendered
6981
self.lastModified = lastModified
7082
self.replies = replies
83+
self.syntax = syntax
84+
self.stars = stars
85+
self.thanks = thanks
86+
self.supplements = supplements
7187
}
7288

7389
enum CodingKeys: String, CodingKey {
@@ -78,5 +94,32 @@ public struct V2Topic: Identifiable, Decodable, Hashable, Sendable {
7894
case contentRendered = "content_rendered"
7995
case lastModified = "last_modified"
8096
case replies, id
97+
case syntax, stars, thanks, supplements
98+
}
99+
}
100+
101+
extension V2Topic {
102+
/// 主题附言(V2 独有)
103+
public struct Supplement: Decodable, Hashable, Sendable {
104+
/// 附言 ID
105+
public let id: Int?
106+
/// 附言原文
107+
public let content: String?
108+
/// 附言渲染后的 HTML
109+
public let contentRendered: String?
110+
/// 创建时间(Unix 时间戳,秒)
111+
public let created: Int?
112+
113+
public init(id: Int? = nil, content: String? = nil, contentRendered: String? = nil, created: Int? = nil) {
114+
self.id = id
115+
self.content = content
116+
self.contentRendered = contentRendered
117+
self.created = created
118+
}
119+
120+
enum CodingKeys: String, CodingKey {
121+
case id, content, created
122+
case contentRendered = "content_rendered"
123+
}
81124
}
82125
}

‎Tests/V2exAPITests/LiveFixtures.swift‎

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,203 @@ enum LiveFixtures {
141141
"status": "found"
142142
}
143143
"""#
144+
145+
// MARK: - V2 真实响应(单条、已脱敏)
146+
147+
/// GET /api/v2/topics/:id
148+
static let v2Topic = #"""
149+
{
150+
"success": true,
151+
"message": "Topic details found",
152+
"result": {
153+
"id": 1000,
154+
"title": "Google App Engine x MobileMe",
155+
"content": "从现在开始,新上传到 V2EX 的头像将存储在 MobileMe iDisk 中。这是 V2EX 到目前为之所用到的第三个云。\r\n\r\n得益于这个架构升级,现在头像上传之后,将立刻在全站的所有页面更新。",
156+
"content_rendered": "从现在开始,新上传到 V2EX 的头像将存储在 MobileMe iDisk 中。这是 V2EX 到目前为之所用到的第三个云。\r\n<br />\r\n<br />得益于这个架构升级,现在头像上传之后,将立刻在全站的所有页面更新。",
157+
"syntax": 0,
158+
"url": "https://example.com",
159+
"replies": 14,
160+
"stars": 0,
161+
"thanks": 0,
162+
"last_reply_by": "user-redacted",
163+
"created": 1280192329,
164+
"last_modified": 1335004238,
165+
"last_touched": 1280285385,
166+
"member": {
167+
"id": 1,
168+
"username": "user-redacted",
169+
"bio": "",
170+
"website": "sepia.sol.build",
171+
"github": "",
172+
"url": "https://example.com",
173+
"avatar": "https://example.com/avatar.png",
174+
"created": 1272203146,
175+
"pro": 1792299120
176+
},
177+
"node": {
178+
"id": 1,
179+
"founder_id": 0,
180+
"url": "https://example.com",
181+
"name": "babel",
182+
"title": "Project Babel",
183+
"header": "",
184+
"footer": "",
185+
"avatar": "https://example.com/avatar.png",
186+
"topics": 1123,
187+
"created": 1272206882,
188+
"last_modified": 1700044199
189+
},
190+
"supplements": []
191+
}
192+
}
193+
"""#
194+
195+
/// GET /api/v2/nodes/:name
196+
static let v2Node = #"""
197+
{
198+
"success": true,
199+
"message": "Node details found",
200+
"result": {
201+
"id": 638,
202+
"founder_id": 0,
203+
"url": "https://example.com",
204+
"name": "swift",
205+
"title": "Swift",
206+
"header": "来自 Apple 的类型安全的编程语言。",
207+
"footer": "",
208+
"avatar": "https://example.com/avatar.png",
209+
"topics": 372,
210+
"created": 1355384712,
211+
"last_modified": 1661630260
212+
}
213+
}
214+
"""#
215+
216+
/// GET /api/v2/nodes/:name/topics(含 pagination)
217+
static let v2NodeTopics = #"""
218+
{
219+
"success": true,
220+
"message": "Node topics found",
221+
"result": [
222+
{
223+
"id": 1243658,
224+
"title": "我开源了一个 Swift 下的 Markdown 解析器和渲染器,如果你的项目中需要解析 Markdown,可以试试",
225+
"content": "代码在: https://github.com/daqing/danbo-swift-markdown",
226+
"content_rendered": "<p>代码在: <a href=\"https://github.com/daqing/danbo-swift-markdown\" rel=\"nofollow\">https://github.com/daqing/danbo-swift-markdown</a></p>\n",
227+
"syntax": 1,
228+
"url": "https://example.com",
229+
"replies": 2,
230+
"stars": 1,
231+
"thanks": 0,
232+
"last_reply_by": "user-redacted",
233+
"created": 1789974967,
234+
"last_modified": 1789975036,
235+
"last_touched": 1790522019
236+
}
237+
],
238+
"pagination": {
239+
"per_page": 20,
240+
"total": 372,
241+
"pages": 19
242+
}
243+
}
244+
"""#
245+
246+
/// GET /api/v2/topics/:id/replies(含 pagination)
247+
static let v2Replies = #"""
248+
{
249+
"success": true,
250+
"message": "Topic replies found",
251+
"result": [
252+
{
253+
"id": 6847,
254+
"content": "右下角的图标该变成三个了。 ^^",
255+
"content_rendered": "右下角的图标该变成三个了。 ^^",
256+
"created": 1280192713,
257+
"member": {
258+
"id": 123,
259+
"username": "user-redacted",
260+
"bio": "",
261+
"website": "",
262+
"github": "romoo",
263+
"url": "https://example.com",
264+
"avatar": "https://example.com/avatar.png",
265+
"created": 1272264436,
266+
"pro": 0
267+
}
268+
}
269+
],
270+
"pagination": {
271+
"per_page": 20,
272+
"total": 14,
273+
"pages": 1
274+
}
275+
}
276+
"""#
277+
278+
/// GET /api/v2/member
279+
static let v2Member = #"""
280+
{
281+
"success": true,
282+
"result": {
283+
"id": 42376,
284+
"username": "user-redacted",
285+
"url": "https://example.com",
286+
"website": "www.isaced.com",
287+
"twitter": "isacedx",
288+
"psn": "",
289+
"github": "isaced",
290+
"btc": "",
291+
"location": "",
292+
"tagline": "",
293+
"bio": "",
294+
"avatar_mini": "https://example.com/avatar.png",
295+
"avatar_normal": "https://example.com/avatar.png",
296+
"avatar_large": "https://example.com/avatar.png",
297+
"created": 1374317846,
298+
"last_modified": 1664252897,
299+
"pro": 0
300+
}
301+
}
302+
"""#
303+
304+
/// GET /api/v2/token
305+
static let v2Token = #"""
306+
{
307+
"success": true,
308+
"message": "Current token details",
309+
"result": {
310+
"token": "REDACTED-TOKEN",
311+
"scope": "everything",
312+
"expiration": 2592000,
313+
"good_for_days": 30,
314+
"total_used": 13,
315+
"last_used": 1792303234,
316+
"created": 1792302304
317+
}
318+
}
319+
"""#
320+
321+
/// GET /api/v2/notifications
322+
static let v2Notifications = #"""
323+
{
324+
"success": true,
325+
"message": "Notifications 1-10/410",
326+
"result": [
327+
{
328+
"id": 28177232,
329+
"member_id": 348660,
330+
"for_member_id": 42376,
331+
"text": "<a href=\"/member/hannibalaikete\" target=\"_blank\"><strong>hannibalaikete</strong></a> 收藏了你发布的主题 › <a href=\"/t/883252#reply72\" class=\"topic-link\">V2exOS - 用 SwiftUI 写了一个 V2EX 客户端</a>",
332+
"payload": null,
333+
"payload_rendered": "",
334+
"created": 1786422119,
335+
"member": {
336+
"username": "user-redacted"
337+
}
338+
}
339+
]
340+
}
341+
"""#
342+
144343
}

0 commit comments

Comments
 (0)