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

Use URLSession in WordPressOAuthClient - #660

Merged
crazytonyli merged 8 commits into
trunkfrom
url-session-in-oauth-client
Jan 11, 2024
Merged

Use URLSession in WordPressOAuthClient#660
crazytonyli merged 8 commits into
trunkfrom
url-session-in-oauth-client

Conversation

@crazytonyli

@crazytonyli crazytonyli commented Dec 13, 2023

Copy link
Copy Markdown
Contributor

Description

This PR replaces some usages of Alamofire in WordPressOAuthClient, with URLSession. There will be more similar PRs later for other usages.

I noticed there are many Alamofire.SessionManager instances are used. I don't understand why, but at the same time I'm too afraid to change it. For now, I'll follow the existing pattern and continue using many URLSession instances too.

Testing Details

The refactored features are: login with WP.com username password and request code via SMS. You can use wordpress-mobile/WordPress-iOS#22215 to test this change. Here are some test scenarios I can think of:

  1. Login with username and password. The app presents 2FA code screen if the account enables 2FA.
  2. The app shows appropriate error messages if password is incorrect, or 2fa code is incorrect.
  3. Request one time code via SMS works.

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

@crazytonyli
crazytonyli requested a review from mokagio December 13, 2023 03:01

@mokagio mokagio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just a few comments... I didn't finish the review but wanted to avoid losing these.


func url(base: String) -> URL {
return URL(string: self.rawValue, relativeTo: URL(string: base))!
func url(base: URL) -> URL {

@mokagio mokagio Dec 14, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 for requiring base to be a URL and pushing the responsibility to create good URLs to the callers.

IMHO, in the ideal world, we'd only have one point in the whole app / library where we go from String to URL. The rest of the code deals with "valid" instances, without having to worry about validation.


private let oauth2SessionManager: SessionManager = {
return WordPressComOAuthClient.sessionManager()
// Question: Is it necessary to use these many URLSession instances?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What do you think of making this a TODO or FIXME just so tooling can pick it up?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Or... have you discovered an answer since writing that comment with the extra work you've done on this library?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't use TODO or FIXME because I'm not sure it needs a change.

But the pattern sure raises a question, at least to me. Because it's pretty rare to see code that uses different URLSession instances to send requests to endpoints that are in the same area (in this case, authentication). But considering the "endpoints" here are all php pages, rather than the common REST style API endpoints, maybe it wants to avoid sharing cookies among those API calls?

self.wordPressComBaseUrl = wordPressComBaseUrl
self.wordPressComApiBaseUrl = wordPressComApiBaseUrl
self.wordPressComBaseUrl = URL(string: wordPressComBaseUrl)!
self.wordPressComApiBaseUrl = URL(string: wordPressComApiBaseUrl)!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If we plan on more breaking changes, we could also slot one in to require those String parameters to be URLs.

@crazytonyli

Copy link
Copy Markdown
Contributor Author

Need to rebase this PR after #668 is merged.

Comment on lines +173 to 187
public enum AuthenticationResult {
case authenticated(token: String)
case needsMultiFactor(userID: Int, nonceInfo: SocialLogin2FANonceInfo)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice


private let oauth2SessionManager: SessionManager = {
return WordPressComOAuthClient.sessionManager()
// Question: Is it necessary to use these many URLSession instances?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Or... have you discovered an answer since writing that comment with the extra work you've done on this library?

})
return .needsMultiFactor(userID: userID, nonceInfo: nonceInfo)
},
failure: Self.processError(_:)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nitpick: I find calling Self. clunky, but that might just be me.

Just for fun, we could

  • Make processError a private free function; or
  • It could be a method on HTTPAPIResponse<Data>; or
  • An init? on AuthenticationFailure

But... 🤷‍♂️ This is internal code that's only used here.

@crazytonyli crazytonyli Jan 11, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call! I've changed it to AuthenticationFailure initialiser in a243bb8 a83f361

Comment on lines +738 to +752
func tokenRequestBuilder() -> HTTPRequestBuilder {
HTTPRequestBuilder(url: wordPressComApiBaseUrl)
.method(.post)
.append(path: "/oauth2/token")
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Love to see these builders in action and to DRY things.

@crazytonyli
crazytonyli force-pushed the url-session-in-oauth-client branch from a243bb8 to a83f361 Compare January 11, 2024 20:54
@crazytonyli
crazytonyli enabled auto-merge January 11, 2024 20:55
@crazytonyli
crazytonyli merged commit 610a94d into trunk Jan 11, 2024
@crazytonyli
crazytonyli deleted the url-session-in-oauth-client branch January 11, 2024 20:57
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