2018-09-11 14:52:21 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2020-10-26 03:07:41 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:52:21 +00:00
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
|
|
case id
|
|
|
|
case clientID = "client_id"
|
|
|
|
case clientSecret = "client_secret"
|
|
|
|
}
|
|
|
|
}
|