From 793db8bac9a55a0942b21ff5f86979614481f308 Mon Sep 17 00:00:00 2001 From: Wilhelm Oks Date: Wed, 18 Dec 2024 15:35:42 +0100 Subject: [PATCH] Requests WIP: getRant --- Sources/SwiftDevRant/SwiftDevRant.swift | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Sources/SwiftDevRant/SwiftDevRant.swift b/Sources/SwiftDevRant/SwiftDevRant.swift index 39c3bb7..7beac02 100644 --- a/Sources/SwiftDevRant/SwiftDevRant.swift +++ b/Sources/SwiftDevRant/SwiftDevRant.swift @@ -133,4 +133,28 @@ public struct SwiftDevRant { return response.data.decoded } + + /// Gets a single rant and its comments. + /// + /// - Parameters: + /// - token: The token from the `logIn` call response. + /// - rantId: The id of the rant. + /// - lastCommentId: Only fetch the comments which were posted after the one corresponding to this id. Pass `nil` to get all comments. + public func getRant(token: AuthToken, rantId: Int, lastCommentId: Int? = nil) async throws -> (rant: Rant, comments: [Comment]) { + var parameters: [String: String] = [:] + + parameters["last_comment_id"] = lastCommentId.flatMap { String($0) } + //parameters["ver"] = "1.17.0.4" //TODO: check if this is needed + + let config = makeConfig(.get, path: "devrant/rants/\(rantId)", urlParameters: parameters) + + struct Response: Codable { + let rant: Rant.CodingData + let comments: [Comment.CodingData]? + } + + let response: Response = try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self) + + return (rant: response.rant.decoded, comments: response.comments?.map(\.decoded) ?? []) + } }