Comments (4)
Hi @miguel-arrf,
any sort of live pub/sub system should be a good match for any of the three streaming formats we added built-in support for recently: JSON Lines, JSON Sequence, or Server-sent Events. Unless you need something more advanced, I think starting with JSON Lines is a good idea, as it's the simplest one.
Note that what's provided is just a way to produce/parse a stream of events, but as you point out, any logic to manage the connection and resumes, for example for a client that needs to handle such a stream in a HTTP response, would need to be written by you, wrapping the low-level generated calls to the server.
The idea would be, each event has a unique ID, you keep track of the latest ID you saw, and if the response stream gets interrupted, make the HTTP request again, sending the latest ID to the server, allowing the server to only replay what you missed since that ID.
So to answer your question: yes, the provided tools can be used as the low-level building blocks, but the stateful connection management needs to be built on top of it, as the details can differ between applications.
from swift-openapi-generator.
Great question, it's not immediately obvious.
For example, if you make a request to GET /stream
and get back 200 OK
immediately, with a content type application/jsonl
, you'd get the HTTPBody
type, which represents the response body stream.
As shown in the examples, you'd then turn it into a stream of parsed, deserialized events, so the full thing looks like this:
let response = try await client.getGreetingsStream(
query: .init(name: "Example", count: 3),
headers: .init(accept: [.init(contentType: .application_jsonl)])
)
let greetingStream = try response.ok.body.application_jsonl.asDecodedJSONLines(
of: Components.Schemas.Greeting.self
)
for try await greeting in greetingStream {
print("Got greeting: \(greeting.message)")
}
Now, if the connection gets interrupted, the for loop at the end is what I'd expect to throw an error. But the details depend on the ClientTransport
implementation you're using.
But if you wrap the for loop in a do/catch block, that at least signals to you when you need to re-establish the connection.
from swift-openapi-generator.
Hi @czechboy0!
Thank you so much for your detailed answer 🚀 😊 ! Just one more question:
if the response stream gets interrupted
How do I know if the stream got interrupted?
(I haven't yet run the example code as I'm still understanding my options. Maybe if I have had run it I would already have the answer for this 😓 )
from swift-openapi-generator.
Awesome, thank you so much for all the help!!
from swift-openapi-generator.
Related Issues (20)
- Generated enums should conform to `CaseIterable` HOT 7
- Is it possible to use component refs in paths that accept multiple content types? HOT 8
- Multipart content not properly generated? HOT 11
- A way to group features or services to allow middleware to be applied to groups HOT 4
- Manual invocation of the generator and SPM warnings HOT 5
- Null property type with quotes not generated HOT 14
- Support for XML request body and response body HOT 7
- Support schemas.type: string, format: uri generates URL property not String HOT 3
- How to support OAuth2 Password flows? HOT 3
- allOf plus properties HOT 2
- OpenAPI shared components HOT 4
- bug/regression: latest yams results in incomplete generation of types HOT 10
- Logging and HTTPBody HOT 3
- Merging oneOf with many compatible enums of a single value HOT 10
- anyOf encoder does not include discriminator's `parameterName` leading to incorrect encoding HOT 2
- Is there a workaround to deal with slow compilation times for a very large openapi file? HOT 3
- Empty arrays are omitted from query params HOT 5
- Support for `multipart/mixed`? HOT 1
- When a delete path do not have responses content an error generated HOT 2
- Add ability to attach protocols to generated models HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swift-openapi-generator.