Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 31798ce

Browse files
committed
Add apiURL parameter to changing WP.com API URL
1 parent 07d9070 commit 31798ce

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

WordPressKit/WordPressOrgRestApi.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public final class WordPressOrgRestApi: NSObject {
4040
}
4141

4242
enum Site {
43-
case dotCom(siteID: UInt64, bearerToken: String)
43+
case dotCom(siteID: UInt64, bearerToken: String, apiURL: URL)
4444
case selfHosted(apiURL: URL, credential: SelfHostedSiteCredential)
4545
}
4646

@@ -49,8 +49,8 @@ public final class WordPressOrgRestApi: NSObject {
4949

5050
var selfHostedSiteNonce: String?
5151

52-
public convenience init(dotComSiteID: UInt64, bearerToken: String, userAgent: String? = nil) {
53-
self.init(site: .dotCom(siteID: dotComSiteID, bearerToken: bearerToken), userAgent: userAgent)
52+
public convenience init(dotComSiteID: UInt64, bearerToken: String, userAgent: String? = nil, apiURL: URL = WordPressComRestApi.apiBaseURL) {
53+
self.init(site: .dotCom(siteID: dotComSiteID, bearerToken: bearerToken, apiURL: apiURL), userAgent: userAgent)
5454
}
5555

5656
public convenience init(selfHostedSiteWPJSONURL apiURL: URL, credential: SelfHostedSiteCredential, userAgent: String? = nil) {
@@ -68,7 +68,7 @@ public final class WordPressOrgRestApi: NSObject {
6868
if let userAgent {
6969
additionalHeaders["User-Agent"] = userAgent
7070
}
71-
if case let Site.dotCom(siteID: _, bearerToken: token) = site {
71+
if case let Site.dotCom(siteID: _, bearerToken: token, _) = site {
7272
additionalHeaders["Authorization"] = "Bearer \(token)"
7373
}
7474

@@ -203,8 +203,8 @@ public final class WordPressOrgRestApi: NSObject {
203203
private extension WordPressOrgRestApi {
204204
func apiBaseURL() -> URL {
205205
switch site {
206-
case .dotCom:
207-
return URL(string: "https://public-api.wordpress.com")!
206+
case let .dotCom(_, _, apiURL):
207+
return apiURL
208208
case let .selfHosted(apiURL, _):
209209
return apiURL
210210
}
@@ -252,7 +252,7 @@ private extension HTTPRequestBuilder {
252252
}
253253

254254
switch site {
255-
case let .dotCom(siteID, _):
255+
case let .dotCom(siteID, _, _):
256256
// Currently only the following namespaces are supported. When adding more supported namespaces, remember to
257257
// update the "path adapter" code below for the REST API in WP.COM.
258258
assert(route.hasPrefix("/wp/v2") || route.hasPrefix("/wp-block-editor/v1"), "Unsupported .org REST API route: \(route)")

WordPressKitTests/WordPressOrgRestApiTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,10 @@ extension WordPressOrgRestApi {
114114
}
115115
}
116116

117+
extension WordPressOrgRestApi.Site {
118+
static func dotCom(siteID: UInt64, bearerToken: String) -> Self {
119+
.dotCom(siteID: siteID, bearerToken: bearerToken, apiURL: WordPressComRestApi.apiBaseURL)
120+
}
121+
}
122+
117123
private struct AnyResponse: Decodable {}

0 commit comments

Comments
 (0)