|
| 1 | +import Foundation |
| 2 | + |
| 3 | +extension DomainsServiceRemote { |
| 4 | + |
| 5 | + // MARK: - API |
| 6 | + |
| 7 | + /// Makes a call request to `GET /v1.1/all-domains` and returns a list of domain objects. |
| 8 | + /// |
| 9 | + /// The endpoint accepts 3 **optionals** query params: |
| 10 | + /// - `resolve_status` of type `boolean`. If `true`, the response will include a `status` attribute for each `domain` object. |
| 11 | + /// - `no_wpcom`of type `boolean`. If `true`, the respnse won't include `wpcom` domains. |
| 12 | + /// - `locale` of type `string`. Used for string localization. |
| 13 | + public func fetchAllDomains(params: AllDomainsEndpointParams? = nil, completion: @escaping (AllDomainsEndpointResult) -> Void) { |
| 14 | + let path = self.path(forEndpoint: "all-domains", withVersion: ._1_1) |
| 15 | + var parameters: [String: AnyObject]? |
| 16 | + |
| 17 | + do { |
| 18 | + parameters = try queryParameters(from: params) |
| 19 | + } catch let error { |
| 20 | + completion(.failure(error)) |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + self.wordPressComRestApi.GET(path, parameters: parameters) { result, _ in |
| 25 | + do { |
| 26 | + switch result { |
| 27 | + case .success(let result): |
| 28 | + let data = try JSONSerialization.data(withJSONObject: result, options: []) |
| 29 | + let decoder = JSONDecoder() |
| 30 | + decoder.dateDecodingStrategy = .iso8601 |
| 31 | + let result = try decoder.decode(AllDomainsEndpointResponse.self, from: data) |
| 32 | + completion(.success(result.domains)) |
| 33 | + case .failure(let error): |
| 34 | + throw error |
| 35 | + } |
| 36 | + } catch let error { |
| 37 | + completion(.failure(error)) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private func queryParameters(from params: AllDomainsEndpointParams?) throws -> [String: AnyObject]? { |
| 43 | + guard let params else { |
| 44 | + return nil |
| 45 | + } |
| 46 | + let encoder = JSONEncoder() |
| 47 | + let data = try encoder.encode(params) |
| 48 | + let dict = try JSONSerialization.jsonObject(with: data) as? [String: AnyObject] |
| 49 | + return dict |
| 50 | + } |
| 51 | + |
| 52 | + // MARK: - Public Types |
| 53 | + |
| 54 | + public typealias AllDomainsEndpointResult = Result<[AllDomainsListItem], Error> |
| 55 | + |
| 56 | + public struct AllDomainsEndpointParams { |
| 57 | + |
| 58 | + public var resolveStatus: Bool = false |
| 59 | + public var noWPCOM: Bool = false |
| 60 | + public var locale: String? |
| 61 | + |
| 62 | + public init() {} |
| 63 | + } |
| 64 | + |
| 65 | + public struct AllDomainsListItem { |
| 66 | + |
| 67 | + public enum StatusType: String { |
| 68 | + case success |
| 69 | + case premium |
| 70 | + case neutral |
| 71 | + case warning |
| 72 | + case alert |
| 73 | + case error |
| 74 | + } |
| 75 | + |
| 76 | + public struct Status { |
| 77 | + |
| 78 | + public let value: String |
| 79 | + public let type: StatusType |
| 80 | + |
| 81 | + public init(value: String, type: StatusType) { |
| 82 | + self.value = value |
| 83 | + self.type = type |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public let domain: String |
| 88 | + public let blogId: Int |
| 89 | + public let blogName: String |
| 90 | + public let type: DomainType |
| 91 | + public let isDomainOnlySite: Bool |
| 92 | + public let isWpcomStagingDomain: Bool |
| 93 | + public let hasRegistration: Bool |
| 94 | + public let registrationDate: Date? |
| 95 | + public let expiryDate: Date? |
| 96 | + public let wpcomDomain: Bool |
| 97 | + public let currentUserIsOwner: Bool? |
| 98 | + public let siteSlug: String |
| 99 | + public let status: Status? |
| 100 | + } |
| 101 | + |
| 102 | + // MARK: - Private Types |
| 103 | + |
| 104 | + private struct AllDomainsEndpointResponse: Decodable { |
| 105 | + let domains: [AllDomainsListItem] |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// MARK: - Encoding / Decoding |
| 110 | + |
| 111 | +extension DomainsServiceRemote.AllDomainsEndpointParams: Encodable { |
| 112 | + |
| 113 | + enum CodingKeys: String, CodingKey { |
| 114 | + case resolveStatus = "resolve_status" |
| 115 | + case locale |
| 116 | + case noWPCOM = "no_wpcom" |
| 117 | + } |
| 118 | + |
| 119 | + public func encode(to encoder: Encoder) throws { |
| 120 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 121 | + try container.encode("\(resolveStatus)", forKey: .resolveStatus) |
| 122 | + try container.encode("\(noWPCOM)", forKey: .noWPCOM) |
| 123 | + try container.encodeIfPresent(locale, forKey: .locale) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +extension DomainsServiceRemote.AllDomainsListItem.StatusType: Decodable { |
| 128 | +} |
| 129 | + |
| 130 | +extension DomainsServiceRemote.AllDomainsListItem.Status: Decodable { |
| 131 | + enum CodingKeys: String, CodingKey { |
| 132 | + case value = "status" |
| 133 | + case type = "status_type" |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +extension DomainsServiceRemote.AllDomainsListItem: Decodable { |
| 138 | + |
| 139 | + enum CodingKeys: String, CodingKey { |
| 140 | + case domain |
| 141 | + case blogId = "blog_id" |
| 142 | + case blogName = "blog_name" |
| 143 | + case type |
| 144 | + case isDomainOnlySite = "is_domain_only_site" |
| 145 | + case isWpcomStagingDomain = "is_wpcom_staging_domain" |
| 146 | + case hasRegistration = "has_registration" |
| 147 | + case registrationDate = "registration_date" |
| 148 | + case expiryDate = "expiry" |
| 149 | + case wpcomDomain = "wpcom_domain" |
| 150 | + case currentUserIsOwner = "current_user_is_owner" |
| 151 | + case siteSlug = "site_slug" |
| 152 | + case status = "domain_status" |
| 153 | + } |
| 154 | + |
| 155 | + public init(from decoder: Decoder) throws { |
| 156 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 157 | + self.domain = try container.decode(String.self, forKey: .domain) |
| 158 | + self.blogId = try container.decode(Int.self, forKey: .blogId) |
| 159 | + self.blogName = try container.decode(String.self, forKey: .blogName) |
| 160 | + self.isDomainOnlySite = try container.decode(Bool.self, forKey: .isDomainOnlySite) |
| 161 | + self.isWpcomStagingDomain = try container.decode(Bool.self, forKey: .isWpcomStagingDomain) |
| 162 | + self.hasRegistration = try container.decode(Bool.self, forKey: .hasRegistration) |
| 163 | + self.wpcomDomain = try container.decode(Bool.self, forKey: .wpcomDomain) |
| 164 | + self.currentUserIsOwner = try container.decode(Bool?.self, forKey: .currentUserIsOwner) |
| 165 | + self.siteSlug = try container.decode(String.self, forKey: .siteSlug) |
| 166 | + self.registrationDate = try { |
| 167 | + if let timestamp = try? container.decodeIfPresent(String.self, forKey: .registrationDate), !timestamp.isEmpty { |
| 168 | + return try container.decode(Date.self, forKey: .registrationDate) |
| 169 | + } |
| 170 | + return nil |
| 171 | + }() |
| 172 | + self.expiryDate = try { |
| 173 | + if let timestamp = try? container.decodeIfPresent(String.self, forKey: .expiryDate), !timestamp.isEmpty { |
| 174 | + return try container.decode(Date.self, forKey: .expiryDate) |
| 175 | + } |
| 176 | + return nil |
| 177 | + }() |
| 178 | + let type: String = try container.decode(String.self, forKey: .type) |
| 179 | + self.type = .init(type: type, wpComDomain: wpcomDomain, hasRegistration: hasRegistration) |
| 180 | + self.status = try container.decodeIfPresent(Status.self, forKey: .status) |
| 181 | + } |
| 182 | +} |
0 commit comments