From d436be5700ac70a362c287aa050d1496ff20b06a Mon Sep 17 00:00:00 2001 From: Ronny Bremer Date: Sat, 13 Jul 2024 14:25:05 +0200 Subject: [PATCH] shut down HTTPClient in example (#4) * HTTPClient needs to be shutdown after use to avoid memory leak See https://github.com/swift-server/async-http-client/blob/main/README.md#request-response-api * need to shutdown the HTTPClient before each exit call Not really recommended programming, but it is to demonstrate the shutdown of the HTTPClient before terminating the program. * use defer to shutdown the HTTPClient * http client shutdown in example --------- Co-authored-by: Simon Leeb <52261246+sliemeobn@users.noreply.github.com> --- Examples/PrintPDF/app.swift | 63 ++++++++++++++++++++++++++++++++++++ Examples/PrintPDF/main.swift | 50 ---------------------------- 2 files changed, 63 insertions(+), 50 deletions(-) create mode 100644 Examples/PrintPDF/app.swift delete mode 100644 Examples/PrintPDF/main.swift diff --git a/Examples/PrintPDF/app.swift b/Examples/PrintPDF/app.swift new file mode 100644 index 0000000..b934478 --- /dev/null +++ b/Examples/PrintPDF/app.swift @@ -0,0 +1,63 @@ +import Foundation +import IppClient + +@main +struct App { + static func main() async throws { + let httpClient = HTTPClient(configuration: .init(certificateVerification: .none)) + + do { + let printer = IppPrinter( + httpClient: httpClient, + uri: "ipps://macmini.not.local/printers/EPSON_XP_7100_Series" + ) + + let attributesResponse = try await printer.getPrinterAttributes() + + if let printerName = attributesResponse[printer: \.printerName], + let printerState = attributesResponse[printer: \.printerState], + let printerStateReasons = attributesResponse[printer: \.printerStateReasons] + { + print("Printing on \(printerName) in state \(printerState), state reasons \(printerStateReasons)") + } else { + print("Could not read printer attributes, status code \(attributesResponse.statusCode)") + } + + let pdf = try Data(contentsOf: URL(fileURLWithPath: "Examples/PrintPDF/hi_mom.pdf")) + + let response = try await printer.printJob( + documentFormat: "application/pdf", + data: .bytes(pdf) + ) + + guard response.statusCode == .successfulOk, let jobId = response[job: \.jobId] else { + print("Print job failed with status \(response.statusCode)") + exit(1) + } + + let job = printer.job(jobId) + + while true { + let response = try await job.getJobAttributes(requestedAttributes: [.jobState]) + guard let jobState = response[job: \.jobState] else { + print("Failed to get job state") + exit(1) + } + + switch jobState { + case .aborted, .canceled, .completed: + print("Job ended with state \(jobState)") + exit(0) + default: + print("Job state is \(jobState)") + } + + try await Task.sleep(nanoseconds: 3_000_000_000) + } + } catch { + print("Error: \(error)") + } + + try await httpClient.shutdown() + } +} diff --git a/Examples/PrintPDF/main.swift b/Examples/PrintPDF/main.swift deleted file mode 100644 index 80d264b..0000000 --- a/Examples/PrintPDF/main.swift +++ /dev/null @@ -1,50 +0,0 @@ -import Foundation -import IppClient - -let printer = IppPrinter( - httpClient: HTTPClient(configuration: .init(certificateVerification: .none)), - uri: "ipps://macmini.local/printers/EPSON_XP_7100_Series" -) - -let attributesResponse = try await printer.getPrinterAttributes() - -if let printerName = attributesResponse[printer: \.printerName], - let printerState = attributesResponse[printer: \.printerState], - let printerStateReasons = attributesResponse[printer: \.printerStateReasons] -{ - print("Printing on \(printerName) in state \(printerState), state reasons \(printerStateReasons)") -} else { - print("Could not read printer attributes, status code \(attributesResponse.statusCode)") -} - -let pdf = try Data(contentsOf: URL(fileURLWithPath: "Examples/PrintPDF/hi_mom.pdf")) - -let response = try await printer.printJob( - documentFormat: "application/pdf", - data: .bytes(pdf) -) - -guard response.statusCode == .successfulOk, let jobId = response[job: \.jobId] else { - print("Print job failed with status \(response.statusCode)") - exit(1) -} - -let job = printer.job(jobId) - -while true { - let response = try await job.getJobAttributes(requestedAttributes: [.jobState]) - guard let jobState = response[job: \.jobState] else { - print("Failed to get job state") - exit(1) - } - - switch jobState { - case .aborted, .canceled, .completed: - print("Job ended with state \(jobState)") - exit(0) - default: - print("Job state is \(jobState)") - } - - try await Task.sleep(nanoseconds: 3_000_000_000) -}