// Network printers by IPP, the protocol behind AirPrint, Mopria and CUPS: the request asking a
// printer about itself, a print job's request with duplex and A4, and a printer's answer
// (saved from an office laser printer) read into a summary.
//
//   quillo run ipp.qwe      then open http://localhost:8080/answer and http://localhost:8080/requests
//   PRINTER_IPP=ipp://192.168.1.70/ipp/print quillo run ipp.qwe      and http://localhost:8080/live

use "ipp.qwer"

route GET /answer {
    let a = ipp_parse_file("ipp_sample.bin")?
    { status: a.status, summary: a.summary, skipped: a.skipped, resolution: a.groups[1].attributes["printer-resolution-default"], copies: a.groups[1].attributes["copies-supported"] }
}

route GET /requests = {
    ask: ipp_request("get-printer-attributes", "ipp://printer.local/ipp/print", { requested: ["printer-state", "printer-state-reasons", "media-supported"] })?,
    job: slice(ipp_request("print-job", "ipps://printer.local/ipp/print", { name: "Invoice 1042", format: "application/pdf", copies: 2, sides: "long", media: "a4", color: false })?, 0, 60),
    cancel: ipp_request("cancel-job", "ipp://printer.local:8631/printers/office", { job_id: 17 })?,
    bad: ipp_request("print-job", "ftp://printer.local", {}).error,
}

route GET /live {
    let url = env("PRINTER_IPP", null) ?? reply 503 "set PRINTER_IPP to the printer's address, like ipp://192.168.1.70/ipp/print"
    let p = ipp_printer(url, 5000)?
    { printer: p.summary, jobs: ipp_jobs(url, "not-completed", 5000)? }
}
