Skip to content

Commit

Permalink
fix assets parsing and add some logging for not OK responses
Browse files Browse the repository at this point in the history
  • Loading branch information
fcs-ts committed Sep 13, 2024
1 parent 096ced6 commit 37c6330
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ suspend fun runBannerAuction(config: BannerConfig): BannerResponse? {
val auctionJob = CoroutineScope(Dispatchers.IO).launch {
response = TopsortAuctionsHttpService.runAuctions(request)
}
auctionJob.join()
if ((response?.results?.isNotEmpty() == true)) {
if (response!!.results[0].winners.isNotEmpty()) {
val winner = response!!.results[0].winners[0]
return BannerResponse(
id = winner.id,
url = winner.asset!!.url,
url = winner.asset!![0].url,
type = winner.type,
resolvedBidId = winner.resolvedBidId
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data class Auction private constructor(
put("products", JSONObject.wrap(products))
}
if (category != null) {
put("category", JSONObject.wrap(category))
put("category", category.toJsonObject())
}
if (searchQuery != null) {
put("searchQuery", searchQuery)
Expand Down Expand Up @@ -214,7 +214,23 @@ data class Auction private constructor(
val id: String? = null,
val ids: List<String>? = null,
val disjunctions: List<List<String>>? = null,
)
) {
fun toJsonObject(): JSONObject {
val builder = JSONObject()
with(builder) {
if (id != null) {
put("id", id)
}
if (ids != null) {
put("ids", JSONObject.wrap(ids))
}
if (disjunctions != null) {
put("disjunctions", JSONObject.wrap(disjunctions))
}
}
return builder
}
}

data class GeoTargeting(
val location: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,33 @@ data class AuctionResponse private constructor(
val type: EntityType,
val id: String,
val resolvedBidId: String,
val asset: Asset? = null,
val asset: List<Asset>? = null,
) {
companion object {
fun fromJsonObject(json: JSONObject): AuctionWinnerItem {
val assetArray = json.optJSONArray("asset")
var assets: List<Asset>? = null
if (assetArray != null) {
assets = (0 until assetArray.length()).map {
Asset.fromJsonObject(assetArray.getJSONObject(it))
}
}
return AuctionWinnerItem(
rank = json.getInt("rank"),
type = EntityType.fromValue(json.getString("type")),
id = json.getString("id"),
resolvedBidId = json.getString("resolvedBidId"),
asset = Asset.fromJsonObject(json),
asset = assets,
)
}
}
}


data class Asset(val url: String) {
companion object {
fun fromJsonObject(json: JSONObject): Asset? {
val asset = json.optJSONObject("asset") ?: return null
val url = asset.getString("url")
fun fromJsonObject(json: JSONObject): Asset {
val url = json.getString("url")
return Asset(url = url)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.topsort.analytics.service

import android.util.Log
import com.topsort.analytics.Cache
import com.topsort.analytics.core.HttpClient
import com.topsort.analytics.core.HttpResponse
Expand All @@ -17,12 +18,15 @@ internal object TopsortAuctionsHttpService {
val response = executeRunAuctions(auctionRequest)
if (response.isSuccessful()) {
return AuctionResponse.fromJson(response.body)
} else {
Log.w("TopsortAuctionsHttpService", "Auction message: " + response.message)
Log.w("TopsortAuctionsHttpService", "Auction response: " + response.body.toString())
}
return null
}

private fun executeRunAuctions(auctionRequest: AuctionRequest): HttpResponse {
if(!this::httpClient.isInitialized){
if (!this::httpClient.isInitialized) {
httpClient = HttpClient("${baseApiUrl}${AUCTION_ENDPOINT}")
}
val json = auctionRequest.toJsonObject().toString()
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/topsort/example/SampleActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class SampleActivity : AppCompatActivity() {

this.lifecycleScope.launch {
val bannerView = findViewById<BannerView>(R.id.bannerView)
val bannerConfig = BannerConfig.LandingPage(slotId = "sample", ids = listOf("p1", "p2"))
val bannerConfig =
BannerConfig.CategorySingle(slotId = "slot", category = "category")
bannerView.setup(
bannerConfig,
"sample_activity",
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/java/com/topsort/example/TestApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class TestApplication : Application() {
opaqueUserId = sessionId,
token = BuildConfig.TOKEN
)

val config = BannerConfig.LandingPage(slotId = "app", ids = listOf("p1", "p2"))
// val banner = BannerView(context = , config = config)



}
}

0 comments on commit 37c6330

Please sign in to comment.