Skip to content

Commit d436be5

Browse files
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>
1 parent bad39b4 commit d436be5

File tree

2 files changed

+63
-50
lines changed

2 files changed

+63
-50
lines changed

Examples/PrintPDF/app.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

Examples/PrintPDF/main.swift

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)