---
title: "Manipulating Orders and LineItems with rdfp"
author: "Steven M. Mortimer"
date: "2018-03-29"
output:
  rmarkdown::html_vignette:
    toc: true
    toc_depth: 4
    keep_md: true
vignette: >
  %\VignetteIndexEntry{Manipulating Orders and LineItems with rdfp}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, echo = FALSE}
NOT_CRAN <- identical(tolower(Sys.getenv("NOT_CRAN")), "true")
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  purl = NOT_CRAN,
  eval = NOT_CRAN
)
```

First, we load **rdfp** and specify the DFP network we would like to connect to. 
Then we authenticate by using `dfp_auth()`. Any existing cached token would be used 
or we will be prompted to authenticate via the browser.

```{r load-package, eval=FALSE}
library(rdfp)
options(rdfp.network_code = 123456789)
dfp_auth()
```

```{r auth, include = FALSE}
library(here)
library(rdfp)
token_path <- here::here("tests", "testthat", "rdfp_token.rds")
suppressMessages(dfp_auth(token = token_path, verbose = FALSE))
options_path <- here::here("tests", "testthat", "rdfp_options.rds")
rdfp_options <- readRDS(options_path)
options(rdfp.network_code = rdfp_options$network_code)
```

#### Create an Order

This example uses a test company as an advertiser and yourself as the trafficker, 
to create an order.

```{r eval=FALSE}
request_data <- list('filterStatement'=list('query'="WHERE name = 'TestCompany1'"))
dfp_getCompaniesByStatement_result <- dfp_getCompaniesByStatement(request_data) 

request_data <- list(list(name='TestOrder', 
                          startDateTime=list(date=list(year=2018, month=12, day=1), 
                                             hour=0,
                                             minute=0,
                                             second=0,
                                             timeZoneID='America/New_York'),
                          endDateTime=list(date=list(year=2018, month=12, day=31), 
                                           hour=23,
                                           minute=59,
                                           second=59,
                                           timeZoneID='America/New_York'), 
                          notes='API Test Order', 
                          externalOrderId=99999, 
                          advertiserId=dfp_getCompaniesByStatement_result$id, 
                          traffickerId=dfp_getCurrentUser()$id))
dfp_createOrders_result <- dfp_createOrders(request_data)
```

#### Get Line Items By A Filter

Below is an example of how to get objects by Publishers Query Language (PQL) statement.
The statement is constructed as a list of lists that are nested to emulate
the hierarchy of the XML to be created. The example uses the `dfp_getLineItemsByStatement`
function from the [LineItemService] (https://developers.google.com/ad-manager/api/reference/v201905/LineItemService)

```{r}
# retrieve 3 line items that have a status of "DELIVERING"
request <- list('filterStatement'=list('query'="WHERE status='DELIVERING' LIMIT 3"))
resultset <- dfp_getLineItemsByStatement(request, as_df=TRUE)
resultset[,c('orderId', 'id', 'priority', 'deliveryRateType')]
```
