Skip to content
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

Add bodyParts computed prop to StreamingClientResponse #2184

Merged
merged 5 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 16 additions & 1 deletion Sources/GRPCCore/Call/Client/ClientResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ extension StreamingClientResponse {

/// Returns the messages received from the server.
///
/// For rejected RPCs the `RPCAsyncSequence` throws a `RPCError``.
/// For rejected RPCs (in other words, where ``accepted`` is `failure`), the `RPCAsyncSequence` throws a `RPCError``.
gjcairo marked this conversation as resolved.
Show resolved Hide resolved
public var messages: RPCAsyncSequence<Message, any Error> {
switch self.accepted {
case let .success(contents):
Expand All @@ -382,4 +382,19 @@ extension StreamingClientResponse {
return RPCAsyncSequence.throwing(error)
}
}

/// Returns the body parts (i.e. `messages` and `trailingMetadata`) returned from the server.
///
/// For rejected RPCs (in other words, where ``accepted`` is `failure`), the `RPCAsyncSequence` throws a `RPCError``.
gjcairo marked this conversation as resolved.
Show resolved Hide resolved
public var bodyParts: RPCAsyncSequence<Contents.BodyPart, any Error> {
switch self.accepted {
case let .success(contents):
return contents.bodyParts

case let .failure(error):
return RPCAsyncSequence.throwing(error)
}
}
}

extension StreamingClientResponse.Contents.BodyPart: Equatable where Message: Equatable {}
30 changes: 29 additions & 1 deletion Tests/GRPCCoreTests/Call/Client/ClientResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class ClientResponseTests: XCTestCase {
XCTAssertEqual(response.trailingMetadata, ["bar": "baz"])
}

func testAcceptedStreamResponseConvenienceMethods() async throws {
func testAcceptedStreamResponseConvenienceMethods_Messages() async throws {
let response = StreamingClientResponse(
of: String.self,
metadata: ["foo": "bar"],
Expand All @@ -73,6 +73,29 @@ final class ClientResponseTests: XCTestCase {
XCTAssertEqual(messages, ["foo", "bar", "baz"])
}

func testAcceptedStreamResponseConvenienceMethods_BodyParts() async throws {
let response = StreamingClientResponse(
of: String.self,
metadata: ["foo": "bar"],
bodyParts: RPCAsyncSequence(
wrapping: AsyncThrowingStream {
$0.yield(.message("foo"))
$0.yield(.message("bar"))
$0.yield(.message("baz"))
$0.yield(.trailingMetadata(["baz": "baz"]))
$0.finish()
}
)
)

XCTAssertEqual(response.metadata, ["foo": "bar"])
let bodyParts = try await response.bodyParts.collect()
XCTAssertEqual(
bodyParts,
[.message("foo"), .message("bar"), .message("baz"), .trailingMetadata(["baz": "baz"])]
)
}

func testRejectedStreamResponseConvenienceMethods() async throws {
let error = RPCError(code: .aborted, message: "error message", metadata: ["bar": "baz"])
let response = StreamingClientResponse(of: String.self, error: error)
Expand All @@ -83,6 +106,11 @@ final class ClientResponseTests: XCTestCase {
} errorHandler: {
XCTAssertEqual($0, error)
}
await XCTAssertThrowsRPCErrorAsync {
try await response.bodyParts.collect()
} errorHandler: {
XCTAssertEqual($0, error)
}
}

func testStreamToSingleConversionForValidStream() async throws {
Expand Down
Loading