All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gravity.gdk.placement.ImpressionViewedSpec.scala Maven / Gradle / Ivy

The newest version!
package com.gravity.gdk.placement

import com.gravity.gdk.util.error.jsErrorToErrors
import play.api.libs.functional.syntax._
import play.api.libs.json._

import scalaj.http.HttpResponse

/*
              ___...---''
  ___...---'\'___
''       _.-''  _`'.______\\.
        /_.)  )..-  __..--'\\
       (    __..--''
        '-''\@


 Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ
*/

/** Specifies how an impression viewed is logged with Gravity. This is generally internal use only. */
case class ImpressionViewedSpec(
  callbackUrl: String,
  hash: String
)

object ImpressionViewedSpec {
  lazy val empty = ImpressionViewedSpec("", "")

  def fromApiResponse(apiResponse: HttpResponse[String]): Either[Seq[Error], ImpressionViewedSpec] = {
    val impViewedHeader = "Grv-Impression-Viewed"
    apiResponse.header(impViewedHeader) match {
      case None => Left(Seq(new Error(s"API response missing header $impViewedHeader")))
      case Some(json) =>
        Json.parse(json).validate[ImpressionViewedSpec] match {
          case e: JsError => Left(jsErrorToErrors(e))
          case JsSuccess(spec, _) => Right(spec)
        }
    }
  }

  private[gdk] implicit val jsonFormat: Format[ImpressionViewedSpec] = Format(
    (
      (__ \ "callback").read[String] and
      (__ \ "hash").read[String]
    )(ImpressionViewedSpec.apply _),

    Writes(spec => Json.obj(
      "callback" -> spec.callbackUrl,
      "hash" -> spec.hash
    ))
  )
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy