Skip to content

Instantly share code, notes, and snippets.

@JonNorman
Created February 8, 2017 17:34
Show Gist options
  • Save JonNorman/d3dcd54e4e161e645b63887e67aacf4f to your computer and use it in GitHub Desktop.
Save JonNorman/d3dcd54e4e161e645b63887e67aacf4f to your computer and use it in GitHub Desktop.
Paginated DFP Ad Unit PQL request
import com.google.api.ads.dfp.axis.factory.DfpServices
import com.google.api.ads.dfp.axis.utils.v201608.{DateTimes, StatementBuilder}
import com.google.api.ads.dfp.axis.v201608.{AdUnitPage, InventoryServiceInterface}
def run(dfpSession: DfpSession): Unit = {
// Construct DFP Services
val dfpServices = new DfpServices
val adUnitService: InventoryServiceInterface = dfpServices.get(dfpSession, classOf[InventoryServiceInterface])
@tailrec
def processPage(query: StatementBuilder, contents: List[String] = Nil): List[String] = {
val page: AdUnitPage = adUnitService.getAdUnitsByStatement(query.toStatement)
val pageContents: List[String] = Option(page.getResults) match {
case None => Logger.info(s"No results found!"); Nil
case Some(results) => results.toList.map(r => Json.toJson(r).toString)
}
query.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT)
val contentsSoFar = contents ++ pageContents
if (query.getOffset < page.getTotalResultSetSize){
processPage(query, contentsSoFar)
} else {
contentsSoFar
}
}
val statementForEverything: StatementBuilder =
new StatementBuilder()
.orderBy("lastModifiedDateTime ASC")
.limit(StatementBuilder.SUGGESTED_PAGE_LIMIT)
val contents: List[String] = processPage(statementForEverything)
Logger.info(s"Processed ${contents.size} Ad Units")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment