newsdataapi 0.3.4__tar.gz → 0.3.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/PKG-INFO +1 -1
  2. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/src/newsdataapi/__init__.py +1 -1
  3. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/src/newsdataapi/websocket.py +53 -16
  4. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/tests/test_websocket.py +137 -8
  5. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/.github/workflows/ci.yml +0 -0
  6. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/.github/workflows/publish.yml +0 -0
  7. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/.gitignore +0 -0
  8. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/.python-version +0 -0
  9. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/LICENSE +0 -0
  10. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/README.md +0 -0
  11. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/examples/basic_usage.py +0 -0
  12. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/examples/websocket_usage.py +0 -0
  13. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/newsdata-logo.png +0 -0
  14. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/pyproject.toml +0 -0
  15. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/src/newsdataapi/client.py +0 -0
  16. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/src/newsdataapi/constants.py +0 -0
  17. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/src/newsdataapi/csv_export.py +0 -0
  18. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/src/newsdataapi/exceptions.py +0 -0
  19. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/src/newsdataapi/py.typed +0 -0
  20. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/tests/__init__.py +0 -0
  21. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/tests/conftest.py +0 -0
  22. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/tests/test_integration.py +0 -0
  23. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/tests/test_unit.py +0 -0
  24. {newsdataapi-0.3.4 → newsdataapi-0.3.5}/uv.lock +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: newsdataapi
3
- Version: 0.3.4
3
+ Version: 0.3.5
4
4
  Summary: Official Python client (SDK) for the Newsdata.io News API — fetch real-time, historical, crypto, and stock-market news via REST, with validation, retries, pagination, and typed exceptions.
5
5
  Project-URL: Homepage, https://newsdata.io
6
6
  Project-URL: Documentation, https://newsdata.io/documentation
@@ -17,7 +17,7 @@ from .exceptions import (
17
17
  )
18
18
  from .websocket import NewsDataApiWebSocket
19
19
 
20
- __version__ = "0.3.4"
20
+ __version__ = "0.3.5"
21
21
 
22
22
  __all__ = [
23
23
  "NewsDataApiClient",
@@ -30,7 +30,7 @@ _WEBSOCKET_SUPPORTED = sys.version_info >= (3, 10)
30
30
 
31
31
  if _WEBSOCKET_SUPPORTED:
32
32
  from websockets.asyncio import client as _asyncio_client
33
- from websockets.exceptions import ConnectionClosedError, InvalidStatus
33
+ from websockets.exceptions import ConnectionClosedError, InvalidMessage, InvalidStatus
34
34
  from websockets.sync import client as _sync_client
35
35
 
36
36
  logger = logging.getLogger(__name__)
@@ -44,13 +44,16 @@ def _check_registration_id(registration_id: str) -> None:
44
44
  )
45
45
 
46
46
 
47
- def _permanent_auth_error(exc: Exception) -> NewsdataWebSocketAuthError | None:
47
+ def _permanent_auth_error(
48
+ exc: Exception, error_response: dict[str, Any] | None
49
+ ) -> NewsdataWebSocketAuthError | None:
48
50
  """Return the auth error to raise if ``exc`` is a permanent rejection.
49
51
 
50
52
  Returns ``None`` for transient failures (which the caller reconnects on):
51
53
  handshake 401/403 and policy-violation close 1008 are permanent; anything
52
54
  else (other handshake status, other close codes, network ``OSError``) is
53
- transient.
55
+ transient. ``error_response`` is the error response the server sent just
56
+ before closing, if any; it becomes the exception's argument.
54
57
  """
55
58
  if isinstance(exc, InvalidStatus):
56
59
  if exc.response.status_code in (401, 403):
@@ -59,14 +62,20 @@ def _permanent_auth_error(exc: Exception) -> NewsdataWebSocketAuthError | None:
59
62
  if isinstance(exc, ConnectionClosedError):
60
63
  close = exc.rcvd
61
64
  if close is not None and close.code == constants.WS_POLICY_VIOLATION:
65
+ if error_response is not None:
66
+ return NewsdataWebSocketAuthError(error_response)
62
67
  return NewsdataWebSocketAuthError(close.reason or "connection rejected")
63
68
  return None
64
69
  return None
65
70
 
66
71
 
67
- def _transient_error(exc: Exception) -> NewsdataWebSocketError:
72
+ def _transient_error(
73
+ exc: Exception, error_response: dict[str, Any] | None
74
+ ) -> NewsdataWebSocketError:
68
75
  """Wrap a transient failure as a :class:`NewsdataWebSocketError` (used only
69
76
  when ``reconnect=False`` so the caller stops instead of retrying)."""
77
+ if error_response is not None:
78
+ return NewsdataWebSocketError(error_response)
70
79
  if isinstance(exc, InvalidStatus):
71
80
  return NewsdataWebSocketError(f"handshake failed (HTTP {exc.response.status_code})")
72
81
  if isinstance(exc, ConnectionClosedError):
@@ -295,24 +304,38 @@ class NewsDataApiWebSocket:
295
304
  url = self._url(registration_id)
296
305
  delay = self._reconnect_delay
297
306
  while True:
307
+ error_response: dict[str, Any] | None = None
298
308
  try:
299
309
  with _sync_client.connect(url, **self._connect_kwargs()) as websocket:
300
310
  self._ws_sync = websocket
301
311
  logger.info("connected to %s", _redact_url(url))
302
- delay = self._reconnect_delay # reset after a successful connect
312
+ # reset after a successful connect, unless the server refuses it
313
+ refused_delay, delay = delay, self._reconnect_delay
303
314
  for message in websocket:
304
315
  try:
305
316
  response = json.loads(message)
306
317
  except ValueError:
307
318
  continue # skip malformed frames
308
- if isinstance(response, dict):
309
- yield response
310
- except (InvalidStatus, ConnectionClosedError, OSError) as exc:
311
- auth = _permanent_auth_error(exc)
319
+ if not isinstance(response, dict):
320
+ continue
321
+ if response.get("status") == "error":
322
+ # sent right before the server closes the connection:
323
+ # raise it with that close, and keep backing off
324
+ error_response, delay = response, refused_delay
325
+ continue
326
+ yield response
327
+ except (
328
+ InvalidStatus,
329
+ InvalidMessage, # connection lost during the handshake
330
+ ConnectionClosedError,
331
+ OSError,
332
+ asyncio.TimeoutError, # not an OSError on Python 3.10
333
+ ) as exc:
334
+ auth = _permanent_auth_error(exc, error_response)
312
335
  if auth is not None:
313
336
  raise auth from exc
314
337
  if not self._reconnect:
315
- raise _transient_error(exc) from exc
338
+ raise _transient_error(exc, error_response) from exc
316
339
  logger.warning(
317
340
  "connection failed (%s); reconnecting in %.1fs", exc, delay
318
341
  )
@@ -330,26 +353,40 @@ class NewsDataApiWebSocket:
330
353
  url = self._url(registration_id)
331
354
  delay = self._reconnect_delay
332
355
  while True:
356
+ error_response: dict[str, Any] | None = None
333
357
  try:
334
358
  async with _asyncio_client.connect(
335
359
  url, **self._connect_kwargs()
336
360
  ) as websocket:
337
361
  self._ws_async = websocket
338
362
  logger.info("connected to %s", _redact_url(url))
339
- delay = self._reconnect_delay # reset after a successful connect
363
+ # reset after a successful connect, unless the server refuses it
364
+ refused_delay, delay = delay, self._reconnect_delay
340
365
  async for message in websocket:
341
366
  try:
342
367
  response = json.loads(message)
343
368
  except ValueError:
344
369
  continue # skip malformed frames
345
- if isinstance(response, dict):
346
- yield response
347
- except (InvalidStatus, ConnectionClosedError, OSError) as exc:
348
- auth = _permanent_auth_error(exc)
370
+ if not isinstance(response, dict):
371
+ continue
372
+ if response.get("status") == "error":
373
+ # sent right before the server closes the connection:
374
+ # raise it with that close, and keep backing off
375
+ error_response, delay = response, refused_delay
376
+ continue
377
+ yield response
378
+ except (
379
+ InvalidStatus,
380
+ InvalidMessage, # connection lost during the handshake
381
+ ConnectionClosedError,
382
+ OSError,
383
+ asyncio.TimeoutError, # not an OSError on Python 3.10
384
+ ) as exc:
385
+ auth = _permanent_auth_error(exc, error_response)
349
386
  if auth is not None:
350
387
  raise auth from exc
351
388
  if not self._reconnect:
352
- raise _transient_error(exc) from exc
389
+ raise _transient_error(exc, error_response) from exc
353
390
  logger.warning(
354
391
  "connection failed (%s); reconnecting in %.1fs", exc, delay
355
392
  )
@@ -12,7 +12,7 @@ import json
12
12
 
13
13
  import pytest
14
14
  from websockets.datastructures import Headers
15
- from websockets.exceptions import ConnectionClosedError, InvalidStatus
15
+ from websockets.exceptions import ConnectionClosedError, InvalidMessage, InvalidStatus
16
16
  from websockets.frames import Close
17
17
  from websockets.http11 import Response
18
18
 
@@ -39,6 +39,11 @@ def _news(*titles: str) -> str:
39
39
  )
40
40
 
41
41
 
42
+ def _error(code: str, message: str = "rejected") -> str:
43
+ """An error frame, shaped like the server's error response."""
44
+ return json.dumps({"status": "error", "results": {"message": message, "code": code}})
45
+
46
+
42
47
  def _closed(code: int, reason: str = "") -> ConnectionClosedError:
43
48
  return ConnectionClosedError(Close(code, reason), None)
44
49
 
@@ -47,6 +52,11 @@ def _invalid_status(status: int) -> InvalidStatus:
47
52
  return InvalidStatus(Response(status, "x", Headers(), b""))
48
53
 
49
54
 
55
+ # Terminals the fakes raise from connect() itself (handshake / network
56
+ # failures) rather than during iteration.
57
+ _CONNECT_ERRORS = (InvalidStatus, InvalidMessage, OSError, asyncio.TimeoutError)
58
+
59
+
50
60
  class _FakeWS:
51
61
  """A scripted sync connection: yields ``messages`` then raises ``terminal``
52
62
  (a ConnectionClosedError) or ends normally if ``terminal`` is None."""
@@ -75,9 +85,10 @@ class _FakeWS:
75
85
  class _FakeConnect:
76
86
  """Scripted ``connect``: each attempt is ``(messages, terminal)``.
77
87
 
78
- ``terminal`` raised from ``connect()`` itself if it is an ``InvalidStatus``
79
- or ``OSError`` (handshake / network failure), otherwise during iteration.
80
- Once the script is exhausted it closes with 1008 to stop any reconnect loop.
88
+ ``terminal`` raised from ``connect()`` itself if it is one of
89
+ ``_CONNECT_ERRORS`` (handshake / network failure), otherwise during
90
+ iteration. Once the script is exhausted it closes with 1008 to stop any
91
+ reconnect loop.
81
92
  """
82
93
 
83
94
  def __init__(self, *attempts: tuple[list, BaseException | None]) -> None:
@@ -95,7 +106,7 @@ class _FakeConnect:
95
106
  messages, terminal = self._attempts.pop(0)
96
107
  else:
97
108
  messages, terminal = [], _closed(1008, "exhausted")
98
- if isinstance(terminal, (InvalidStatus, OSError)):
109
+ if isinstance(terminal, _CONNECT_ERRORS):
99
110
  raise terminal
100
111
  conn = _FakeWS(messages, terminal)
101
112
  self.conns.append(conn)
@@ -239,6 +250,18 @@ def test_close_1008_raises_auth_with_reason(monkeypatch: pytest.MonkeyPatch) ->
239
250
  assert fake.calls == 1
240
251
 
241
252
 
253
+ def test_error_response_raises_auth_with_it(monkeypatch: pytest.MonkeyPatch) -> None:
254
+ error = _error("ApiLimitExceeded", "out of credits")
255
+ fake = _FakeConnect(([_news("A"), error], _closed(1008, "ApiLimitExceeded")))
256
+ monkeypatch.setattr(CONNECT, fake)
257
+ got: list[str] = []
258
+ with pytest.raises(NewsdataWebSocketAuthError) as exc_info:
259
+ _collect(_ws(), got) # reconnect=True default
260
+ assert got == ["A"] # the error response is raised, never yielded
261
+ assert exc_info.value.args[0] == json.loads(error)
262
+ assert fake.calls == 1
263
+
264
+
242
265
  # --- reconnect=False: stop on first drop ------------------------------------
