|
| 1 | + |
| 2 | +import Foundation |
| 3 | + |
| 4 | +// MARK: - SiteCreationRequest |
| 5 | + |
| 6 | +/// This value type is intended to express a site creation request. |
| 7 | +/// |
| 8 | +public struct SiteCreationRequest: Encodable { |
| 9 | + let segmentIdentifier: Int64 |
| 10 | + let verticalIdentifier: String? |
| 11 | + let title: String |
| 12 | + let tagline: String? |
| 13 | + let siteURLString: String |
| 14 | + let isPublic: Bool |
| 15 | + let languageIdentifier: String |
| 16 | + let shouldValidate: Bool |
| 17 | + let clientIdentifier: String |
| 18 | + let clientSecret: String |
| 19 | + |
| 20 | + public func encode(to encoder: Encoder) throws { |
| 21 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 22 | + |
| 23 | + try container.encode(clientIdentifier, forKey: .clientIdentifier) |
| 24 | + try container.encode(clientSecret, forKey: .clientSecret) |
| 25 | + try container.encode(languageIdentifier, forKey: .languageIdentifier) |
| 26 | + try container.encode(shouldValidate, forKey: .shouldValidate) |
| 27 | + try container.encode(siteURLString, forKey: .siteURLString) |
| 28 | + try container.encode(title, forKey: .title) |
| 29 | + |
| 30 | + let publicValue = isPublic ? 1 : 0 |
| 31 | + try container.encode(publicValue, forKey: .isPublic) |
| 32 | + |
| 33 | + let siteInfo: SiteInformation? |
| 34 | + if let tagline = tagline { |
| 35 | + siteInfo = SiteInformation(tagline: tagline) |
| 36 | + } else { |
| 37 | + siteInfo = nil |
| 38 | + } |
| 39 | + let options = SiteCreationOptions(segmentIdentifier: segmentIdentifier, verticalIdentifier: verticalIdentifier, siteInformation: siteInfo) |
| 40 | + |
| 41 | + try container.encode(options, forKey: .options) |
| 42 | + } |
| 43 | + |
| 44 | + private enum CodingKeys: String, CodingKey { |
| 45 | + case clientIdentifier = "client_id" |
| 46 | + case clientSecret = "client_secret" |
| 47 | + case languageIdentifier = "lang_id" |
| 48 | + case isPublic = "public" |
| 49 | + case shouldValidate = "validate" |
| 50 | + case siteURLString = "blog_name" |
| 51 | + case title = "blog_title" |
| 52 | + case options = "options" |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +private struct SiteCreationOptions: Encodable { |
| 57 | + let segmentIdentifier: Int64 |
| 58 | + let verticalIdentifier: String? |
| 59 | + let siteInformation: SiteInformation? |
| 60 | + |
| 61 | + enum CodingKeys: String, CodingKey { |
| 62 | + case segmentIdentifier = "site_segment" |
| 63 | + case verticalIdentifier = "site_vertical" |
| 64 | + case siteInformation = "site_information" |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +private struct SiteInformation: Encodable { |
| 69 | + let tagline: String? |
| 70 | + |
| 71 | + enum CodingKeys: String, CodingKey { |
| 72 | + case tagline = "site_tagline" |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// MARK: - SiteCreationResponse |
| 77 | + |
| 78 | +/// This value type is intended to express a site creation response. |
| 79 | +/// |
| 80 | +public struct SiteCreationResponse: Decodable { |
| 81 | + struct CreatedSite: Decodable { |
| 82 | + let identifier: String |
| 83 | + let title: String |
| 84 | + let urlString: String |
| 85 | + let xmlrpcString: String |
| 86 | + |
| 87 | + enum CodingKeys: String, CodingKey { |
| 88 | + case identifier = "blogid" |
| 89 | + case title = "blogname" |
| 90 | + case urlString = "url" |
| 91 | + case xmlrpcString = "xmlrpc" |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + let createdSite: CreatedSite |
| 96 | + let success: Bool |
| 97 | + |
| 98 | + enum CodingKeys: String, CodingKey { |
| 99 | + case createdSite = "blog_details" |
| 100 | + case success |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// MARK: - WordPressComServiceRemote (Site Creation) |
| 105 | + |
| 106 | +/// Describes the errors that could arise during the process of site creation. |
| 107 | +/// |
| 108 | +/// - requestEncodingFailure: unable to encode the request parameters. |
| 109 | +/// - responseDecodingFailure: unable to decode the server response. |
| 110 | +/// - serviceFailure: the service returned an unexpected error. |
| 111 | +/// |
| 112 | +public enum SiteCreationError: Error { |
| 113 | + case requestEncodingFailure |
| 114 | + case responseDecodingFailure |
| 115 | + case serviceFailure |
| 116 | +} |
| 117 | + |
| 118 | +/// Advises the caller of results related to site creation requests. |
| 119 | +/// |
| 120 | +/// - success: the site creation request succeeded with the accompanying result. |
| 121 | +/// - failure: the site creation request failed due to the accompanying error. |
| 122 | +/// |
| 123 | +public enum SiteCreationResult { |
| 124 | + case success(SiteCreationResponse) |
| 125 | + case failure(SiteCreationError) |
| 126 | +} |
| 127 | + |
| 128 | +public typealias SiteCreationResultHandler = ((SiteCreationResult) -> ()) |
| 129 | + |
| 130 | +/// Site creation services, exclusive to WordPress.com. |
| 131 | +/// |
| 132 | +public extension WordPressComServiceRemote { |
| 133 | + |
| 134 | + /// Initiates a request to create a new WPCOM site. |
| 135 | + /// |
| 136 | + /// - Parameters: |
| 137 | + /// - request: the value object with which to compose the request. |
| 138 | + /// - completion: a closure including the result of the site creation attempt. |
| 139 | + /// |
| 140 | + func createWPComSite(request: SiteCreationRequest, completion: @escaping SiteCreationResultHandler) { |
| 141 | + |
| 142 | + let endpoint = "sites/new" |
| 143 | + let path = self.path(forEndpoint: endpoint, withVersion: ._1_1) |
| 144 | + |
| 145 | + let requestParameters: [String : AnyObject] |
| 146 | + do { |
| 147 | + requestParameters = try encodeRequestParameters(request: request) |
| 148 | + } catch { |
| 149 | + DDLogError("Failed to encode \(SiteCreationRequest.self) : \(error)") |
| 150 | + |
| 151 | + completion(.failure(SiteCreationError.requestEncodingFailure)) |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + wordPressComRestApi.POST( |
| 156 | + path, |
| 157 | + parameters: requestParameters, |
| 158 | + success: { [weak self] responseObject, httpResponse in |
| 159 | + DDLogInfo("\(responseObject) | \(String(describing: httpResponse))") |
| 160 | + |
| 161 | + guard let self = self else { |
| 162 | + return |
| 163 | + } |
| 164 | + |
| 165 | + do { |
| 166 | + let response = try self.decodeResponse(responseObject: responseObject) |
| 167 | + completion(.success(response)) |
| 168 | + } catch { |
| 169 | + DDLogError("Failed to decode \(SiteCreationResponse.self) : \(error.localizedDescription)") |
| 170 | + completion(.failure(SiteCreationError.responseDecodingFailure)) |
| 171 | + } |
| 172 | + }, |
| 173 | + failure: { error, httpResponse in |
| 174 | + DDLogError("\(error) | \(String(describing: httpResponse))") |
| 175 | + completion(.failure(SiteCreationError.serviceFailure)) |
| 176 | + }) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +// MARK: - Serialization support |
| 181 | + |
| 182 | +private extension WordPressComServiceRemote { |
| 183 | + |
| 184 | + func encodeRequestParameters(request: SiteCreationRequest) throws -> [String : AnyObject] { |
| 185 | + |
| 186 | + let encoder = JSONEncoder() |
| 187 | + |
| 188 | + let jsonData = try encoder.encode(request) |
| 189 | + let serializedJSON = try JSONSerialization.jsonObject(with: jsonData, options: []) |
| 190 | + |
| 191 | + let requestParameters: [String : AnyObject] |
| 192 | + if let jsonDictionary = serializedJSON as? [String : AnyObject] { |
| 193 | + requestParameters = jsonDictionary |
| 194 | + } else { |
| 195 | + requestParameters = [:] |
| 196 | + } |
| 197 | + |
| 198 | + return requestParameters |
| 199 | + } |
| 200 | + |
| 201 | + func decodeResponse(responseObject: AnyObject) throws -> SiteCreationResponse { |
| 202 | + |
| 203 | + let decoder = JSONDecoder() |
| 204 | + |
| 205 | + let data = try JSONSerialization.data(withJSONObject: responseObject, options: []) |
| 206 | + let response = try decoder.decode(SiteCreationResponse.self, from: data) |
| 207 | + |
| 208 | + return response |
| 209 | + } |
| 210 | +} |
0 commit comments