-
Notifications
You must be signed in to change notification settings - Fork 38
Woo REST API: fix Woo detection for non-eligible users #2628
Changes from all 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 |
|---|---|---|
|
|
@@ -5,7 +5,6 @@ import com.google.gson.annotations.SerializedName | |
| import org.wordpress.android.fluxc.generated.endpoint.WOOCOMMERCE | ||
| import org.wordpress.android.fluxc.generated.endpoint.WPAPI | ||
| import org.wordpress.android.fluxc.model.SiteModel | ||
| import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType.NOT_FOUND | ||
| import org.wordpress.android.fluxc.network.rest.wpapi.WPAPIResponse | ||
| import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooNetwork | ||
| import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload | ||
|
|
@@ -14,6 +13,11 @@ import org.wordpress.android.fluxc.utils.toWooPayload | |
| import javax.inject.Inject | ||
|
|
||
| class WooSystemRestClient @Inject constructor(private val wooNetwork: WooNetwork) { | ||
| companion object { | ||
| private const val NOT_FOUND = 404 | ||
| private const val FORBIDDEN = 403 | ||
| } | ||
|
|
||
| suspend fun fetchInstalledPlugins(site: SiteModel): WooPayload<WCSystemPluginResponse> { | ||
| val url = WOOCOMMERCE.system_status.pathV3 | ||
|
|
||
|
|
@@ -59,10 +63,14 @@ class WooSystemRestClient @Inject constructor(private val wooNetwork: WooNetwork | |
| return when (response) { | ||
| is WPAPIResponse.Success -> WooPayload(true) | ||
| is WPAPIResponse.Error -> { | ||
| if (response.error.type == NOT_FOUND) { | ||
| WooPayload(false) | ||
| } else { | ||
| WooPayload(response.error.toWooError()) | ||
| when (response.error.volleyError?.networkResponse?.statusCode) { | ||
| NOT_FOUND -> WooPayload(false) | ||
| FORBIDDEN -> { | ||
| // If we get a 403 status code, we can infer that Woo is installed, | ||
| // the user just doesn't have permission to access the Woo API | ||
|
Contributor
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. Just to discuss: when a user doesn't get a permission to access API, is this exclusively because of their user role? Or can it be because of security plugins blocking certain users from using API, for example? If there can be multiple reasons, do they need to be handled in this PR (maybe be clarified in comments)? I'm mostly asking because this PR seems to be tied to a task specifically about checking user role.
Member
Author
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.
We don't care about the cause here, our goal of this code is just to know whether Woo is installed or not, what happens later depends on other parts of the app: for example, we handle the role check, but if the cause is different, then the behavior won't be defined, same as what happens when accessing the site using WordPress.com or Jeptack CP. Another point to keep in mind: this is needed just because we fetch the site using XMLRPC for now, if we get a chance to work on improving this part, we will handle the Woo detection differently, as we will be able to infer whether its present or not from fetching the root
Contributor
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. Thank you for the explanation! |
||
| WooPayload(true) | ||
| } | ||
| else -> WooPayload(response.error.toWooError()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
These functions in the
SiteStoreare suspendable, and yet, the DB logic wasn't offloaded correctly to the background thread, with this change, we follow the convention that the suspendable functions in theStoresuse the default context.This exact issue was fixed for
fetchSitesin #2254, and this PR updates the two otherfetchfunctions.