Tweaks to support decoding Pixelfed instance response

This commit is contained in:
Shadowfacts 2019-09-22 18:57:33 -04:00
parent 682d68fd81
commit b81e4d0a9e
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 38 additions and 7 deletions

View File

@ -12,19 +12,49 @@ public class Instance: Decodable {
public let uri: String public let uri: String
public let title: String public let title: String
public let description: String public let description: String
public let email: String public let email: String?
public let version: String public let version: String
public let urls: [String: URL] public let urls: [String: URL]
public let thumbnail: URL?
public let languages: [String]?
public let stats: Stats?
// pleroma doesn't currently implement these // pleroma doesn't currently implement these
public let languages: [String]?
public let contactAccount: Account? public let contactAccount: Account?
// MARK: Unofficial additions to the Mastodon API. // MARK: Unofficial additions to the Mastodon API.
public let stats: Stats?
public let thumbnail: URL?
public let maxStatusCharacters: Int? public let maxStatusCharacters: Int?
// we need a custom decoder, because all API-compatible implementations don't return some data in the same format
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.uri = try container.decode(String.self, forKey: .uri)
self.title = try container.decode(String.self, forKey: .title)
self.description = try container.decode(String.self, forKey: .description)
self.email = try container.decodeIfPresent(String.self, forKey: .email)
self.version = try container.decode(String.self, forKey: .version)
if let urls = try? container.decodeIfPresent([String: URL].self, forKey: .urls) {
self.urls = urls
} else {
self.urls = [:]
}
self.languages = try? container.decodeIfPresent([String].self, forKey: .languages)
self.contactAccount = try? container.decodeIfPresent(Account.self, forKey: .contactAccount)
self.stats = try? container.decodeIfPresent(Stats.self, forKey: .stats)
self.thumbnail = try? container.decodeIfPresent(URL.self, forKey: .thumbnail)
if let maxStatusCharacters = try? container.decodeIfPresent(Int.self, forKey: .maxStatusCharacters) {
self.maxStatusCharacters = maxStatusCharacters
} else if let str = try? container.decodeIfPresent(String.self, forKey: .maxStatusCharacters),
let maxStatusCharacters = Int(str, radix: 10) {
self.maxStatusCharacters = maxStatusCharacters
} else {
self.maxStatusCharacters = nil
}
}
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case uri case uri
case title case title
@ -32,11 +62,12 @@ public class Instance: Decodable {
case email case email
case version case version
case urls case urls
case thumbnail
case languages case languages
case stats
case contactAccount = "contact_account" case contactAccount = "contact_account"
case stats
case thumbnail
case maxStatusCharacters = "max_toot_chars" case maxStatusCharacters = "max_toot_chars"
} }
} }