From 7da4c186e1b3121b6d9d17fa29b1400229aaaa95 Mon Sep 17 00:00:00 2001 From: Zac West Date: Tue, 15 Mar 2022 20:16:08 -0700 Subject: [PATCH 1/2] Handle onboarding authentication errors in /api/v1/instance --- .../API/Mastodon+API+Instance.swift | 13 +++++++++++- .../Entity/Mastodon+Entity+Instance.swift | 21 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Instance.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Instance.swift index e91aaa7b..e90ee27c 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Instance.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Instance.swift @@ -37,7 +37,18 @@ extension Mastodon.API.Instance { ) return session.dataTaskPublisher(for: request) .tryMap { data, response in - let value = try Mastodon.API.decode(type: Mastodon.Entity.Instance.self, from: data, response: response) + let value: Mastodon.Entity.Instance + + do { + value = try Mastodon.API.decode(type: Mastodon.Entity.Instance.self, from: data, response: response) + } catch { + if let response = response as? HTTPURLResponse, 400 ..< 500 ~= response.statusCode { + // For example, AUTHORIZED_FETCH may result in authentication errors + value = Mastodon.Entity.Instance(domain: domain) + } else { + throw error + } + } return Mastodon.Response.Content(value: value, response: response) } .eraseToAnyPublisher() diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Instance.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Instance.swift index f245d741..0f9392ad 100644 --- a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Instance.swift +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Instance.swift @@ -22,7 +22,7 @@ extension Mastodon.Entity { public let title: String public let description: String public let shortDescription: String? - public let email: String + public let email: String? public let version: String? public let languages: [String]? // (ISO 639 Part 1-5 language codes) public let registrations: Bool? @@ -38,6 +38,25 @@ extension Mastodon.Entity { // https://github.com/mastodon/mastodon/pull/16485 public let configuration: Configuration? + public init(domain: String) { + self.uri = domain + self.title = domain + self.description = "" + self.shortDescription = nil + self.email = nil + self.version = nil + self.languages = nil + self.registrations = nil + self.approvalRequired = nil + self.invitesEnabled = nil + self.urls = nil + self.statistics = nil + self.thumbnail = nil + self.contactAccount = nil + self.rules = nil + self.configuration = nil + } + enum CodingKeys: String, CodingKey { case uri case title From a2ac3ea5d32bf1c354915ca5d3f57cc97ca8ede1 Mon Sep 17 00:00:00 2001 From: CMK Date: Sat, 19 Mar 2022 01:19:07 +0800 Subject: [PATCH 2/2] chore: retain the API model semantic --- .../Sources/MastodonSDK/Entity/Mastodon+Entity+Instance.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Instance.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Instance.swift index 0f9392ad..7cf4890b 100644 --- a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Instance.swift +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Instance.swift @@ -22,7 +22,7 @@ extension Mastodon.Entity { public let title: String public let description: String public let shortDescription: String? - public let email: String? + public let email: String public let version: String? public let languages: [String]? // (ISO 639 Part 1-5 language codes) public let registrations: Bool? @@ -43,7 +43,7 @@ extension Mastodon.Entity { self.title = domain self.description = "" self.shortDescription = nil - self.email = nil + self.email = "" self.version = nil self.languages = nil self.registrations = nil