Introduce a synchronous web socket interface #1913
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Since thanks to @benzwreck we now have websocket support in the default backend, below is a proposition to improve the "user experience" when using synchronous web sockets.
Of course, synchronous web sockets can be used with the existing API, but the user than has to deal with the
Identity
pseudo-monad (type alias), which wraps all results. This is visible in theWebSocketSynchronous
example, where the request looks as follows (notice the type parameters, which are necessary for the example to compile):This can be improved by extracting the web socket API into separate traits, that is extracting from
SttpApi
the following three traits:SttpWebSocketAsyncApi
(what we have today),SttpWebSocketSyncApi
andSttpWebSocketStreamApi
.The sync version is a specialized variant of the async one, where each
F[_]
is ommited and internally replaced withIdentity
. Additionally, we can introduce aSyncWebSocket
wrapper forWebSocket
, which has type signatures without theF
/Identity
wrapper, i.e.:This might be clearer when discovering the API. Summarizing the upsides:
(+) straightforward types when using web sockets in typical scenarios
(+) no
F[_]
in the synchronous APIBut there are downsides as well:
(-) instead of the single
import sttp.client4._
, which brings in all the response specifications, users now how to also import the web socket api, i.e.import sttp.client4.ws.sync._
orimport sttp.client4.ws.async._
(-) import suggestions for
asWebSocket
now yield two variants (sync and async)(-) the
Identity
type alias still leaks in some places, e.g. in the types of response specifications returned by the api:def asWebSocket[T](f: SyncWebSocket => T): WebSocketResponseAs[Identity, Either[String, T]]
What do you think?
cc @szymon-rd, @bishabosha, @adpi2 (I think you wanted to do sth similar in the original sttp4 refactoring?)
Example code using the API (same as in examples):