Tusker/Pachyderm/Sources/Pachyderm/Model/RegisteredApplication.swift

41 lines
1.4 KiB
Swift

//
// RegisteredApplication.swift
// Pachyderm
//
// Created by Shadowfacts on 9/9/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import Foundation
public class RegisteredApplication: Decodable {
public let id: String
public let clientID: String
public let clientSecret: String
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// Pixelfed API returns id/client_id as numbers instead of strings
func decodeStringOrInt(key: CodingKeys) throws -> String {
if let str = try? container.decode(String.self, forKey: key) {
return str
} else if let int = try? container.decode(Int.self, forKey: key) {
return int.description
} else {
throw DecodingError.typeMismatch(String.self, DecodingError.Context(codingPath: container.codingPath + [CodingKeys.id], debugDescription: ""))
}
}
self.id = try decodeStringOrInt(key: .id)
self.clientID = try decodeStringOrInt(key: .clientID)
self.clientSecret = try container.decode(String.self, forKey: .clientSecret)
}
private enum CodingKeys: String, CodingKey {
case id
case clientID = "client_id"
case clientSecret = "client_secret"
}
}