-
Notifications
You must be signed in to change notification settings - Fork 8
Add Stats API #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
piobeny
wants to merge
3
commits into
main
Choose a base branch
from
stats-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Stats API #62
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import mailtrap as mt | ||
| from mailtrap.models.stats import SendingStatGroup | ||
| from mailtrap.models.stats import SendingStats | ||
| from mailtrap.models.stats import StatsFilterParams | ||
|
|
||
| API_TOKEN = "YOUR_API_TOKEN" | ||
| ACCOUNT_ID = "YOUR_ACCOUNT_ID" | ||
|
|
||
| client = mt.MailtrapClient(token=API_TOKEN) | ||
| stats_api = client.general_api.stats | ||
|
|
||
|
|
||
| def get_stats(account_id: int) -> SendingStats: | ||
| params = StatsFilterParams(start_date="2026-01-01", end_date="2026-01-31") | ||
| return stats_api.get(account_id=account_id, params=params) | ||
|
|
||
|
|
||
| def get_stats_by_domains(account_id: int) -> list[SendingStatGroup]: | ||
| params = StatsFilterParams(start_date="2026-01-01", end_date="2026-01-31") | ||
| return stats_api.by_domains(account_id=account_id, params=params) | ||
|
|
||
|
|
||
| def get_stats_by_categories(account_id: int) -> list[SendingStatGroup]: | ||
| params = StatsFilterParams(start_date="2026-01-01", end_date="2026-01-31") | ||
| return stats_api.by_categories(account_id=account_id, params=params) | ||
|
|
||
|
|
||
| def get_stats_by_email_service_providers(account_id: int) -> list[SendingStatGroup]: | ||
| params = StatsFilterParams(start_date="2026-01-01", end_date="2026-01-31") | ||
| return stats_api.by_email_service_providers(account_id=account_id, params=params) | ||
|
|
||
|
|
||
| def get_stats_by_date(account_id: int) -> list[SendingStatGroup]: | ||
| params = StatsFilterParams(start_date="2026-01-01", end_date="2026-01-31") | ||
| return stats_api.by_date(account_id=account_id, params=params) | ||
|
|
||
|
|
||
| def get_stats_with_filters(account_id: int) -> SendingStats: | ||
| params = StatsFilterParams( | ||
| start_date="2026-01-01", | ||
| end_date="2026-01-31", | ||
| sending_domain_ids=[1, 2], | ||
| sending_streams=["transactional"], | ||
| categories=["Transactional", "Marketing"], | ||
| email_service_providers=["Gmail", "Yahoo"], | ||
| ) | ||
| return stats_api.get(account_id=account_id, params=params) | ||
|
|
||
|
|
||
| def get_stats_by_domains_with_filters(account_id: int) -> list[SendingStatGroup]: | ||
| params = StatsFilterParams( | ||
| start_date="2026-01-01", | ||
| end_date="2026-01-31", | ||
| sending_streams=["transactional"], | ||
| categories=["Transactional"], | ||
| ) | ||
| return stats_api.by_domains(account_id=account_id, params=params) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(get_stats(ACCOUNT_ID)) | ||
| print(get_stats_by_domains(ACCOUNT_ID)) | ||
| print(get_stats_by_categories(ACCOUNT_ID)) | ||
| print(get_stats_by_email_service_providers(ACCOUNT_ID)) | ||
| print(get_stats_by_date(ACCOUNT_ID)) | ||
| print(get_stats_with_filters(ACCOUNT_ID)) | ||
| print(get_stats_by_domains_with_filters(ACCOUNT_ID)) | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.stats import SendingStatGroup | ||
| from mailtrap.models.stats import SendingStats | ||
| from mailtrap.models.stats import StatsFilterParams | ||
|
|
||
| GROUP_KEYS = { | ||
| "domains": "sending_domain_id", | ||
| "categories": "category", | ||
| "email_service_providers": "email_service_provider", | ||
| "date": "date", | ||
| } | ||
|
|
||
|
|
||
| class StatsApi: | ||
| def __init__(self, client: HttpClient) -> None: | ||
| self._client = client | ||
|
|
||
| def get(self, account_id: int, params: StatsFilterParams) -> SendingStats: | ||
| """Get aggregated sending stats.""" | ||
| response = self._client.get( | ||
| self._base_path(account_id), | ||
| params=params.api_query_params, | ||
| ) | ||
| return SendingStats(**response) | ||
|
|
||
| def by_domains( | ||
| self, account_id: int, params: StatsFilterParams | ||
| ) -> list[SendingStatGroup]: | ||
| """Get sending stats grouped by domains.""" | ||
| return self._grouped_stats(account_id, "domains", params) | ||
|
|
||
| def by_categories( | ||
| self, account_id: int, params: StatsFilterParams | ||
| ) -> list[SendingStatGroup]: | ||
| """Get sending stats grouped by categories.""" | ||
| return self._grouped_stats(account_id, "categories", params) | ||
|
|
||
| def by_email_service_providers( | ||
| self, account_id: int, params: StatsFilterParams | ||
| ) -> list[SendingStatGroup]: | ||
| """Get sending stats grouped by email service providers.""" | ||
| return self._grouped_stats(account_id, "email_service_providers", params) | ||
|
|
||
| def by_date( | ||
| self, account_id: int, params: StatsFilterParams | ||
| ) -> list[SendingStatGroup]: | ||
| """Get sending stats grouped by date.""" | ||
| return self._grouped_stats(account_id, "date", params) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def _grouped_stats( | ||
| self, account_id: int, group: str, params: StatsFilterParams | ||
| ) -> list[SendingStatGroup]: | ||
| response = self._client.get( | ||
| f"{self._base_path(account_id)}/{group}", params=params.api_query_params | ||
| ) | ||
| group_key = GROUP_KEYS[group] | ||
|
|
||
| return [ | ||
| SendingStatGroup( | ||
| name=group_key, | ||
| value=item[group_key], | ||
| stats=SendingStats(**item["stats"]), | ||
| ) | ||
| for item in response | ||
| ] | ||
|
|
||
| @staticmethod | ||
| def _base_path(account_id: int) -> str: | ||
| return f"/api/accounts/{account_id}/stats" | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| from typing import Optional | ||
| from typing import Union | ||
|
|
||
| from pydantic.dataclasses import dataclass | ||
|
|
||
| from mailtrap.models.common import RequestParams | ||
|
|
||
|
|
||
| @dataclass | ||
| class SendingStats: | ||
| delivery_count: int | ||
| delivery_rate: float | ||
| bounce_count: int | ||
| bounce_rate: float | ||
| open_count: int | ||
| open_rate: float | ||
| click_count: int | ||
| click_rate: float | ||
| spam_count: int | ||
| spam_rate: float | ||
|
|
||
|
|
||
| @dataclass | ||
| class SendingStatGroup: | ||
| name: str | ||
| value: Union[str, int] | ||
| stats: SendingStats | ||
|
|
||
|
|
||
| @dataclass | ||
| class StatsFilterParams(RequestParams): | ||
| start_date: Optional[str] = None | ||
| end_date: Optional[str] = None | ||
| sending_domain_ids: Optional[list[int]] = None | ||
| sending_streams: Optional[list[str]] = None | ||
| categories: Optional[list[str]] = None | ||
| email_service_providers: Optional[list[str]] = None |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type mismatch between
ACCOUNT_IDand function signatures.ACCOUNT_IDis defined as a string, but all helper functions declareaccount_id: int. Users copying this example may inadvertently pass a string when an integer is expected.Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.15.4)
[error] 6-6: Possible hardcoded password assigned to: "API_TOKEN"
(S105)
🤖 Prompt for AI Agents