Powerspace / scala-openrtb

Scala OpenRTB tools used at @Powerspace
https://powerspace.com
Apache License 2.0
23 stars 8 forks source link

Force usage of native specs when required #46

Open valdo404 opened 4 years ago

valdo404 commented 4 years ago

Dsps should forcibly reply native responses when requests contains native request.

hence the following code should be using the bid request to determine if impression is native based.

implicit val admOneOfDecoder: Decoder[SeatBid.Bid.AdmOneof] = { cursor =>
    cursor.focus.map {
      case json if json.isString =>
        json.asString
          .map(s => {
            decode[NativeResponse](s)
              .map(AdmOneof.AdmNative) // the string can be decoded as native
              .getOrElse(AdmOneof.Adm(s)) // the string cannot be decoded
          })
          .getOrElse(AdmOneof.Empty) // there's no string or it's impossible to decode it
      case json if json.isObject =>
        json.as[NativeResponse].map(AdmOneof.AdmNative).getOrElse(AdmOneof.Empty)
    }.orElse(Some(AdmOneof.Empty)).map(Right(_)).get
  }