Skip to content

AHC updates #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.macOS(.v14),
],
dependencies: [
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.9.0")
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.21.0")
],
targets: [
.executableTarget(
Expand Down
192 changes: 78 additions & 114 deletions Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,37 @@ struct Entrypoint {
try await example2()
try await example3()
}

// MARK: -

static func example1() async throws {

let httpClient = HTTPClient(
eventLoopGroupProvider: .singleton
)

do {
var request = HTTPClientRequest(
url: "https://httpbin.org/post"
)
request.method = .POST
request.headers.add(name: "User-Agent", value: "Swift AsyncHTTPClient")
request.body = .bytes(ByteBuffer(string: "Some data"))

let response = try await httpClient.execute(
request,
timeout: .seconds(5)
)

if response.status == .ok {
let contentType = response.headers.first(
name: "content-type"
)
print("\(contentType ?? "")")

let contentLength = response.headers.first(
name: "content-length"
).flatMap(Int.init)
print("\(contentLength ?? -1)")
var request = HTTPClientRequest(url: "https://httpbin.org/post")
request.method = .POST
request.headers.add(name: "User-Agent", value: "Swift AsyncHTTPClient")
request.body = .bytes(ByteBuffer(string: "Some data"))

let response = try await HTTPClient.shared.execute(
request,
timeout: .seconds(5)
)

let buffer = try await response.body.collect(upTo: 1024 * 1024)
let rawResponseBody = buffer.getString(
at: 0,
length: buffer.readableBytes
)
print("\(rawResponseBody ?? "")")
}
}
catch {
print("\(error)")
guard response.status == .ok else {
print("Invalid status code: \(response.status)")
return
}
let contentType = response.headers.first(name: "content-type")
print("\(contentType ?? "")")

let contentLength = response.headers.first(
name: "content-length"
).flatMap(Int.init)

try await httpClient.shutdown()
print("\(contentLength ?? -1)")

let buffer = try await response.body.collect(upTo: 1024 * 1024)
let rawResponseBody = String(buffer: buffer)
print("\(rawResponseBody)")
}

// MARK: -
Expand All @@ -71,94 +56,73 @@ struct Entrypoint {
let json: Input
}

let httpClient = HTTPClient(
eventLoopGroupProvider: .singleton
var request = HTTPClientRequest(url: "https://httpbin.org/post")
request.method = .POST
request.headers.add(name: "content-type", value: "application/json")

let input = Input(
id: 1,
title: "foo",
completed: false
)
do {
var request = HTTPClientRequest(
url: "https://httpbin.org/post"
)
request.method = .POST
request.headers.add(name: "content-type", value: "application/json")

let input = Input(
id: 1,
title: "foo",
completed: false
)

let encoder = JSONEncoder()
let data = try encoder.encode(input)
let buffer = ByteBuffer(bytes: data)
request.body = .bytes(buffer)

let response = try await httpClient.execute(
request,
timeout: .seconds(5)
)

if response.status == .ok {
if let contentType = response.headers.first(
name: "content-type"
), contentType.contains("application/json") {
var buffer: ByteBuffer = .init()
for try await var chunk in response.body {
buffer.writeBuffer(&chunk)
}

let decoder = JSONDecoder()
if let data = buffer.getData(at: 0, length: buffer.readableBytes) {
let output = try decoder.decode(Output.self, from: data)
print(output.json.title)
}
}

}
else {
print("Invalid status code: \(response.status)")
}

let encoder = JSONEncoder()
let data = try encoder.encode(input)
request.body = .bytes(.init(bytes: data))

let response = try await HTTPClient.shared.execute(
request,
timeout: .seconds(5)
)

guard response.status == .ok else {
print("Invalid status code: \(response.status)")
return
}
guard
let contentType = response.headers.first(name: "content-type"),
contentType.contains("application/json")
else {
print("Invalid content type.")
return
}
catch {
print("\(error)")

var buffer: ByteBuffer = .init()
for try await var chunk in response.body {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't even needed. You can just .collect the chunks into a single buffer with a NIO-provided helper on AsyncSequence

buffer.writeBuffer(&chunk)
}

try await httpClient.shutdown()
let decoder = JSONDecoder()
let output = try decoder.decode(Output.self, from: buffer)
print(output.json.title)
}

// MARK: -

static func example3() async throws {

let httpClient = HTTPClient(
eventLoopGroupProvider: .singleton
let delegate = try FileDownloadDelegate(
path: NSTemporaryDirectory() + "600x400.png",
reportProgress: {
if let totalBytes = $0.totalBytes {
print("Total: \(totalBytes).")
}
print("Downloaded: \($0.receivedBytes).")
}
)

do {
let delegate = try FileDownloadDelegate(
path: NSTemporaryDirectory() + "600x400.png",
reportProgress: {
if let totalBytes = $0.totalBytes {
print("Total: \(totalBytes).")
}
print("Downloaded: \($0.receivedBytes).")
}
)

let fileDownloadResponse = try await httpClient.execute(
request: .init(
url: "https://placehold.co/600x400.png"
),
delegate: delegate
).futureResult.get()

print(fileDownloadResponse)
}
catch {
print("\(error)")
}
let fileDownloadResponse = try await HTTPClient.shared.execute(
request: .init(url: "https://placehold.co/600x400.png"),
delegate: delegate
).futureResult.get()

try await httpClient.shutdown()
print(fileDownloadResponse)
}
}

try await Entrypoint.main()
do {
try await Entrypoint.main()
}
catch {
fatalError("\(error)")
}