|
@@ -20,6 +20,26 @@ internal class FlickrServiceLogic(private val api: FlickrApi) : FlickrService {
|
|
|
cachedPhotos
|
|
|
} ?: emptyList()
|
|
|
|
|
|
+ override suspend fun getRecentPhotos(): List<Photo> {
|
|
|
+ return runCatching {
|
|
|
+ val response = api.getRecent()
|
|
|
+ if (response.isSuccessful) {
|
|
|
+ response.body()?.photosPage?.photos ?: emptyList()
|
|
|
+ } else {
|
|
|
+ throw Exception(
|
|
|
+ "Flickr API responded with error: ${
|
|
|
+ response.errorBody().toString()
|
|
|
+ }"
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }.onFailure { exception: Throwable ->
|
|
|
+ Log.d(
|
|
|
+ LOG_TAG,
|
|
|
+ "Exception occurred when executing API call: ${exception.message}"
|
|
|
+ )
|
|
|
+ }.getOrDefault(emptyList())
|
|
|
+ }
|
|
|
+
|
|
|
private suspend fun updateCachedPhotos(searchQuery: String) =
|
|
|
runCatching {
|
|
|
val response = api.search(searchQuery)
|
|
@@ -29,9 +49,10 @@ internal class FlickrServiceLogic(private val api: FlickrApi) : FlickrService {
|
|
|
cachedPhotos.addAll(it.photosPage.photos)
|
|
|
}
|
|
|
} else {
|
|
|
- Log.d(
|
|
|
- LOG_TAG,
|
|
|
- "Flickr API responded with error: ${response.errorBody().toString()}"
|
|
|
+ throw Exception(
|
|
|
+ "Flickr API responded with error: ${
|
|
|
+ response.errorBody().toString()
|
|
|
+ }"
|
|
|
)
|
|
|
}
|
|
|
}.onFailure { exception: Throwable ->
|