|
| 1 | +import Foundation |
| 2 | +import IppClient |
| 3 | + |
| 4 | +@main |
| 5 | +struct App { |
| 6 | + static func main() async throws { |
| 7 | + let httpClient = HTTPClient(configuration: .init(certificateVerification: .none)) |
| 8 | + |
| 9 | + do { |
| 10 | + let printer = IppPrinter( |
| 11 | + httpClient: httpClient, |
| 12 | + uri: "ipps://macmini.not.local/printers/EPSON_XP_7100_Series" |
| 13 | + ) |
| 14 | + |
| 15 | + let attributesResponse = try await printer.getPrinterAttributes() |
| 16 | + |
| 17 | + if let printerName = attributesResponse[printer: \.printerName], |
| 18 | + let printerState = attributesResponse[printer: \.printerState], |
| 19 | + let printerStateReasons = attributesResponse[printer: \.printerStateReasons] |
| 20 | + { |
| 21 | + print("Printing on \(printerName) in state \(printerState), state reasons \(printerStateReasons)") |
| 22 | + } else { |
| 23 | + print("Could not read printer attributes, status code \(attributesResponse.statusCode)") |
| 24 | + } |
| 25 | + |
| 26 | + let pdf = try Data(contentsOf: URL(fileURLWithPath: "Examples/PrintPDF/hi_mom.pdf")) |
| 27 | + |
| 28 | + let response = try await printer.printJob( |
| 29 | + documentFormat: "application/pdf", |
| 30 | + data: .bytes(pdf) |
| 31 | + ) |
| 32 | + |
| 33 | + guard response.statusCode == .successfulOk, let jobId = response[job: \.jobId] else { |
| 34 | + print("Print job failed with status \(response.statusCode)") |
| 35 | + exit(1) |
| 36 | + } |
| 37 | + |
| 38 | + let job = printer.job(jobId) |
| 39 | + |
| 40 | + while true { |
| 41 | + let response = try await job.getJobAttributes(requestedAttributes: [.jobState]) |
| 42 | + guard let jobState = response[job: \.jobState] else { |
| 43 | + print("Failed to get job state") |
| 44 | + exit(1) |
| 45 | + } |
| 46 | + |
| 47 | + switch jobState { |
| 48 | + case .aborted, .canceled, .completed: |
| 49 | + print("Job ended with state \(jobState)") |
| 50 | + exit(0) |
| 51 | + default: |
| 52 | + print("Job state is \(jobState)") |
| 53 | + } |
| 54 | + |
| 55 | + try await Task.sleep(nanoseconds: 3_000_000_000) |
| 56 | + } |
| 57 | + } catch { |
| 58 | + print("Error: \(error)") |
| 59 | + } |
| 60 | + |
| 61 | + try await httpClient.shutdown() |
| 62 | + } |
| 63 | +} |
0 commit comments