|
| 1 | +import Codegen |
| 2 | + |
| 3 | +/// Media from WordPress Site API |
| 4 | +public struct WordPressMedia: Equatable { |
| 5 | + public let mediaID: Int64 |
| 6 | + public let date: Date |
| 7 | + public let slug: String |
| 8 | + public let mimeType: String |
| 9 | + public let src: String |
| 10 | + public let alt: String? |
| 11 | + public let details: MediaDetails? |
| 12 | + public let title: MediaTitle? |
| 13 | + |
| 14 | + /// Media initializer. |
| 15 | + public init(mediaID: Int64, |
| 16 | + date: Date, |
| 17 | + slug: String, |
| 18 | + mimeType: String, |
| 19 | + src: String, |
| 20 | + alt: String?, |
| 21 | + details: MediaDetails?, |
| 22 | + title: MediaTitle?) { |
| 23 | + self.mediaID = mediaID |
| 24 | + self.date = date |
| 25 | + self.slug = slug |
| 26 | + self.mimeType = mimeType |
| 27 | + self.src = src |
| 28 | + self.alt = alt |
| 29 | + self.details = details |
| 30 | + self.title = title |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +extension WordPressMedia: Decodable { |
| 35 | + /// Decodable Initializer. |
| 36 | + public init(from decoder: Decoder) throws { |
| 37 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 38 | + |
| 39 | + let mediaID = try container.decode(Int64.self, forKey: .mediaID) |
| 40 | + let date = try container.decodeIfPresent(Date.self, forKey: .date) ?? Date() |
| 41 | + let slug = try container.decodeIfPresent(String.self, forKey: .slug) ?? "" |
| 42 | + let mimeType = try container.decodeIfPresent(String.self, forKey: .mimeType) ?? "" |
| 43 | + let src = try container.decodeIfPresent(String.self, forKey: .src) ?? "" |
| 44 | + let alt = try container.decodeIfPresent(String.self, forKey: .alt) |
| 45 | + let details = try container.decodeIfPresent(MediaDetails.self, forKey: .details) |
| 46 | + let title = try container.decodeIfPresent(MediaTitle.self, forKey: .title) |
| 47 | + |
| 48 | + self.init(mediaID: mediaID, |
| 49 | + date: date, |
| 50 | + slug: slug, |
| 51 | + mimeType: mimeType, |
| 52 | + src: src, |
| 53 | + alt: alt, |
| 54 | + details: details, |
| 55 | + title: title) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +public extension WordPressMedia { |
| 60 | + /// Details about a WordPress site media. |
| 61 | + struct MediaDetails: Decodable, Equatable { |
| 62 | + public let width: Double |
| 63 | + public let height: Double |
| 64 | + public let fileName: String |
| 65 | + public let sizes: [String: MediaSizeDetails] |
| 66 | + |
| 67 | + enum CodingKeys: String, CodingKey { |
| 68 | + case width |
| 69 | + case height |
| 70 | + case fileName = "file" |
| 71 | + case sizes |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Details about a size of WordPress site media (e.g. `thumbnail`, `medium`, `2048x2048`). |
| 76 | + struct MediaSizeDetails: Decodable, Equatable { |
| 77 | + public let fileName: String |
| 78 | + public let src: String |
| 79 | + public let width: Double |
| 80 | + public let height: Double |
| 81 | + |
| 82 | + enum CodingKeys: String, CodingKey { |
| 83 | + case fileName = "file" |
| 84 | + case src = "source_url" |
| 85 | + case width |
| 86 | + case height |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /// Title of the WordPress site media. |
| 91 | + struct MediaTitle: Decodable, Equatable { |
| 92 | + /// `GET` media list request's `title` field only contains `rendered`, while `POST` media request includes both `raw` and `rendered`. |
| 93 | + let rendered: String |
| 94 | + |
| 95 | + enum CodingKeys: String, CodingKey { |
| 96 | + case rendered |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +private extension WordPressMedia { |
| 102 | + enum CodingKeys: String, CodingKey { |
| 103 | + case mediaID = "id" |
| 104 | + case date = "date_gmt" |
| 105 | + case slug |
| 106 | + case mimeType = "mime_type" |
| 107 | + case src = "source_url" |
| 108 | + case alt = "alt_text" |
| 109 | + case details = "media_details" |
| 110 | + case title |
| 111 | + } |
| 112 | +} |
0 commit comments