-
Notifications
You must be signed in to change notification settings - Fork 17
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
Improve API for bidi and server streaming calls #130
Changes from 4 commits
2678d89
aba7b5c
9ba17de
fdcd426
0e76254
b222897
d220eeb
d2c5400
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ | |
|
||
package com.connectrpc.examples.kotlin | ||
|
||
import com.connectrpc.Code | ||
import com.connectrpc.ConnectException | ||
import com.connectrpc.ProtocolClientConfig | ||
import com.connectrpc.eliza.v1.ElizaServiceClient | ||
|
@@ -63,23 +62,8 @@ class Main { | |
// Add the message the user is sending to the views. | ||
stream.send(converseRequest { sentence = "hello" }) | ||
stream.sendClose() | ||
for (streamResult in stream.resultChannel()) { | ||
streamResult.maybeFold( | ||
onMessage = { result -> | ||
// Update the view with the response. | ||
val elizaResponse = result.message | ||
println(elizaResponse.sentence) | ||
}, | ||
onCompletion = { result -> | ||
if (result.code != Code.OK) { | ||
val exception = result.connectException() | ||
if (exception != null) { | ||
throw exception | ||
} | ||
throw ConnectException(code = result.code, metadata = result.trailers) | ||
} | ||
}, | ||
) | ||
for (response in stream.responseChannel()) { | ||
println(response.sentence) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's an example of the improved API experience for callers. |
||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,11 +21,11 @@ import kotlinx.coroutines.channels.ReceiveChannel | |
*/ | ||
interface BidirectionalStreamInterface<Input, Output> { | ||
/** | ||
* The Channel for received StreamResults. | ||
* The Channel for responses. | ||
* | ||
* @return ReceiveChannel for iterating over the received results. | ||
* @return ReceiveChannel for iterating over the responses. | ||
*/ | ||
fun resultChannel(): ReceiveChannel<StreamResult<Output>> | ||
fun responseChannel(): ReceiveChannel<Output> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean consumers have no way of reading stream response headers? Where is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consumers can continue to access stream response headers from interceptors - I've opened #131 with some ideas on making it easier for people to access these based on patterns in connect-go. |
||
|
||
/** | ||
* Send a request to the server over the stream. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,9 +29,9 @@ interface ClientOnlyStreamInterface<Input, Output> { | |
/** | ||
* Receive a single response and close the stream. | ||
* | ||
* @return the single response [ResponseMessage]. | ||
* @return the single response [Output]. | ||
*/ | ||
suspend fun receiveAndClose(): ResponseMessage<Output> | ||
suspend fun receiveAndClose(): Output | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now all of the streaming interfaces work the same. |
||
|
||
/** | ||
* Close the stream. No calls to [send] are valid after calling [sendClose]. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now users don't need to handle this complexity to use bidi or server streaming calls.