Skip to content

Commit

Permalink
Avoid returning empty query results if end of stream is not reached.
Browse files Browse the repository at this point in the history
If the result of the current query is empty but next href exists perform next query instead.
Otherwise, the empty result will be treated as end of stream.
  • Loading branch information
0xf4b1 committed May 9, 2024
1 parent 2a1b8b0 commit b8ba017
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions soundcloud/src/main/java/com/tiefensuche/soundcloud/api/Requests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,28 @@ class Requests {

if (!reset && url in session.nextQueryUrls) {
session.nextQueryUrls[url]?.let {
if (it == JSON_NULL) {
return JSONArray()
}
currentUrl = it
} ?: return JSONArray()
}

val response = request(currentUrl)
val json = JSONObject(response.value)
if (!json.isNull(NEXT_HREF)) {
session.nextQueryUrls[url] = json.getString(NEXT_HREF)
} else {
session.nextQueryUrls[url] = JSON_NULL
}
while (currentUrl != JSON_NULL) {
val response = request(currentUrl)
val json = JSONObject(response.value)
currentUrl = if (!json.isNull(NEXT_HREF)) {
json.getString(NEXT_HREF)
} else {
JSON_NULL
}
session.nextQueryUrls[url] = currentUrl

if (!json.has(COLLECTION)) {
return JSONArray()
if (!json.has(COLLECTION)) {
return JSONArray()
}
val collection = json.getJSONArray(COLLECTION)
if (collection.length() > 0)
return collection
}
return json.getJSONArray(COLLECTION)
return JSONArray()
}
}

Expand Down

0 comments on commit b8ba017

Please sign in to comment.