This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathWordPressOrgRestApi.swift
More file actions
275 lines (230 loc) · 9.84 KB
/
WordPressOrgRestApi.swift
File metadata and controls
275 lines (230 loc) · 9.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import Foundation
public struct WordPressOrgRestApiError: LocalizedError, Decodable, HTTPURLResponseProviding {
public enum CodingKeys: String, CodingKey {
case code, message
}
public var code: String
public var message: String?
var response: HTTPAPIResponse<Data>?
var httpResponse: HTTPURLResponse? {
response?.response
}
public var errorDescription: String? {
return message ?? NSLocalizedString(
"wordpresskit.org-rest-api.not-found",
value: "Couldn't find your site's REST API URL. The app needs that in order to communicate with your site. Contact your host to solve this problem.",
comment: "Message to show to user when the app can't find WordPress.org REST API URL."
)
}
}
@objc
public final class WordPressOrgRestApi: NSObject {
public struct SelfHostedSiteCredential {
public let loginURL: URL
public let username: String
public let password: Secret<String>
public let adminURL: URL
public init(loginURL: URL, username: String, password: String, adminURL: URL) {
self.loginURL = loginURL
self.username = username
self.password = .init(password)
self.adminURL = adminURL
}
}
enum Site {
case dotCom(siteID: UInt64, bearerToken: String, apiURL: URL)
case selfHosted(apiURL: URL, credential: SelfHostedSiteCredential)
}
let site: Site
let urlSession: URLSession
var selfHostedSiteNonce: String?
public convenience init(dotComSiteID: UInt64, bearerToken: String, userAgent: String? = nil, apiURL: URL = WordPressComRestApi.apiBaseURL) {
self.init(site: .dotCom(siteID: dotComSiteID, bearerToken: bearerToken, apiURL: apiURL), userAgent: userAgent)
}
public convenience init(selfHostedSiteWPJSONURL apiURL: URL, credential: SelfHostedSiteCredential, userAgent: String? = nil) {
assert(apiURL.host != "public-api.wordpress.com", "Not a self-hosted site: \(apiURL)")
// Potential improvement(?): discover API URL instead. See https://developer.wordpress.org/rest-api/using-the-rest-api/discovery/
assert(apiURL.lastPathComponent == "wp-json", "Not a REST API URL: \(apiURL)")
self.init(site: .selfHosted(apiURL: apiURL, credential: credential), userAgent: userAgent)
}
init(site: Site, userAgent: String? = nil) {
self.site = site
var additionalHeaders = [String: String]()
if let userAgent {
additionalHeaders["User-Agent"] = userAgent
}
if case let Site.dotCom(_, token, _) = site {
additionalHeaders["Authorization"] = "Bearer \(token)"
}
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = additionalHeaders
urlSession = URLSession(configuration: configuration)
}
deinit {
urlSession.finishTasksAndInvalidate()
}
@objc
public func invalidateAndCancelTasks() {
urlSession.invalidateAndCancel()
}
public func get<Success: Decodable>(
path: String,
parameters: [String: Any]? = nil,
jsonDecoder: JSONDecoder = JSONDecoder(),
type: Success.Type = Success.self
) async -> WordPressAPIResult<Success, WordPressOrgRestApiError> {
await perform(.get, path: path, parameters: parameters, jsonDecoder: jsonDecoder, type: type)
}
public func get(
path: String,
parameters: [String: Any]? = nil,
options: JSONSerialization.ReadingOptions = []
) async -> WordPressAPIResult<Any, WordPressOrgRestApiError> {
await perform(.get, path: path, parameters: parameters, options: options)
}
public func get(
path: String,
parameters: [String: Any]? = nil
) async -> WordPressAPIResult<Data, WordPressOrgRestApiError> {
await perform(.get, path: path, parameters: parameters)
}
public func post<Success: Decodable>(
path: String,
parameters: [String: Any]? = nil,
jsonDecoder: JSONDecoder = JSONDecoder(),
type: Success.Type = Success.self
) async -> WordPressAPIResult<Success, WordPressOrgRestApiError> {
await perform(.post, path: path, parameters: parameters, jsonDecoder: jsonDecoder, type: type)
}
public func post(
path: String,
parameters: [String: Any]? = nil,
options: JSONSerialization.ReadingOptions = []
) async -> WordPressAPIResult<Any, WordPressOrgRestApiError> {
await perform(.post, path: path, parameters: parameters, options: options)
}
func perform<Success: Decodable>(
_ method: HTTPRequestBuilder.Method,
path: String,
parameters: [String: Any]? = nil,
jsonDecoder: JSONDecoder = JSONDecoder(),
type: Success.Type = Success.self
) async -> WordPressAPIResult<Success, WordPressOrgRestApiError> {
await perform(method, path: path, parameters: parameters) {
try jsonDecoder.decode(type, from: $0)
}
}
func perform(
_ method: HTTPRequestBuilder.Method,
path: String,
parameters: [String: Any]? = nil,
options: JSONSerialization.ReadingOptions = []
) async -> WordPressAPIResult<Any, WordPressOrgRestApiError> {
await perform(method, path: path, parameters: parameters) {
try JSONSerialization.jsonObject(with: $0, options: options)
}
}
private func perform<Success>(
_ method: HTTPRequestBuilder.Method,
path: String,
parameters: [String: Any]? = nil,
decoder: @escaping (Data) throws -> Success
) async -> WordPressAPIResult<Success, WordPressOrgRestApiError> {
var builder = HTTPRequestBuilder(url: apiBaseURL())
.dotOrgRESTAPI(route: path, site: site)
.method(method)
if method.allowsHTTPBody {
builder = builder.body(form: parameters ?? [:])
} else {
builder = builder.query(parameters ?? [:])
}
return await perform(builder: builder)
.mapSuccess { try decoder($0.body) }
}
func perform(builder originalBuilder: HTTPRequestBuilder) async -> WordPressAPIResult<HTTPAPIResponse<Data>, WordPressOrgRestApiError> {
var builder = originalBuilder
if case .selfHosted = site, let nonce = selfHostedSiteNonce {
builder = originalBuilder.header(name: "X-WP-Nonce", value: nonce)
}
var result = await urlSession.perform(request: builder, errorType: WordPressOrgRestApiError.self)
// When a self hosted site request fails with 401, authenticate and retry the request.
if case .selfHosted = site,
case let .failure(.unacceptableStatusCode(response, _)) = result,
response.statusCode == 401,
await refreshNonce(),
let nonce = selfHostedSiteNonce {
builder = originalBuilder.header(name: "X-WP-Nonce", value: nonce)
result = await urlSession.perform(request: builder, errorType: WordPressOrgRestApiError.self)
}
return result
.mapError { error in
if case let .unacceptableStatusCode(response, body) = error {
do {
var endpointError = try JSONDecoder().decode(WordPressOrgRestApiError.self, from: body)
endpointError.response = HTTPAPIResponse(response: response, body: body)
return WordPressAPIError.endpointError(endpointError)
} catch {
return .unparsableResponse(response: response, body: body, underlyingError: error)
}
}
return error
}
}
}
// MARK: - Authentication
private extension WordPressOrgRestApi {
func apiBaseURL() -> URL {
switch site {
case let .dotCom(_, _, apiURL):
return apiURL
case let .selfHosted(apiURL, _):
return apiURL
}
}
/// Fetch REST API nonce from the site.
///
/// - Returns true if the nonce is fetched and it's different than the cached one.
func refreshNonce() async -> Bool {
guard case let .selfHosted(_, credential) = site else {
return false
}
var refreshed = false
let methods: [NonceRetrievalMethod] = [.ajaxNonceRequest, .newPostScrap]
for method in methods {
guard let nonce = await method.retrieveNonce(
username: credential.username,
password: credential.password,
loginURL: credential.loginURL,
adminURL: credential.adminURL,
using: urlSession
) else {
continue
}
refreshed = selfHostedSiteNonce != nonce
selfHostedSiteNonce = nonce
break
}
return refreshed
}
}
// MARK: - Helpers
private extension HTTPRequestBuilder {
func dotOrgRESTAPI(route aRoute: String, site: WordPressOrgRestApi.Site) -> Self {
var route = aRoute
if !route.hasPrefix("/") {
route = "/" + route
}
switch site {
case let .dotCom(siteID, _, _):
// Currently only the following namespaces are supported. When adding more supported namespaces, remember to
// update the "path adapter" code below for the REST API in WP.COM.
assert(route.hasPrefix("/wp/v2") || route.hasPrefix("/wp-block-editor/v1"), "Unsupported .org REST API route: \(route)")
route = route
.replacingOccurrences(of: "/wp/v2/", with: "/wp/v2/sites/\(siteID)/")
.replacingOccurrences(of: "/wp-block-editor/v1/", with: "/wp-block-editor/v1/sites/\(siteID)/")
case let .selfHosted(apiURL, _):
assert(apiURL.lastPathComponent == "wp-json")
}
return appendURLString(route)
}
}