import Foundation

struct ServerKeyErrorModel: Codable {
    
    let api_status : APIStatus?
    let errors : Errors?
    
    enum CodingKeys: String, CodingKey {
        
        case api_status = "api_status"
        case errors = "errors"
    }
    
    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        api_status = try values.decodeIfPresent(APIStatus.self, forKey: .api_status)
        errors = try values.decodeIfPresent(Errors.self, forKey: .errors)
    }
    
}

enum APIStatus: Codable {
    
    case integer(Int)
    case string(String)
    
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let x = try? container.decode(Int.self) {
            self = .integer(x)
            return
        }
        if let x = try? container.decode(String.self) {
            self = .string(x)
            return
        }
        throw DecodingError.typeMismatch(APIStatus.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for APIStatus"))
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .integer(let x):
            try container.encode(x)
        case .string(let x):
            try container.encode(x)
        }
    }
    
}
