-
Notifications
You must be signed in to change notification settings - Fork 16
Add apiURL parameter for changing WP.com API URL
#731
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(siteID: _, bearerToken: token, _) = site { | ||||||
|
||||||
| if case let Site.dotCom(siteID: _, bearerToken: token, _) = site { | |
| if case let Site.dotCom(_, bearerToken: token, _) = site { |
Site note: I looked for a SwiftLint rule for this but did not find one that would have picked that up. At first, I browsed the rules docs. Then, I tried with
cat WordPressKit/WordPressOrgRestApi.swift \
| ./Pods/SwiftLint/swiftlint lint --quiet --enable-all-rules --use-stdin \
| sort -v
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.
| 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 {} | ||

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. 😄