Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use URLSession to send WP.com REST API requests #720

Merged
merged 9 commits into from
Feb 6, 2024

Conversation

crazytonyli
Copy link
Contributor

@crazytonyli crazytonyli commented Feb 5, 2024

Description

This PR updates the WP.com REST API client to send HTTP requests using URLSession, when useURLSession is set to true (it's false by default).

Note

I'd recommend reviewing this PR commit-by-commit.

Testing Details

The unit test suite passes with useURLSession set to ture. I had to updated one test case to make it pass, because the new URLSession API does not return AFError.

In theory, all the new code in this PR are dead code at the moment. I will update the apps to enable useURLSession as in-development feature flag, and we can see how the new implementation work out in the app.

wordpress-mobile/WordPress-iOS#22540 can be used to get an app build which enables useURLSession by default.


  • 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 force-pushed the wordpress-com-rest-api-error-refactor branch 2 times, most recently from 3dd50a3 to e30ba27 Compare February 5, 2024 01:05
@crazytonyli crazytonyli marked this pull request as ready for review February 5, 2024 01:10
Copy link
Contributor

@mokagio mokagio left a comment

Choose a reason for hiding this comment

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

Only a couple of nitpicks.

@@ -612,6 +639,38 @@ open class WordPressComRestApi: NSObject {
}
}

public func upload(
_ URLString: String,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick. Value names should start with lowercase letters.

Suggested change
_ URLString: String,
_ urlString: String,

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 have updated this part in a3c19a9. which probably made it worse, in terms of the name... I used URLString because that's the term used throughout this class...

completion(
result
.map { ($0.body, $0.response) }
.mapError { error -> Error in error }
Copy link
Contributor

Choose a reason for hiding this comment

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

It wasn't obvious to me why this mapError was necessary.

What to you think of adding a line such as

Suggested change
.mapError { error -> Error in error }
// The completion expects a result with any Error.
// We need to "downcast" the WordPressComRestApiEndpointError error in the APIResult.
.mapError { error -> Error in error }

Also, using $0 would work, too. But, having the explicit function signature with Error might be clearer for the reader in this unusual scenario.

@crazytonyli crazytonyli enabled auto-merge February 6, 2024 20:20
@crazytonyli crazytonyli merged commit 8ae7850 into trunk Feb 6, 2024
6 checks passed
@crazytonyli crazytonyli deleted the wordpress-com-rest-api-error-refactor branch February 6, 2024 20:25
@crazytonyli crazytonyli mentioned this pull request Mar 8, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants