Add real-time WebSocket support - #2
Merged
Merged
Conversation
Sync with python-client 0.3.0. Adds the three query-management endpoints (websocket/register, /fetch, /delete) and NewsDataApiWebSocket, which streams the news matching a registered query via a handler predicate. Built on Java 11's java.net.http.WebSocket, so no new dependency. The server accepts every handshake and then closes with code 1008 on a permanent rejection (invalid credentials, api limit, device limit); those throw NewsdataWebSocketAuthException and are never retried. request() now carries a per-endpoint HTTP method, and the websocket endpoints are exempt from the results-present success check. Tests run against a minimal RFC 6455 mock server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Syncs this client with python-client 0.3.0 (PR #27), which added the real-time WebSocket service.
What's here
Query management — three REST endpoints, verified against the OpenAPI 3.1 spec:
websocket/registerPOST(query params, no body)websocket/fetchGETwebsocket/deleteDELETERegister once, stream many times. A registered query has no date or paging filters — it matches news as it is published. Registering identical filters twice answers
409with the existingregistration_id.Streaming over
wss://ws.newsdata.io/ws/event?apikey=…®istration_id=…, with a capped exponential backoff (1s → 30s) and malformed frames skipped.Failure model
Per the spec, the handshake is always accepted; a refused connection is then closed with code 1008 carrying one of three reasons —
invalid credentials or registration not found,api limit reached, ordevice limit reached(>5 devices on oneregistration_id). Those raise the new typed auth error and are never retried. Every other close code, including1013(send timeout— the client read too slowly), is transient and reconnects.Shared plumbing
The HTTP layer gained a per-endpoint method (it was GET-only), and the websocket endpoints are exempt from the results-present success check since their success envelope may omit
results. Both touch the request path used by every endpoint — worth a look.Java specifics
No new dependency — built on Java 11’s
java.net.http.WebSocket.stream(id, handler)blocks; the handler returnstrueto continue,falseto stop.AutoCloseable, so try-with-resources ends an in-flight stream.Tests run against a minimal RFC 6455 mock server (server→client frames are unmasked, so it stays short). 50 tests pass.