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

Commit 8776bc1

Browse files
Merge pull request #3038 from wordpress-mobile/add/woo-gla-eligibility-check
[Woo: Google Listings and Ads] Create store and add functionality to check Google Ads connection status
2 parents e7f84fa + 5a2342f commit 8776bc1

File tree

6 files changed

+112
-1
lines changed

6 files changed

+112
-1
lines changed

example/src/main/java/org/wordpress/android/fluxc/example/ui/WooCommerceFragment.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import org.wordpress.android.fluxc.example.ui.storecreation.WooStoreCreationFrag
3030
import org.wordpress.android.fluxc.example.ui.taxes.WooTaxFragment
3131
import org.wordpress.android.fluxc.example.ui.wooadmin.WooAdminFragment
3232
import org.wordpress.android.fluxc.store.WCDataStore
33+
import org.wordpress.android.fluxc.store.WCGoogleStore
3334
import org.wordpress.android.fluxc.store.WCUserStore
3435
import org.wordpress.android.fluxc.store.WooCommerceStore
3536
import org.wordpress.android.util.AppLog
@@ -41,6 +42,7 @@ class WooCommerceFragment : StoreSelectingFragment() {
4142
@Inject lateinit var wooCommerceStore: WooCommerceStore
4243
@Inject lateinit var wooDataStore: WCDataStore
4344
@Inject lateinit var wooUserStore: WCUserStore
45+
@Inject lateinit var wooGoogleStore: WCGoogleStore
4446

4547
private val coroutineScope = CoroutineScope(Dispatchers.Main)
4648

@@ -231,6 +233,22 @@ class WooCommerceFragment : StoreSelectingFragment() {
231233
woo_admin.setOnClickListener {
232234
replaceFragment(WooAdminFragment())
233235
}
236+
237+
woo_gla_google_ads_status.setOnClickListener {
238+
selectedSite?.let { selectedSite ->
239+
coroutineScope.launch {
240+
val result = withContext(Dispatchers.Default) {
241+
wooGoogleStore.isGoogleAdsAccountConnected(selectedSite)
242+
}
243+
result.error?.let {
244+
prependToLog("Error in fetchGoogleAdsConnectionStatus: ${it.type} - ${it.message}")
245+
}
246+
result.model?.let {
247+
prependToLog("Google Ads connection status: $it")
248+
} ?: prependToLog("Couldn't fetch Google Ads connection status.")
249+
}
250+
} ?: showNoWCSitesToast()
251+
}
234252
}
235253

236254
@Suppress("TooGenericExceptionCaught")

example/src/main/res/layout/fragment_woocommerce.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,11 @@
147147
android:layout_height="wrap_content"
148148
android:text="WooCommerce Admin" />
149149

150+
<Button
151+
android:id="@+id/woo_gla_google_ads_status"
152+
android:layout_width="match_parent"
153+
android:layout_height="wrap_content"
154+
android:text="Google Listing and Ads: Check Google Ads Connection Status" />
155+
150156
</LinearLayout>
151157
</ScrollView>

fluxc-annotations/src/main/java/org/wordpress/android/fluxc/annotations/endpoint/WCWPAPIEndpoint.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.wordpress.android.fluxc.annotations.endpoint;
22

33
public class WCWPAPIEndpoint {
4+
private static final String WC_PREFIX = "wc";
5+
46
private static final String WC_PREFIX_V3 = "wc/v3";
57

68
private static final String WC_PREFIX_V1 = "wc/v1";
@@ -51,6 +53,10 @@ public String getPathV4() {
5153
return "/" + WC_PREFIX_V4 + mEndpoint;
5254
}
5355

56+
public String getPathNoVersion() {
57+
return "/" + WC_PREFIX + mEndpoint;
58+
}
59+
5460
public String getPathV4Analytics() {
5561
return "/" + WC_PREFIX_V4_ANALYTICS + mEndpoint;
5662
}

fluxc-processor/src/main/resources/wc-wp-api-endpoints.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,6 @@
122122
/options
123123

124124
/shipping_methods
125-
/shipping_methods/<id>#String
125+
/shipping_methods/<id>#String
126+
127+
/gla/ads/connection
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.wordpress.android.fluxc.network.rest.wpcom.wc.google
2+
3+
import com.google.gson.annotations.SerializedName
4+
import org.wordpress.android.fluxc.generated.endpoint.WOOCOMMERCE
5+
import org.wordpress.android.fluxc.model.SiteModel
6+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooNetwork
7+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload
8+
import org.wordpress.android.fluxc.utils.toWooPayload
9+
import javax.inject.Inject
10+
import javax.inject.Singleton
11+
12+
@Singleton
13+
class WCGoogleRestClient @Inject constructor(private val wooNetwork: WooNetwork) {
14+
companion object {
15+
const val GOOGLE_ADS_CONNECTED_STATUS = "connected"
16+
}
17+
18+
suspend fun fetchGoogleAdsConnectionStatus(
19+
site: SiteModel
20+
): WooPayload<Boolean> {
21+
val url = WOOCOMMERCE.gla.ads.connection.pathNoVersion
22+
val result = wooNetwork.executeGetGsonRequest(
23+
site = site,
24+
path = url,
25+
clazz = GoogleAdsConnectionStatusResponse::class.java
26+
).toWooPayload()
27+
28+
return when {
29+
result.isError -> WooPayload(result.error)
30+
result.result != null -> WooPayload(result.result.status == GOOGLE_ADS_CONNECTED_STATUS)
31+
else -> WooPayload(false)
32+
}
33+
}
34+
}
35+
36+
/**
37+
* Response model for the Google Ads connection status.
38+
* The full response has more fields, but for now we only care about the status.
39+
*/
40+
data class GoogleAdsConnectionStatusResponse(
41+
@SerializedName("status")
42+
val status: String
43+
)
44+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.wordpress.android.fluxc.store
2+
3+
import org.wordpress.android.fluxc.model.SiteModel
4+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooResult
5+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.google.WCGoogleRestClient
6+
import org.wordpress.android.fluxc.tools.CoroutineEngine
7+
import org.wordpress.android.util.AppLog.T.API
8+
import javax.inject.Inject
9+
import javax.inject.Singleton
10+
11+
/**
12+
* This store is intended to be used to support the Google Listings and Ads plugin.
13+
* https://wordpress.org/plugins/google-listings-and-ads/
14+
*/
15+
@Singleton
16+
class WCGoogleStore @Inject constructor(
17+
private val restClient: WCGoogleRestClient,
18+
private val coroutineEngine: CoroutineEngine
19+
) {
20+
/**
21+
* Checks the connection status of the Google Ads account used in the plugin.
22+
*
23+
* @return WooResult<Boolean> true if the account is connected, false otherwise. Optionally,
24+
* passes error, too.
25+
*/
26+
suspend fun isGoogleAdsAccountConnected(site: SiteModel): WooResult<Boolean> =
27+
coroutineEngine.withDefaultContext(API, this, "fetchGoogleAdsConnectionStatus") {
28+
val response = restClient.fetchGoogleAdsConnectionStatus(site)
29+
when {
30+
response.isError -> WooResult(response.error)
31+
response.result != null -> response.asWooResult()
32+
else -> WooResult(false)
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)