Skip to content

Commit

Permalink
Merge pull request #2869 from wordpress-mobile/task/add-fetch-all-dom…
Browse files Browse the repository at this point in the history
…ains-call-resolve-status

Enhances the fetch all-domains api call to resolve domain status
  • Loading branch information
Antonis Lilis authored Oct 13, 2023
2 parents ae6a6e7 + 329bf4b commit 21b9cb5
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class DomainsFragment : StoreSelectingFragment() {

fetch_all_domains.setOnClickListener {
lifecycleScope.launch {
val result = store.fetchAllDomains(noWpCom = false) // fetching wpcom too for debugging purposes
// fetching wpcom too for debugging purposes
val result = store.fetchAllDomains(noWpCom = false, resolveStatus = true)
when {
result.isError -> {
prependToLog("Error fetching all domains: ${result.error.message}")
Expand All @@ -79,7 +80,8 @@ class DomainsFragment : StoreSelectingFragment() {
prependToLog("All domains count: ${result.domains?.size}")
val domains = result.domains
?.joinToString(separator = "\n") {
"${it.domain} (type: ${it.type}), expiry: ${it.expiry}"
"${it.domain} (type: ${it.type}, expiry: ${it.expiry}, " +
"status: ${it.domainStatus?.statusType.toString()})"
}
prependToLog("Domains:\n$domains")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ import org.wordpress.android.fluxc.network.rest.wpcom.auth.AccessToken
import org.wordpress.android.fluxc.network.rest.wpcom.auth.AppSecrets
import org.wordpress.android.fluxc.network.rest.wpcom.site.NewSiteResponse.BlogDetails
import org.wordpress.android.fluxc.network.rest.wpcom.site.SiteWPComRestResponse.SitesResponse
import org.wordpress.android.fluxc.network.rest.wpcom.site.StatusType.ERROR
import org.wordpress.android.fluxc.network.rest.wpcom.site.StatusType.SUCCESS
import org.wordpress.android.fluxc.store.SiteStore.PostFormatsErrorType
import org.wordpress.android.fluxc.store.SiteStore.SiteFilter.WPCOM
import org.wordpress.android.fluxc.store.SiteStore.SiteVisibility
import org.wordpress.android.fluxc.store.SiteStore.SiteVisibility.COMING_SOON
import org.wordpress.android.fluxc.store.SiteStore.SiteVisibility.PUBLIC
import org.wordpress.android.fluxc.test
import org.wordpress.android.util.DateTimeUtils
import kotlin.test.assertNotNull

@RunWith(MockitoJUnitRunner::class)
Expand Down Expand Up @@ -538,14 +541,32 @@ class SiteRestClientTest {

initAllDomainsResponse(data = response)

val responseModel = restClient.fetchAllDomains(noWpCom = true)
val responseModel = restClient.fetchAllDomains()
assert(responseModel is Success)
with((responseModel as Success).data) {
assertThat(domains).hasSize(2)
assertThat(domains).hasSize(4)
assertThat(domains[0].domain).isEqualTo("some.test.domain")
assertThat(domains[0].wpcomDomain).isFalse
assertThat(domains[1].domain).isEqualTo("some.test.domain 2")
assertThat(domains[0].registrationDate).isEqualTo(
DateTimeUtils.dateUTCFromIso8601("2009-03-26T21:20:53+00:00")
)
assertThat(domains[0].expiry).isEqualTo(
DateTimeUtils.dateUTCFromIso8601("2024-03-24T00:00:00+00:00")
)
assertThat(domains[0].domainStatus).isNotNull
assertThat(domains[0].domainStatus?.status).isEqualTo("Active")
assertThat(domains[0].domainStatus?.statusType).isEqualTo(SUCCESS)
assertThat(domains[1].domain).isEqualTo("some.test.domain.with.status.weight")
assertThat(domains[1].wpcomDomain).isTrue
assertThat(domains[1].domainStatus).isNotNull
assertThat(domains[1].domainStatus?.status).isEqualTo("Expiring soon")
assertThat(domains[1].domainStatus?.statusType).isEqualTo(ERROR)
assertThat(domains[1].domainStatus?.statusWeight).isEqualTo(1000)
assertThat(domains[2].domain).isEqualTo("some.test.domain.with.action.required")
assertThat(domains[2].domainStatus).isNotNull
assertThat(domains[2].domainStatus?.actionRequired).isTrue
assertThat(domains[3].domain).isEqualTo("some.test.domain.no.domain.status")
assertThat(domains[3].domainStatus).isNull()
}
}

Expand All @@ -554,7 +575,7 @@ class SiteRestClientTest {
val error = WPComGsonNetworkError(BaseNetworkError(GenericErrorType.NETWORK_ERROR))
initAllDomainsResponse(error = error)

val response = restClient.fetchAllDomains(noWpCom = true)
val response = restClient.fetchAllDomains()
assert(response is Response.Error)
with((response as Response.Error).error) {
assertThat(type).isEqualTo(GenericErrorType.NETWORK_ERROR)
Expand All @@ -567,7 +588,7 @@ class SiteRestClientTest {
val error = WPComGsonNetworkError(BaseNetworkError(GenericErrorType.TIMEOUT))
initAllDomainsResponse(error = error)

val response = restClient.fetchAllDomains(noWpCom = true)
val response = restClient.fetchAllDomains()
assert(response is Response.Error)
with((response as Response.Error).error) {
assertThat(type).isEqualTo(GenericErrorType.TIMEOUT)
Expand All @@ -581,7 +602,7 @@ class SiteRestClientTest {
val error = WPComGsonNetworkError(BaseNetworkError(GenericErrorType.NOT_AUTHENTICATED, tokenErrorMessage))
initAllDomainsResponse(error = error)

val response = restClient.fetchAllDomains(noWpCom = true)
val response = restClient.fetchAllDomains()
assert(response is Response.Error)
with((response as Response.Error).error) {
assertThat(type).isEqualTo(GenericErrorType.NOT_AUTHENTICATED)
Expand Down
49 changes: 46 additions & 3 deletions example/src/test/resources/wp/all-domains/all-domains.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,55 @@
"expiry": "2024-03-24T00:00:00+00:00",
"wpcom_domain": false,
"current_user_is_owner": true,
"site_slug": "test slug"
"site_slug": "test slug",
"domain_status": {
"status": "Active",
"status_type": "success"
}
},
{
"domain": "some.test.domain 2",
"domain": "some.test.domain.with.status.weight",
"blog_id": 22222,
"blog_name": "some test blog 2",
"blog_name": "some test blog with status_weight",
"type": "mapping",
"is_domain_only_site": false,
"is_wpcom_staging_domain": false,
"has_registration": false,
"registration_date": "2009-03-26T21:20:53+00:00",
"expiry": "2024-03-24T00:00:00+00:00",
"wpcom_domain": true,
"current_user_is_owner": false,
"site_slug": "test slug 2",
"domain_status": {
"status": "Expiring soon",
"status_type": "error",
"status_weight": 1000
}
},
{
"domain": "some.test.domain.with.action.required",
"blog_id": 22222,
"blog_name": "some test blog with action_required",
"type": "mapping",
"is_domain_only_site": false,
"is_wpcom_staging_domain": false,
"has_registration": false,
"registration_date": "2009-03-26T21:20:53+00:00",
"expiry": "2024-03-24T00:00:00+00:00",
"wpcom_domain": true,
"current_user_is_owner": false,
"site_slug": "test slug 2",
"domain_status": {
"status": "Expiring soon",
"status_type": "error",
"status_weight": 1000,
"action_required": true
}
},
{
"domain": "some.test.domain.no.domain.status",
"blog_id": 22222,
"blog_name": "some test blog no domain status",
"type": "mapping",
"is_domain_only_site": false,
"is_wpcom_staging_domain": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package org.wordpress.android.fluxc.network.rest.wpcom.site

import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonParseException
import com.google.gson.annotations.JsonAdapter
import com.google.gson.annotations.SerializedName
import org.wordpress.android.util.DateTimeUtils
import java.lang.reflect.Type
import java.util.Date

data class AllDomainsResponse(val domains: List<AllDomainsDomain>)

Expand All @@ -24,9 +31,11 @@ data class AllDomainsDomain(
@JsonAdapter(BooleanTypeAdapter::class)
val hasRegistration: Boolean = false,
@SerializedName("registration_date")
val registrationDate: String? = null,
@JsonAdapter(AllDomainsDateAdapter::class)
val registrationDate: Date? = null,
@SerializedName("expiry")
val expiry: String? = null,
@JsonAdapter(AllDomainsDateAdapter::class)
val expiry: Date? = null,
@SerializedName("wpcom_domain")
@JsonAdapter(BooleanTypeAdapter::class)
val wpcomDomain: Boolean = false,
Expand All @@ -35,4 +44,71 @@ data class AllDomainsDomain(
val currentUserIsOwner: Boolean = false,
@SerializedName("site_slug")
val siteSlug: String? = null,
@SerializedName("domain_status")
val domainStatus: DomainStatus? = null,
)

data class DomainStatus(
@SerializedName("status")
val status: String? = null,
@SerializedName("status_type")
@JsonAdapter(StatusTypeAdapter::class)
val statusType: StatusType? = null,
@SerializedName("status_weight")
val statusWeight: Long? = 0,
@SerializedName("action_required")
@JsonAdapter(BooleanTypeAdapter::class)
val actionRequired: Boolean? = false,
)

enum class StatusType(private val stringValue: String) {
SUCCESS("success"),
NEUTRAL("neutral"),
ALERT("alert"),
WARNING("warning"),
ERROR("error"),
UNKNOWN("unknown");

override fun toString() = stringValue

companion object {
fun fromString(string: String): StatusType {
for (item in values()) {
if (item.stringValue == string) {
return item
}
}
return UNKNOWN
}
}
}

internal class StatusTypeAdapter : JsonDeserializer<StatusType> {
@Throws(JsonParseException::class)
override fun deserialize(
json: JsonElement,
typeOfT: Type,
context: JsonDeserializationContext
): StatusType {
val jsonPrimitive = json.asJsonPrimitive
return when {
jsonPrimitive.isString -> StatusType.fromString(jsonPrimitive.asString)
else -> StatusType.UNKNOWN
}
}
}

internal class AllDomainsDateAdapter : JsonDeserializer<Date?> {
@Throws(JsonParseException::class)
override fun deserialize(
json: JsonElement,
typeOfT: Type,
context: JsonDeserializationContext
): Date? {
val jsonPrimitive = json.asJsonPrimitive
return when {
jsonPrimitive.isString -> DateTimeUtils.dateUTCFromIso8601(jsonPrimitive.asString)
else -> null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -837,9 +837,12 @@ class SiteRestClient @Inject constructor(
add(request)
}

suspend fun fetchAllDomains(noWpCom: Boolean = true): Response<AllDomainsResponse> {
suspend fun fetchAllDomains(noWpCom: Boolean = true, resolveStatus: Boolean = true): Response<AllDomainsResponse> {
val url = WPCOMREST.all_domains.urlV1_1
val params = mapOf("no_wpcom" to noWpCom.toString())
val params = mapOf(
"no_wpcom" to noWpCom.toString(),
"resolve_status" to resolveStatus.toString()
)
return wpComGsonRequestBuilder.syncGetRequest(this, url, params, AllDomainsResponse::class.java)
}
suspend fun fetchSiteDomains(site: SiteModel): Response<DomainsResponse> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2098,10 +2098,13 @@ open class SiteStore @Inject constructor(
emitChange(event)
}

suspend fun fetchAllDomains(noWpCom: Boolean = true): FetchedAllDomainsPayload =
suspend fun fetchAllDomains(
noWpCom: Boolean = true,
resolveStatus: Boolean = true
): FetchedAllDomainsPayload =
coroutineEngine.withDefaultContext(T.API, this, "Fetch all domains") {
return@withDefaultContext when (val response =
siteRestClient.fetchAllDomains(noWpCom)) {
siteRestClient.fetchAllDomains(noWpCom, resolveStatus)) {
is Success -> {
val domains = response.data.domains
FetchedAllDomainsPayload(domains)
Expand Down

0 comments on commit 21b9cb5

Please sign in to comment.