-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Jetpack REST connection: connect site and user #22126
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
Merged
Merged
Changes from all commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
da347ce
Skip wp.com login if access token exists
nbradbury c95fa12
First pass at installing Jetpack
nbradbury f9dba6f
Second pass at installing Jetpack
nbradbury 03f85e7
First pass at handling error response
nbradbury 74f5f45
Return a pair for installJetpack and restored launch wpcom login from…
nbradbury a298f39
Added InstallJetpackResult to make it clear the second item in the pa…
nbradbury d15a2bb
Added InstallJetpackResult to make it clear the second item in the pa…
nbradbury 8d426ca
refresh the site before starting jetpack install
nbradbury ae6eaa3
Fixed detekt warnings, fetch site directly
nbradbury f72cc1f
Added/updated comments
nbradbury b2f520b
Merge branch 'trunk' of https://github.com/wordpress-mobile/WordPress…
nbradbury fe56a2e
Handle inactive Jetpack install, refresh site in init
nbradbury 1ee7396
Tweaked comments
nbradbury 1cc674f
Restored check for access token
nbradbury 2c1a58a
Check if we need to install or simply activate the jp plugin
nbradbury 1237bc9
Simplified JetpackInstaller to always install the plugin
nbradbury c171b36
Increased timeout
nbradbury d1b59a1
Fixed Detekt warnings
nbradbury 974941a
Merge branch 'trunk' into feature/jetpack-connect-install
nbradbury b5b7cbc
Simplified JetpackInstaller.kt
nbradbury a5ac0b4
Simplified JetpackInstaller.kt, p2
nbradbury 5e4aa19
Added log message
nbradbury fe48169
Get plugin status before activating
nbradbury 6af0b6c
Use correct names for slugs
nbradbury 6354253
Removed iOS commented code
nbradbury 8c4dca1
Reduced timeout from 60 to 45 seconds
nbradbury e19e769
Simplified installer
nbradbury ca4efc5
Code cleanup
nbradbury 2a163dd
Removed refreshSite
nbradbury 89ac854
Merge branch 'trunk' into feature/jetpack-connect-install
nbradbury d705d4c
Pass WpApiClient as a parameter
nbradbury f7f49ff
Added missing space
nbradbury 7d7d38b
Use a Result<PluginStatus> for installJetpack
nbradbury 4baf46d
Add a delay to wp.com login if already logged in
nbradbury bbccb48
Removed unnecessary launch
nbradbury b601a0c
Suppress TooGenericExceptionCaught
nbradbury b59b354
Merge branch 'trunk' into feature/jetpack-connect-install
nbradbury b6b0eb8
Moved JetpackConnectionHelper to its own helper class
nbradbury 2f25831
Connect Jetpack to site
nbradbury 61757f3
Handle failure
nbradbury 3f2aa5a
Added fun to connect user
nbradbury dc18965
Added error handling to connect user
nbradbury 9ee69c6
Updated error handling to connect user
nbradbury 615f0c9
Remove extra message from all errors except ErrorType.Unknown
nbradbury 860cd15
Throw exception on invalid authentication
nbradbury 078494b
Simplified JetpackConnectionHelper.kt
nbradbury 59676e7
Simplified JetpackConnector
nbradbury c94134f
Code tweaks
nbradbury eaa9003
Further simplified JetpackConnector.kt
nbradbury 7a2dcd1
Modified text & icon for user connection step
nbradbury b28a408
Merge branch 'trunk' into feature/jetpack-connect-site-and-user
nbradbury cdbe695
Clarified that REST credentials are required
nbradbury 086d881
Corrected ConnectUser step completion
nbradbury ff4f1fb
Corrected ConnectUser step failure
nbradbury 35e77b0
Removed unnecessary job?.cancel
nbradbury 6c03ee4
Merge branch 'trunk' of https://github.com/wordpress-mobile/WordPress…
nbradbury 328139a
Minor code cleanup
nbradbury 6185c2d
Merge branch 'trunk' into feature/jetpack-connect-site-and-user
nbradbury 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
72 changes: 72 additions & 0 deletions
72
...s/src/main/java/org/wordpress/android/ui/jetpackrestconnection/JetpackConnectionHelper.kt
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.wordpress.android.ui.jetpackrestconnection | ||
|
||
import org.wordpress.android.fluxc.model.SiteModel | ||
import org.wordpress.android.fluxc.utils.AppLogWrapper | ||
import org.wordpress.android.util.AppLog | ||
import rs.wordpress.api.kotlin.WpApiClient | ||
import rs.wordpress.api.kotlin.WpRequestExecutor | ||
import uniffi.wp_api.JetpackConnectionClient | ||
import uniffi.wp_api.ParsedUrl | ||
import uniffi.wp_api.WpApiClientDelegate | ||
import uniffi.wp_api.WpApiMiddlewarePipeline | ||
import uniffi.wp_api.WpAppNotifier | ||
import uniffi.wp_api.WpAuthenticationProvider | ||
import java.net.URL | ||
import javax.inject.Inject | ||
|
||
class JetpackConnectionHelper @Inject constructor( | ||
private val appLogWrapper: AppLogWrapper | ||
) { | ||
fun initWpApiClient(site: SiteModel): WpApiClient { | ||
requireRestCredentials(site) | ||
return WpApiClient( | ||
wpOrgSiteApiRootUrl = URL(resolveRestApiUrl(site)), | ||
authProvider = createRestAuthProvider(site) | ||
) | ||
} | ||
|
||
fun initJetpackConnectionClient(site: SiteModel): JetpackConnectionClient { | ||
requireRestCredentials(site) | ||
|
||
val delegate = WpApiClientDelegate( | ||
authProvider = createRestAuthProvider(site), | ||
requestExecutor = WpRequestExecutor(), | ||
middlewarePipeline = WpApiMiddlewarePipeline(emptyList()), | ||
appNotifier = InvalidAuthNotifier() | ||
) | ||
|
||
return JetpackConnectionClient( | ||
apiRootUrl = ParsedUrl.parse(resolveRestApiUrl(site)), | ||
delegate = delegate | ||
) | ||
} | ||
|
||
private fun createRestAuthProvider(site: SiteModel) = | ||
WpAuthenticationProvider.staticWithUsernameAndPassword( | ||
site.apiRestUsernamePlain!!, | ||
site.apiRestPasswordPlain!! | ||
) | ||
|
||
private fun requireRestCredentials(site: SiteModel) { | ||
require(!site.apiRestUsernamePlain.isNullOrBlank()) { | ||
"API username is required" | ||
} | ||
require(!site.apiRestPasswordPlain.isNullOrBlank()) { | ||
"API password is required" | ||
} | ||
} | ||
|
||
private fun resolveRestApiUrl(site: SiteModel) = | ||
site.wpApiRestUrl ?: "${site.url}/wp-json" | ||
|
||
private inner class InvalidAuthNotifier : WpAppNotifier { | ||
override suspend fun requestedWithInvalidAuthentication() { | ||
appLogWrapper.d(AppLog.T.API, "$TAG: requestedWithInvalidAuthentication") | ||
throw IllegalArgumentException("Invalid credentials") | ||
} | ||
} | ||
|
||
companion object { | ||
private const val TAG = "JetpackConnectionHelper" | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
WordPress/src/main/java/org/wordpress/android/ui/jetpackrestconnection/JetpackConnector.kt
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.wordpress.android.ui.jetpackrestconnection | ||
|
||
import org.wordpress.android.fluxc.model.SiteModel | ||
import uniffi.wp_api.WpAuthentication | ||
import uniffi.wp_api.WpComSiteId | ||
import javax.inject.Inject | ||
|
||
class JetpackConnector @Inject constructor( | ||
private val jetpackConnectionHelper: JetpackConnectionHelper | ||
) { | ||
/** | ||
* Connects the Jetpack site to WordPress.com and returns the site ID | ||
*/ | ||
suspend fun connectSite(site: SiteModel): Result<WpComSiteId> = runCatching { | ||
val client = jetpackConnectionHelper.initJetpackConnectionClient(site) | ||
val wpComSiteId = client.connectSite(CONNECT_FROM) | ||
requireValidSiteId(wpComSiteId) | ||
} | ||
|
||
/** | ||
* Connects the Jetpack user to WordPress.com and returns the site ID | ||
*/ | ||
suspend fun connectUser(site: SiteModel, accessToken: String): Result<WpComSiteId> = runCatching { | ||
val client = jetpackConnectionHelper.initJetpackConnectionClient(site) | ||
val wpComAuthentication = WpAuthentication.Bearer(token = accessToken) | ||
val wpComSiteId = client.connectUser( | ||
wpComAuthentication = wpComAuthentication, | ||
from = CONNECT_FROM | ||
) | ||
requireValidSiteId(wpComSiteId) | ||
} | ||
|
||
private fun requireValidSiteId(wpComSiteId: WpComSiteId): WpComSiteId { | ||
require(wpComSiteId > 0UL) { "Jetpack connection failed, no site ID returned" } | ||
return wpComSiteId | ||
} | ||
|
||
companion object { | ||
const val CONNECT_FROM = "jetpack-android-app" | ||
} | ||
} |
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
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
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
Oops, something went wrong.
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.
⛏️ I know I have already mentioned this, but what about keeping all the
WpApiClient
creations inside theWpApiClientProvider
so the maintenante is clearer?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.
Yep, this is already a task on the Linear project. I'll DM you the project link.