243
266
 
244
267
 
@@ -269,6 +292,17 @@ def test_no_reconnect_handshake_5xx_raises(monkeypatch: pytest.MonkeyPatch) -> N
269
292
  assert not isinstance(exc_info.value, NewsdataWebSocketAuthError)
270
293
 
271
294
 
295
+ def test_no_reconnect_server_error_response_raises(monkeypatch: pytest.MonkeyPatch) -> None:
296
+ error = _error("ServerError", "try again later")
297
+ fake = _FakeConnect(([error], _closed(1011, "ServerError")))
298
+ monkeypatch.setattr(CONNECT, fake)
299
+ with pytest.raises(NewsdataWebSocketError) as exc_info:
300
+ list(_ws(reconnect=False).stream("r"))
301
+ assert not isinstance(exc_info.value, NewsdataWebSocketAuthError)
302
+ assert exc_info.value.args[0] == json.loads(error)
303
+ assert fake.calls == 1
304
+
305
+
272
306
  # --- reconnect=True: retry transient drops ----------------------------------
273
307
 
274
308
 
@@ -324,6 +358,40 @@ def test_reconnects_on_network_error(monkeypatch: pytest.MonkeyPatch) -> None:
324
358
  assert fake.calls == 2
325
359
 
326
360
 
361
+ def test_reconnects_on_handshake_connection_lost(monkeypatch: pytest.MonkeyPatch) -> None:
362
+ fake = _FakeConnect(
363
+ ([], InvalidMessage("did not receive a valid HTTP response")), # lost mid-handshake
364
+ ([_news("A")], _closed(1008, "stop")),
365
+ )
366
+ monkeypatch.setattr(CONNECT, fake)
367
+ got: list[str] = []
368
+ with pytest.raises(NewsdataWebSocketAuthError):
369
+ _collect(_ws(), got)
370
+ assert got == ["A"]
371
+ assert fake.calls == 2
372
+
373
+
374
+ def test_server_error_responses_keep_backing_off(monkeypatch: pytest.MonkeyPatch) -> None:
375
+ slept: list[float] = []
376
+ monkeypatch.setattr(
377
+ "newsdataapi.websocket.time.sleep", lambda d: slept.append(d)
378
+ )
379
+ refused = ([_error("ServerError")], _closed(1011, "ServerError"))
380
+ fake = _FakeConnect(
381
+ refused,
382
+ refused,
383
+ refused,
384
+ ([_news("A")], _closed(1013, "SendTimeout")), # accepted -> backoff starts over
385
+ ([_news("B")], _closed(1008, "stop")),
386
+ )
387
+ monkeypatch.setattr(CONNECT, fake)
388
+ got: list[str] = []
389
+ with pytest.raises(NewsdataWebSocketAuthError):
390
+ _collect(_ws(), got)
391
+ assert got == ["A", "B"] # error responses are never yielded
392
+ assert slept == [1.0, 2.0, 4.0, 1.0]
393
+
394
+
327
395
  # ===========================================================================
328
396
  # Async streaming (NewsDataApiWebSocket.stream_async)
329
397
  # ===========================================================================
@@ -373,8 +441,8 @@ class _FakeAsyncCM:
373
441
  class _FakeAsyncConnect:
374
442
  """Scripted async ``connect``: each attempt is ``(messages, terminal)``.
375
443
 
376
- An ``InvalidStatus`` / ``OSError`` terminal is raised from ``__aenter__``
377
- (handshake failure); otherwise it is raised during async iteration.
444
+ A ``_CONNECT_ERRORS`` terminal is raised from ``__aenter__`` (handshake
445
+ failure); otherwise it is raised during async iteration.
378
446
  """
379
447
 
380
448
  def __init__(self, *attempts: tuple[list, BaseException | None]) -> None:
@@ -392,7 +460,7 @@ class _FakeAsyncConnect:
392
460
  messages, terminal = self._attempts.pop(0)
