Skip to content

Commit

Permalink
Add support to fetch WP media using v2 endpoint
Browse files Browse the repository at this point in the history
This enables fetching media successfully for Jetpack CP sites
  • Loading branch information
JorgeMucientes committed Sep 18, 2024
1 parent a6a3b2f commit 0d25ae0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.wordpress.android.fluxc.network.rest.wpcom.WPComGsonRequest
import org.wordpress.android.fluxc.store.MediaStore.FetchMediaListResponsePayload
import org.wordpress.android.fluxc.store.MediaStore.MediaError
import org.wordpress.android.fluxc.store.MediaStore.MediaErrorType
import org.wordpress.android.fluxc.store.MediaStore.MediaPayload
import org.wordpress.android.fluxc.store.MediaStore.ProgressPayload
import org.wordpress.android.fluxc.tools.CoroutineEngine
import org.wordpress.android.fluxc.utils.MimeType
Expand Down Expand Up @@ -91,6 +92,16 @@ abstract class BaseWPV2MediaRestClient constructor(
}
}

fun fetchMedia(
site: SiteModel,
media: MediaModel
) {
coroutineEngine.launch(MEDIA, this, "Fetching Media using WPCom's v2 API") {
val payload = syncFetchMedia(site, media.mediaId)
dispatcher.dispatch(MediaActionBuilder.newFetchedMediaAction(payload))
}
}

@Suppress("TooGenericExceptionCaught", "SwallowedException")
private fun syncUploadMedia(site: SiteModel, media: MediaModel): Flow<ProgressPayload> {
fun ProducerScope<ProgressPayload>.handleFailure(media: MediaModel, error: MediaError) {
Expand Down Expand Up @@ -172,6 +183,44 @@ abstract class BaseWPV2MediaRestClient constructor(
}
}

private suspend fun syncFetchMedia(site: SiteModel, mediaId: Long): MediaPayload {
val url = WPAPI.media.id(mediaId)
val response = executeGetGsonRequest(
site,
url,
emptyMap(),
MediaWPRESTResponse::class.java
)

return when (response) {
is WPAPIResponse.Error -> {
val errorMessage = "Fail to fetch media. Response: $response"
AppLog.w(MEDIA, errorMessage)
val error = MediaError(MediaErrorType.fromBaseNetworkError(response.error))
error.logMessage = errorMessage
MediaPayload(site, null, error)
}

is WPAPIResponse.Success -> {
val fetchedMedia = response.data?.toMediaModel(site.id)
when {
fetchedMedia != null -> {
AppLog.v(MEDIA, "Fetched media successfully for mediaId: $mediaId")
MediaPayload(site, fetchedMedia)
}

else -> {
AppLog.w(
MEDIA,
"Request successful but fetched media is null for mediaId: $mediaId"
)
MediaPayload(site, null, MediaError(MediaErrorType.NULL_MEDIA_ARG))
}
}
}
}
}

private suspend fun syncFetchMediaList(
site: SiteModel,
perPage: Int,
Expand Down Expand Up @@ -202,6 +251,7 @@ abstract class BaseWPV2MediaRestClient constructor(
error.logMessage = errorMessage
FetchMediaListResponsePayload(site, error, mimeType)
}

is WPAPIResponse.Success -> {
val mediaList = response.data.orEmpty().map { it.toMediaModel(site.id) }
AppLog.v(MEDIA, "Fetched media list for site with size: " + mediaList.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,8 @@ private void performFetchMedia(@NonNull MediaPayload payload) {

if (payload.site.isUsingWpComRestApi()) {
mMediaRestClient.fetchMedia(payload.site, payload.media);
} else if (payload.site.isJetpackCPConnected()) {
mWPComV2MediaRestClient.fetchMedia(payload.site, payload.media);
} else {
mMediaXmlrpcClient.fetchMedia(payload.site, payload.media);
}
Expand Down

0 comments on commit 0d25ae0

Please sign in to comment.