|
| 1 | +// |
| 2 | +// APIResponse.swift |
| 3 | +// SwiftStack |
| 4 | +// |
| 5 | +// Created by FelixSFD on 11.12.16. |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +/** |
| 12 | + The common wrapper object that is returned by the StackExchange API. |
| 13 | + |
| 14 | + - authors: FelixSFD, NobodyNada |
| 15 | + |
| 16 | + - seealso: [StackExchange API](https://api.stackexchange.com/docs/wrapper) |
| 17 | + */ |
| 18 | +public class APIResponse<T: JsonConvertible>: JsonConvertible { |
| 19 | + |
| 20 | + // - MARK: Items |
| 21 | + |
| 22 | + /** |
| 23 | + The items that are returned of the generic type `T`. |
| 24 | + |
| 25 | + - note: It's always an array. Even if only a single item was requested. |
| 26 | + |
| 27 | + - author: FelixSFD |
| 28 | + */ |
| 29 | + public var items: [T]? |
| 30 | + |
| 31 | + |
| 32 | + // - MARK: Initializers |
| 33 | + |
| 34 | + /** |
| 35 | + Basic initializer without default values |
| 36 | + */ |
| 37 | + public init() { |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + public required convenience init?(jsonString json: String) { |
| 42 | + do { |
| 43 | + guard let dictionary = try JSONSerialization.jsonObject(with: json.data(using: String.Encoding.utf8)!) as? [String: Any] else { |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + self.init(dictionary: dictionary) |
| 48 | + } catch { |
| 49 | + return nil |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public required init(dictionary: [String: Any]) { |
| 54 | + self.backoff = dictionary["backoff"] as? Int |
| 55 | + self.error_id = dictionary["error_id"] as? Int |
| 56 | + self.error_message = dictionary["error_message"] as? String |
| 57 | + self.error_name = dictionary["error_name"] as? String |
| 58 | + self.has_more = dictionary["has_more"] as? Bool |
| 59 | + self.page = dictionary["page"] as? Int |
| 60 | + self.page_size = dictionary["page_size"] as? Int |
| 61 | + self.quota_remaining = dictionary["quota_remaining"] as? Int |
| 62 | + self.quota_max = dictionary["quota_max"] as? Int |
| 63 | + self.total = dictionary["total"] as? Int |
| 64 | + self.type = dictionary["type"] as? String |
| 65 | + |
| 66 | + |
| 67 | + //items |
| 68 | + if let array = dictionary["items"] as? [[String: Any]] { |
| 69 | + items = array.map { T(dictionary: $0) } |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + // - MARK: JsonConvertible |
| 75 | + |
| 76 | + public var dictionary: [String: Any] { |
| 77 | + var dict = [String: Any]() |
| 78 | + |
| 79 | + dict["backoff"] = backoff |
| 80 | + dict["error_id"] = error_id |
| 81 | + dict["error_message"] = error_message |
| 82 | + dict["error_name"] = error_name |
| 83 | + dict["has_more"] = has_more |
| 84 | + dict["page"] = page |
| 85 | + dict["page_size"] = page_size |
| 86 | + dict["quota_max"] = quota_max |
| 87 | + dict["quota_remaining"] = quota_remaining |
| 88 | + dict["total"] = total |
| 89 | + dict["type"] = type |
| 90 | + |
| 91 | + return dict |
| 92 | + } |
| 93 | + |
| 94 | + public var jsonString: String? { |
| 95 | + return (try? JsonHelper.jsonString(from: self)) ?? nil |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + // - MARK: Fields |
| 100 | + |
| 101 | + public var backoff: Int? |
| 102 | + |
| 103 | + public var error_id: Int? |
| 104 | + |
| 105 | + public var error_message: String? |
| 106 | + |
| 107 | + public var error_name: String? |
| 108 | + |
| 109 | + public var has_more: Bool? |
| 110 | + |
| 111 | + public var page: Int? |
| 112 | + |
| 113 | + public var page_size: Int? |
| 114 | + |
| 115 | + public var quota_max: Int? |
| 116 | + |
| 117 | + public var quota_remaining: Int? |
| 118 | + |
| 119 | + public var total: Int? |
| 120 | + |
| 121 | + public var type: String? |
| 122 | + |
| 123 | +} |
0 commit comments