393
461
  else:
394
462
  messages, terminal = [], _closed(1008, "exhausted")
395
- if isinstance(terminal, (InvalidStatus, OSError)):
463
+ if isinstance(terminal, _CONNECT_ERRORS):
396
464
  return _FakeAsyncCM(None, terminal)
397
465
  conn = _FakeAsyncWS(messages, terminal)
398
466
  self.conns.append(conn)
@@ -502,6 +570,18 @@ def test_async_close_1008_raises_auth_with_reason(monkeypatch: pytest.MonkeyPatc
502
570
  assert fake.calls == 1
503
571
 
504
572
 
573
+ def test_async_error_response_raises_auth_with_it(monkeypatch: pytest.MonkeyPatch) -> None:
574
+ error = _error("ApiLimitExceeded", "out of credits")
575
+ fake = _FakeAsyncConnect(([_news("A"), error], _closed(1008, "ApiLimitExceeded")))
576
+ monkeypatch.setattr(CONNECT_ASYNC, fake)
577
+ got: list[str] = []
578
+ with pytest.raises(NewsdataWebSocketAuthError) as exc_info:
579
+ _acollect(_ws(), got) # reconnect=True default
580
+ assert got == ["A"] # the error response is raised, never yielded
581
+ assert exc_info.value.args[0] == json.loads(error)
582
+ assert fake.calls == 1
583
+
584
+
505
585
  # --- reconnect=False --------------------------------------------------------
506
586
 
507
587
 
@@ -542,3 +622,52 @@ def test_async_reconnects_on_network_error(monkeypatch: pytest.MonkeyPatch) -> N
542
622
  _acollect(_ws(), got)
543
623
  assert got == ["A"]
544
624
  assert fake.calls == 2
625
+
626
+
627
+ def test_async_reconnects_on_handshake_connection_lost(monkeypatch: pytest.MonkeyPatch) -> None:
628
+ fake = _FakeAsyncConnect(
629
+ ([], InvalidMessage("did not receive a valid HTTP response")), # lost mid-handshake
630
+ ([_news("A")], _closed(1008, "stop")),
631
+ )
632
+ monkeypatch.setattr(CONNECT_ASYNC, fake)
633
+ got: list[str] = []
634
+ with pytest.raises(NewsdataWebSocketAuthError):
635
+ _acollect(_ws(), got)
636
+ assert got == ["A"]
637
+ assert fake.calls == 2
638
+
639
+
640
+ def test_async_reconnects_on_open_timeout(monkeypatch: pytest.MonkeyPatch) -> None:
641
+ fake = _FakeAsyncConnect(
642
+ ([], asyncio.TimeoutError()), # handshake timeout; not an OSError on Python 3.10
643
+ ([_news("A")], _closed(1008, "stop")),
644
+ )
645
+ monkeypatch.setattr(CONNECT_ASYNC, fake)
646
+ got: list[str] = []
647
+ with pytest.raises(NewsdataWebSocketAuthError):
648
+ _acollect(_ws(), got)
649
+ assert got == ["A"]
650
+ assert fake.calls == 2
651
+
652
+
653
+ def test_async_server_error_responses_keep_backing_off(monkeypatch: pytest.MonkeyPatch) -> None:
654
+ slept: list[float] = []
655
+
656
+ async def record(delay: float) -> None:
657
+ slept.append(delay)
658
+
659
+ monkeypatch.setattr("newsdataapi.websocket.asyncio.sleep", record)
660
+ refused = ([_error("ServerError")], _closed(1011, "ServerError"))
661
+ fake = _FakeAsyncConnect(
662
+ refused,
663
+ refused,
664
+ refused,
665
+ ([_news("A")], _closed(1013, "SendTimeout")), # accepted -> backoff starts over
666
+ ([_news("B")], _closed(1008, "stop")),
667
+ )
668
+ monkeypatch.setattr(CONNECT_ASYNC, fake)
669
+ got: list[str] = []
670
+ with pytest.raises(NewsdataWebSocketAuthError):
671
+ _acollect(_ws(), got)
672
+ assert got == ["A", "B"] # error responses are never yielded
673
+ assert slept == [1.0, 2.0, 4.0, 1.0]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes