com.gravity.gdk.placement.Placement.scala Maven / Gradle / Ivy
The newest version!
package com.gravity.gdk.placement
import com.fasterxml.jackson.core.JsonParseException
import com.gravity.gdk.config.DefaultSettings
import com.gravity.gdk.reco.{RecoContext, RecoResult}
import scala.collection.mutable
import scala.concurrent._
import scalaj.http.{BaseHttp, Http}
/*
___...---''
___...---'\'___
'' _.-'' _`'.______\\.
/_.) )..- __..--'\\
( __..--''
'-''\@
Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ
*/
/**
* Article recommendations are generated against a placement. Placements at this time are configured for you by a
* Gravity Account Manager.
*/
case class Placement(key: PlacementKey) {
/**
* @param limit Limit number of article recommendations. The default is the number of articles configured for your
* placement. The limit you specify must be <= the number of articles
* configured for your placement.
* @param bucketNumber Optional bucket number override; if you don't know what this is or haven't received
* instructions from Gravity to use this parameter, then it is safe to ignore.
* @param logImpression If FALSE, you *must* log your impression using [[com.gravity.gdk.impression.Impression.log]].
*/
def getRecos(
limit: Int = 0,
bucketNumber: Int = 0,
logImpression: Boolean = true
)(implicit recoCtx: RecoContext, http: BaseHttp = Http, ec: ExecutionContext): Future[RecoResult] = Future {
val params = mutable.HashMap(
"placement" -> key.placementId.toString,
"userguid" -> recoCtx.user.userGuid,
"url" -> recoCtx.currentUrl,
"_" -> System.currentTimeMillis().toString,
"pageViewGuid" -> recoCtx.pageViewGuid.guid
)
if(limit > 0)
params += "limit" -> limit.toString
if(recoCtx.imageWidth > 0)
params += "imageWidth" -> recoCtx.imageWidth.toString
if(recoCtx.imageHeight > 0)
params += "imageHeight" -> recoCtx.imageHeight.toString
if(bucketNumber > 0)
params += "bucket" -> bucketNumber.toString
if(!logImpression)
params += "logResult" -> "0"
val response = http(DefaultSettings.apiRecosUrl(key.placementId)).params(params.toMap).asString
if(response.isSuccess) {
try {
RecoResult.fromApiResponse(response) match {
case Left(errors) =>
throw PlacementGetRecosResponseFormatException(errors)
case Right(recoResult) =>
recoResult
}
}
catch {
case ex: JsonParseException => throw PlacementGetRecosResponseJsonParseException(ex)
}
}
else
throw PlacementGetRecosBadHttpStatusException(response.code)
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy