This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Couldn't load subscription status.
- Fork 16
Add apiURL parameter for changing WP.com API URL
#731
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,7 @@ public final class WordPressOrgRestApi: NSObject { | |
| } | ||
|
|
||
| enum Site { | ||
| case dotCom(siteID: UInt64, bearerToken: String) | ||
| case dotCom(siteID: UInt64, bearerToken: String, apiURL: URL) | ||
| case selfHosted(apiURL: URL, credential: SelfHostedSiteCredential) | ||
| } | ||
|
|
||
|
|
@@ -49,8 +49,8 @@ public final class WordPressOrgRestApi: NSObject { | |
|
|
||
| var selfHostedSiteNonce: String? | ||
|
|
||
| public convenience init(dotComSiteID: UInt64, bearerToken: String, userAgent: String? = nil) { | ||
| self.init(site: .dotCom(siteID: dotComSiteID, bearerToken: bearerToken), userAgent: userAgent) | ||
| 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) { | ||
|
|
@@ -68,7 +68,7 @@ public final class WordPressOrgRestApi: NSObject { | |
| if let userAgent { | ||
| additionalHeaders["User-Agent"] = userAgent | ||
| } | ||
| if case let Site.dotCom(siteID: _, bearerToken: token) = site { | ||
| if case let Site.dotCom(_, token, _) = site { | ||
| additionalHeaders["Authorization"] = "Bearer \(token)" | ||
| } | ||
|
|
||
|
|
@@ -203,8 +203,8 @@ public final class WordPressOrgRestApi: NSObject { | |
| private extension WordPressOrgRestApi { | ||
| func apiBaseURL() -> URL { | ||
| switch site { | ||
| case .dotCom: | ||
| return URL(string: "https://public-api.wordpress.com")! | ||
| case let .dotCom(_, _, apiURL): | ||
| return apiURL | ||
|
Comment on lines
-206
to
+207
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| case let .selfHosted(apiURL, _): | ||
| return apiURL | ||
| } | ||
|
|
@@ -252,7 +252,7 @@ private extension HTTPRequestBuilder { | |
| } | ||
|
|
||
| switch site { | ||
| case let .dotCom(siteID, _): | ||
| 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)") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,6 +103,18 @@ class WordPressOrgRestApiTests: XCTestCase { | |
| let api = WordPressOrgRestApi(site: .dotCom(siteID: 1001, bearerToken: "fakeToken")) | ||
| let _ = try await api.get(path: "/wp-block-editor/v1/settings", type: AnyResponse.self).get() | ||
| } | ||
|
|
||
| func testSettingWPComAPIURL() async { | ||
| var request: URLRequest? | ||
| stub(condition: { _ in true }, response: { | ||
| request = $0 | ||
| return HTTPStubsResponse(error: URLError(.networkConnectionLost)) | ||
| }) | ||
|
|
||
| let api = WordPressOrgRestApi(dotComSiteID: 1001, bearerToken: "token", apiURL: URL(string: "http://localhost:8000")!) | ||
| let _ = await api.get(path: "/wp/v2/hello", type: AnyResponse.self) | ||
| XCTAssertEqual(request?.url?.absoluteString, "http://localhost:8000/wp/v2/sites/1001/hello") | ||
| } | ||
| } | ||
|
|
||
| extension WordPressOrgRestApi { | ||
|
|
@@ -114,4 +126,10 @@ extension WordPressOrgRestApi { | |
| } | ||
| } | ||
|
|
||
| extension WordPressOrgRestApi.Site { | ||
| static func dotCom(siteID: UInt64, bearerToken: String) -> Self { | ||
| .dotCom(siteID: siteID, bearerToken: bearerToken, apiURL: WordPressComRestApi.apiBaseURL) | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for future readers: This method looks unused in the diff, but it's actually a way to bridge the new implementation, which expects an See for example line 103 in this file. |
||
|
|
||
| private struct AnyResponse: Decodable {} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: What do you think of making
apiURLthe first associated value, so that it's in line with theselfHostedcase?One argument against doing so would be that
siteIDis more important when it comes to identifying a .com site, so it belongs in first position. I'd be fine with that. This is just a style nitpick / question that I'm not sure on.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd agree with that argument, considering 'apiURL' is an optional parameter in the initialiser. 😄