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

Commit d2b5254

Browse files
Add a coroutine based implementation for fetching site info
1 parent 6a73a88 commit d2b5254

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/site/SiteRestClient.kt

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import org.wordpress.android.fluxc.store.SiteStore.SuggestDomainErrorType.EMPTY_
8888
import org.wordpress.android.fluxc.store.SiteStore.SuggestDomainsResponsePayload
8989
import org.wordpress.android.fluxc.store.SiteStore.UserRolesError
9090
import org.wordpress.android.fluxc.store.SiteStore.UserRolesErrorType
91+
import org.wordpress.android.fluxc.tools.CoroutineEngine
9192
import org.wordpress.android.fluxc.utils.SiteUtils
9293
import org.wordpress.android.util.AppLog
9394
import org.wordpress.android.util.AppLog.T.API
@@ -110,6 +111,7 @@ class SiteRestClient @Inject constructor(
110111
@Named("regular") requestQueue: RequestQueue?,
111112
private val appSecrets: AppSecrets,
112113
private val wpComGsonRequestBuilder: WPComGsonRequestBuilder,
114+
private val coroutineEngine: CoroutineEngine,
113115
accessToken: AccessToken?,
114116
userAgent: UserAgent?
115117
) : BaseWPComRestClient(appContext, dispatcher, requestQueue, accessToken, userAgent) {
@@ -673,34 +675,58 @@ class SiteRestClient @Inject constructor(
673675
}
674676

675677
// Unauthenticated network calls
676-
@Suppress("SwallowedException")
677678
fun fetchConnectSiteInfo(siteUrl: String) {
679+
coroutineEngine.launch(AppLog.T.API, this, "fetchConnectSiteInfo") {
680+
fetchConnectSiteInfoSync(siteUrl).let { payload ->
681+
mDispatcher.dispatch(SiteActionBuilder.newFetchedConnectSiteInfoAction(payload))
682+
}
683+
}
684+
}
685+
686+
@Suppress("SwallowedException")
687+
suspend fun fetchConnectSiteInfoSync(siteUrl: String): ConnectSiteInfoPayload {
688+
fun ConnectSiteInfoResponse.toConnectSiteInfoPayload(url: String): ConnectSiteInfoPayload {
689+
return ConnectSiteInfoPayload(
690+
url,
691+
exists,
692+
isWordPress,
693+
hasJetpack,
694+
isJetpackActive,
695+
isJetpackConnected,
696+
isWordPressDotCom, // CHECKSTYLE IGNORE
697+
urlAfterRedirects
698+
)
699+
}
700+
678701
// Get a proper URI to reliably retrieve the scheme.
679702
val uri: URI = try {
680703
URI.create(UrlUtils.addUrlSchemeIfNeeded(siteUrl, false))
681704
} catch (e: IllegalArgumentException) {
682705
val siteError = SiteError(INVALID_SITE)
683-
val payload = ConnectSiteInfoPayload(siteUrl, siteError)
684-
mDispatcher.dispatch(SiteActionBuilder.newFetchedConnectSiteInfoAction(payload))
685-
return
706+
return ConnectSiteInfoPayload(siteUrl, siteError)
686707
}
708+
687709
val params = mutableMapOf<String, String>()
688710
params["url"] = uri.toString()
689711

690712
// Make the call.
691713
val url = WPCOMREST.connect.site_info.urlV1_1
692-
val request = WPComGsonRequest.buildGetRequest(url, params,
693-
ConnectSiteInfoResponse::class.java,
694-
{ response ->
695-
val info = connectSiteInfoFromResponse(siteUrl, response)
696-
mDispatcher.dispatch(SiteActionBuilder.newFetchedConnectSiteInfoAction(info))
697-
}
698-
) {
699-
val siteError = SiteError(INVALID_SITE)
700-
val info = ConnectSiteInfoPayload(siteUrl, siteError)
701-
mDispatcher.dispatch(SiteActionBuilder.newFetchedConnectSiteInfoAction(info))
714+
val response = wpComGsonRequestBuilder.syncGetRequest(
715+
restClient = this,
716+
url = url,
717+
params = params,
718+
clazz = ConnectSiteInfoResponse::class.java
719+
)
720+
721+
return when (response) {
722+
is Error -> {
723+
val siteError = SiteError(INVALID_SITE)
724+
ConnectSiteInfoPayload(siteUrl, siteError)
725+
}
726+
is Success -> {
727+
response.data.toConnectSiteInfoPayload(siteUrl)
728+
}
702729
}
703-
addUnauthedRequest(request)
704730
}
705731

706732
@Suppress("SwallowedException")
@@ -1229,19 +1255,6 @@ class SiteRestClient @Inject constructor(
12291255
return payload
12301256
}
12311257

1232-
private fun connectSiteInfoFromResponse(url: String, response: ConnectSiteInfoResponse): ConnectSiteInfoPayload {
1233-
return ConnectSiteInfoPayload(
1234-
url,
1235-
response.exists,
1236-
response.isWordPress,
1237-
response.hasJetpack,
1238-
response.isJetpackActive,
1239-
response.isJetpackConnected,
1240-
response.isWordPressDotCom, // CHECKSTYLE IGNORE
1241-
response.urlAfterRedirects
1242-
)
1243-
}
1244-
12451258
private fun responseToDomainAvailabilityPayload(
12461259
response: DomainAvailabilityResponse
12471260
): DomainAvailabilityResponsePayload {

fluxc/src/main/java/org/wordpress/android/fluxc/store/SiteStore.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,7 @@ open class SiteStore @Inject constructor(
402402
}
403403
}
404404

405-
data class ConnectSiteInfoPayload
406-
@JvmOverloads constructor(
405+
data class ConnectSiteInfoPayload @JvmOverloads constructor(
407406
@JvmField val url: String,
408407
@JvmField val exists: Boolean = false,
409408
@JvmField val isWordPress: Boolean = false,
@@ -1861,6 +1860,12 @@ open class SiteStore @Inject constructor(
18611860
siteRestClient.fetchConnectSiteInfo(payload)
18621861
}
18631862

1863+
suspend fun fetchConnectSiteInfoSync(siteUrl: String): ConnectSiteInfoPayload {
1864+
return coroutineEngine.withDefaultContext(T.API, this, "Fetch Connect Site Info") {
1865+
siteRestClient.fetchConnectSiteInfoSync(siteUrl)
1866+
}
1867+
}
1868+
18641869
private fun handleFetchedConnectSiteInfo(payload: ConnectSiteInfoPayload) {
18651870
val event = OnConnectSiteInfoChecked(payload)
18661871
event.error = payload.error

0 commit comments

Comments
 (0)