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

Conversation

crazytonyli
Copy link
Contributor

Description

This PR brings back support of mocking WP.com .org REST API. See wordpress-mobile/WordPress-iOS#22612 (review) for context in the WordPress app.

Testing Details

I have added one unit test. Let me know if you think there are more can be added.


  • Please check here if your pull request includes additional test coverage.
  • I have considered if this change warrants release notes and have added them to the appropriate section in the CHANGELOG.md if necessary.

Comment on lines -43 to 44
enum Site {
case dotCom(siteID: UInt64, bearerToken: String)
case dotCom(siteID: UInt64, bearerToken: String, apiURL: URL)
case selfHosted(apiURL: URL, credential: SelfHostedSiteCredential)
Copy link
Contributor

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 apiURL the first associated value, so that it's in line with the selfHosted case?

One argument against doing so would be that siteID is 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.

Copy link
Contributor Author

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

additionalHeaders["User-Agent"] = userAgent
}
if case let Site.dotCom(siteID: _, bearerToken: token) = site {
if case let Site.dotCom(siteID: _, bearerToken: token, _) = site {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: The new _ entry made me realize that the siteID: label is unnecessary

Suggested change
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

Comment on lines -206 to +207
case .dotCom:
return URL(string: "https://public-api.wordpress.com")!
case let .dotCom(_, _, apiURL):
return apiURL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nice side effect is that now we are one step closer to having a single source of truth for what this URL should be.

image

static func dotCom(siteID: UInt64, bearerToken: String) -> Self {
.dotCom(siteID: siteID, bearerToken: bearerToken, apiURL: WordPressComRestApi.apiBaseURL)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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 apiURL parameter, with the existing code that did not have it.

See for example line 103 in this file.

@crazytonyli crazytonyli merged commit 676fb99 into trunk Feb 21, 2024
@crazytonyli crazytonyli deleted the allow-setting-api-url branch February 21, 2024 00:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants