Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ print(result.json())

Stop writing `while` loops to fetch thousands of contacts. Use `.stream()` to return a native Python Generator. The SDK will automatically manage `Limit`, `Offset`, and network pagination under the hood.

Streaming continues until a short or empty page is returned. It does not use `Total` as the collection size, since normal REST responses report the number of elements in the current page.

```python
# Fetch all contacts seamlessly. Memory-safe and clean.
for contact in mailjet.contact.stream(chunk_size=500):
Expand Down
6 changes: 3 additions & 3 deletions mailjet_rest/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,9 @@ def stream(

yield from data

total = body.get("Total")
reached_total = isinstance(total, int) and (current_offset + len(data) >= total)
if not data or len(data) < chunk_size or reached_total:
# REST endpoints report a page-local Total unless countOnly is used.
# Continue through full pages rather than treating it as a collection size.
if not data or len(data) < chunk_size:
break

current_offset += chunk_size
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@
from mailjet_rest.endpoint import Endpoint


@pytest.mark.parametrize("offset", [0, 10])
@pytest.mark.parametrize("size", [3, 4])
@responses.activate
def test_stream_with_page_local_total(offset: int, size: int) -> None:
"""REST responses report Total for the current page, not the whole collection."""
data = [{"ID": index} for index in range(offset, offset + size)]
for start in range(0, size + 1, 2):
page = data[start : start + 2]
responses.add(
responses.GET,
"https://api.mailjet.com/v3/REST/contact",
match=[responses.matchers.query_param_matcher({"Offset": offset + start, "Limit": 2})],
json={"Count": len(page), "Total": len(page), "Data": page},
)

with Client(auth=("test", "test"), version="v3") as client:
assert list(client.contact.stream(filters={"Offset": offset}, chunk_size=2)) == data
assert len(responses.calls) == size // 2 + 1


@pytest.fixture
def client_offline() -> Client:
"""Local fixture to provide a basic Client instance."""
Expand Down
Loading