com.gravity.gdk.reco.RecoResult.scala Maven / Gradle / Ivy
The newest version!
package com.gravity.gdk.reco
import com.gravity.gdk.placement.ImpressionViewedSpec
import com.gravity.gdk.util.error.jsErrorToErrors
import play.api.libs.json._
import scalaj.http.HttpResponse
/*
___...---''
___...---'\'___
'' _.-'' _`'.______\\.
/_.) )..- __..--'\\
( __..--''
'-''\@
Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ
*/
/**
* A set of article recommendations as received when using [[com.gravity.gdk.placement.Placement.getRecos]].
*
* @param articles The recommended articles.
* @param impressionViewed Information to assist in tracking impression viewed. Generally internal use only.
* @param impressionSlug This is Gravity internal use only.
* @param meta This contains internal fields and possibly custom fields specified in your RSS. If you desire
* metadata to be included, you should speak with your account manager.
*/
case class RecoResult(
articles: List[RecoArticle],
impressionViewed: ImpressionViewedSpec,
impressionSlug: String,
meta: Map[String, JsValue]
)
object RecoResult {
def fromApiResponse(apiResponse: HttpResponse[String]): Either[Seq[Error], RecoResult] = {
val articlesResult: Either[Seq[Error], List[RecoArticle]] =
RecoArticle.listFromApiResponse(apiResponse)
val impressionViewedResult: Either[Seq[Error], ImpressionViewedSpec] =
ImpressionViewedSpec.fromApiResponse(apiResponse)
val impressionSlugResult: Either[Seq[Error], String] = {
val impSlugHeader = "Grv-Impression-Slug"
apiResponse.header(impSlugHeader) match {
case None => Left(Seq(new Error(s"Missing required header $impSlugHeader")))
case Some(slug) => Right(slug)
}
}
val metaResult: Either[Seq[Error], Map[String, JsValue]] = apiResponse.header("Grv-Meta") match {
case None => Right(Map.empty)
case Some(json) =>
Json.parse(json).validate[Map[String, JsValue]] match {
case e: JsError => Left(jsErrorToErrors(e))
case JsSuccess(map, _) => Right(map)
}
}
val errors = articlesResult.left.getOrElse(Seq.empty) ++
impressionViewedResult.left.getOrElse(Seq.empty) ++
impressionSlugResult.left.getOrElse(Seq.empty) ++
metaResult.left.getOrElse(Seq.empty)
if(errors.nonEmpty)
Left(errors)
else {
val articles = articlesResult.right.get
val impressionViewedSpec = impressionViewedResult.right.get
val slug = impressionSlugResult.right.get
val meta = metaResult.right.get
Right(RecoResult(articles, impressionViewedSpec, slug, meta))
}
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy