From fa362e24b6c7a7655c2aeda8d1468065335e03ad Mon Sep 17 00:00:00 2001 From: Wilhelm Oks Date: Mon, 23 Dec 2024 16:04:19 +0100 Subject: [PATCH] fixed the Profile request --- Sources/SwiftDevRant/DevRantRequest.swift | 2 +- Sources/SwiftDevRant/Models/Profile.swift | 38 +++++++++++++---------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Sources/SwiftDevRant/DevRantRequest.swift b/Sources/SwiftDevRant/DevRantRequest.swift index dc3cb6f..9946803 100644 --- a/Sources/SwiftDevRant/DevRantRequest.swift +++ b/Sources/SwiftDevRant/DevRantRequest.swift @@ -256,7 +256,7 @@ public extension DevRantRequest { let config = makeConfig(.get, path: "users/\(userId)", urlParameters: parameters, token: token) - let response: Profile.CodingData = try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self) + let response: Profile.CodingData.Container = try await request.requestJson(config: config, apiError: DevRantApiError.CodingData.self) return response.decoded } diff --git a/Sources/SwiftDevRant/Models/Profile.swift b/Sources/SwiftDevRant/Models/Profile.swift index 3d7c728..325815e 100644 --- a/Sources/SwiftDevRant/Models/Profile.swift +++ b/Sources/SwiftDevRant/Models/Profile.swift @@ -39,7 +39,7 @@ public struct Profile: Hashable, Sendable { public let devRantSupporter: Bool /// True if the logged in user is subscribed to the user of this profile. - public var subscribed: Bool //TODO: where is this set? It's not in the json data of the profile + public var subscribed: Bool public init(username: String, score: Int, created: Date, about: String?, location: String?, skills: String?, github: String?, website: String?, content: Profile.Content, avatarLarge: User.Avatar, avatarSmall: User.Avatar, devRantSupporter: Bool, subscribed: Bool) { self.username = username @@ -82,6 +82,11 @@ public extension Profile { extension Profile { struct CodingData: Codable { + struct Container: Codable { + let profile: Profile.CodingData + let subscribed: Int? + } + let username: String let score: Int let created_time: Int @@ -94,26 +99,25 @@ extension Profile { let avatar: User.Avatar.CodingData let avatar_sm: User.Avatar.CodingData let dpp: Int? - let subscribed: Bool? //TODO: check if it exists in the json data } } -extension Profile.CodingData { +extension Profile.CodingData.Container { var decoded: Profile { - .init( - username: username, - score: score, - created: Date(timeIntervalSince1970: TimeInterval(created_time)), - about: about.isEmpty ? nil : about, - location: location.isEmpty ? nil : location, - skills: skills.isEmpty ? nil : skills, - github: github.isEmpty ? nil : github, - website: website.isEmpty ? nil : website, - content: content.decoded, - avatarLarge: avatar.decoded, - avatarSmall: avatar_sm.decoded, - devRantSupporter: (dpp ?? 0) != 0, - subscribed: subscribed ?? false + return .init( + username: profile.username, + score: profile.score, + created: Date(timeIntervalSince1970: TimeInterval(profile.created_time)), + about: profile.about.isEmpty ? nil : profile.about, + location: profile.location.isEmpty ? nil : profile.location, + skills: profile.skills.isEmpty ? nil : profile.skills, + github: profile.github.isEmpty ? nil : profile.github, + website: profile.website.isEmpty ? nil : profile.website, + content: profile.content.decoded, + avatarLarge: profile.avatar.decoded, + avatarSmall: profile.avatar_sm.decoded, + devRantSupporter: (profile.dpp ?? 0) != 0, + subscribed: (subscribed ?? 0) != 0 ) } }