Skip to content
Open
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 src/apify_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from importlib import metadata

from ._apify_client import ApifyClient, ApifyClientAsync
from ._statistics import ClientStatistics

__version__ = metadata.version('apify-client')

__all__ = [
'ApifyClient',
'ApifyClientAsync',
'ClientStatistics',
'__version__',
]
12 changes: 12 additions & 0 deletions src/apify_client/_apify_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,19 @@ def send_request(self, *, method, url, headers, content, timeout, stream) -> Htt
if token is not None:
http_client.set_default_authorization(token)
instance._http_client = http_client
instance._statistics = http_client.statistics
return instance

@property
def token(self) -> str | None:
"""The Apify API token used by the client."""
return self._token

@property
def statistics(self) -> ClientStatistics:
"""Statistics about API client usage and rate limit errors."""
return self._statistics

@property
def http_client(self) -> HttpClient:
"""The HTTP client instance used for API communication.
Expand Down Expand Up @@ -660,13 +666,19 @@ async def send_request(self, *, method, url, headers, content, timeout, stream)
if token is not None:
http_client.set_default_authorization(token)
instance._http_client = http_client
instance._statistics = http_client.statistics
return instance

@property
def token(self) -> str | None:
"""The Apify API token used by the client."""
return self._token

@property
def statistics(self) -> ClientStatistics:
"""Statistics about API client usage and rate limit errors."""
return self._statistics

@property
def http_client(self) -> HttpClientAsync:
"""The HTTP client instance used for API communication.
Expand Down
3 changes: 3 additions & 0 deletions src/apify_client/_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from collections import defaultdict
from dataclasses import dataclass, field

from apify_client._docs import docs_group


@docs_group('Other')
@dataclass
class ClientStatistics:
"""Statistics about API client usage and rate limit errors."""
Expand Down
5 changes: 5 additions & 0 deletions src/apify_client/http_clients/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ def __init__(

self._headers = merge_headers(default_headers, headers)

@property
def statistics(self) -> ClientStatistics:
"""Statistics about API client usage and rate limit errors."""
return self._statistics

def set_default_authorization(self, token: str) -> None:
"""Set the `Authorization` header from the token, unless an authorization header is already configured.

Expand Down
96 changes: 95 additions & 1 deletion tests/unit/test_statistics.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest
from werkzeug import Response

import apify_client
from apify_client import ApifyClient, ApifyClientAsync, ClientStatistics
from apify_client.http_clients import ImpitHttpClient, ImpitHttpClientAsync

from apify_client._statistics import ClientStatistics
if TYPE_CHECKING:
from pytest_httpserver import HTTPServer


@pytest.mark.parametrize(
Expand Down Expand Up @@ -120,3 +128,89 @@ def test_add_rate_limit_error_large_attempt() -> None:

stats.add_rate_limit_error(10000)
assert stats.rate_limit_errors[9999] == 1


def test_client_statistics_is_exported() -> None:
"""Test that ClientStatistics is exported from the top-level apify_client package."""
assert 'ClientStatistics' in apify_client.__all__
assert apify_client.ClientStatistics is ClientStatistics


def test_apify_client_exposes_default_http_client_statistics(httpserver: HTTPServer) -> None:
"""Test that ApifyClient.statistics tracks calls and requests on the default HTTP client."""
httpserver.expect_request('/v2/logs/test-id').respond_with_response(
Response('ok', status=200, content_type='text/plain')
)
client = ApifyClient('test-token', api_url=httpserver.url_for('/'))

assert isinstance(client.statistics, ClientStatistics)
assert client.statistics is client.http_client.statistics
assert client.statistics.calls == 0
assert client.statistics.requests == 0

client.log('test-id').get()

assert client.statistics.calls == 1
assert client.statistics.requests == 1


def test_apify_client_exposes_custom_http_client_statistics(httpserver: HTTPServer) -> None:
"""Test that ApifyClient.with_custom_http_client points client.statistics to the custom HTTP client's stats."""
httpserver.expect_request('/v2/logs/test-id').respond_with_response(
Response('ok', status=200, content_type='text/plain')
)
custom_http_client = ImpitHttpClient()
client = ApifyClient.with_custom_http_client(
'test-token',
api_url=httpserver.url_for('/'),
http_client=custom_http_client,
)

assert client.statistics is custom_http_client.statistics
assert client.statistics.calls == 0
assert client.statistics.requests == 0

client.log('test-id').get()

assert client.statistics.calls == 1
assert client.statistics.requests == 1


async def test_apify_client_async_exposes_default_http_client_statistics(httpserver: HTTPServer) -> None:
"""Test that ApifyClientAsync.statistics tracks calls and requests on the default HTTP client."""
httpserver.expect_request('/v2/logs/test-id').respond_with_response(
Response('ok', status=200, content_type='text/plain')
)
client = ApifyClientAsync('test-token', api_url=httpserver.url_for('/'))

assert isinstance(client.statistics, ClientStatistics)
assert client.statistics is client.http_client.statistics
assert client.statistics.calls == 0
assert client.statistics.requests == 0

await client.log('test-id').get()

assert client.statistics.calls == 1
assert client.statistics.requests == 1


async def test_apify_client_async_exposes_custom_http_client_statistics(httpserver: HTTPServer) -> None:
"""Test that ApifyClientAsync.with_custom_http_client points client.statistics to the custom HTTP client's stats."""
httpserver.expect_request('/v2/logs/test-id').respond_with_response(
Response('ok', status=200, content_type='text/plain')
)
custom_http_client = ImpitHttpClientAsync()
client = ApifyClientAsync.with_custom_http_client(
'test-token',
api_url=httpserver.url_for('/'),
http_client=custom_http_client,
)

assert client.statistics is custom_http_client.statistics
assert client.statistics.calls == 0
assert client.statistics.requests == 0

await client.log('test-id').get()

assert client.statistics.calls == 1
assert client.statistics.requests == 1
Loading