-
Notifications
You must be signed in to change notification settings - Fork 8
Make it possible to set a custom user agent #61
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import warnings | ||
| from typing import Optional | ||
| from typing import Union | ||
| from typing import cast | ||
|
|
||
| import importlib.metadata | ||
|
|
||
| from typing import Optional, Union, cast | ||
|
|
||
| from pydantic import TypeAdapter | ||
|
|
||
|
|
@@ -34,6 +35,10 @@ class MailtrapClient: | |
| DEFAULT_PORT = 443 | ||
| BULK_HOST = BULK_HOST | ||
| SANDBOX_HOST = SANDBOX_HOST | ||
| DEFAULT_USER_AGENT = ( | ||
| f"mailtrap-python/{importlib.metadata.version('mailtrap')} " | ||
| "(https://github.com/railsware/mailtrap-python)" | ||
| ) | ||
|
Comment on lines
+38
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🛡️ Proposed fallback+try:
+ _mailtrap_version = importlib.metadata.version("mailtrap")
+except importlib.metadata.PackageNotFoundError:
+ _mailtrap_version = "unknown"
+
class MailtrapClient:
...
DEFAULT_USER_AGENT = (
- f"mailtrap-python/{importlib.metadata.version('mailtrap')} "
+ f"mailtrap-python/{_mailtrap_version} "
"(https://github.com/railsware/mailtrap-python)"
)🤖 Prompt for AI Agents |
||
|
|
||
| def __init__( | ||
| self, | ||
|
|
@@ -44,6 +49,7 @@ def __init__( | |
| sandbox: bool = False, | ||
| account_id: Optional[str] = None, | ||
| inbox_id: Optional[str] = None, | ||
| user_agent: Optional[str] = None, | ||
| ) -> None: | ||
| self.token = token | ||
| self.api_host = api_host | ||
|
|
@@ -52,6 +58,9 @@ def __init__( | |
| self.sandbox = sandbox | ||
| self.account_id = account_id | ||
| self.inbox_id = inbox_id | ||
| self._user_agent = ( | ||
| user_agent if user_agent is not None else self.DEFAULT_USER_AGENT | ||
| ) | ||
|
|
||
| self._validate_itself() | ||
|
|
||
|
|
@@ -147,9 +156,7 @@ def headers(self) -> dict[str, str]: | |
| return { | ||
| "Authorization": f"Bearer {self.token}", | ||
| "Content-Type": "application/json", | ||
| "User-Agent": ( | ||
| "mailtrap-python (https://github.com/railsware/mailtrap-python)" | ||
| ), | ||
| "User-Agent": self._user_agent, | ||
| } | ||
|
|
||
| @property | ||
|
|
||
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.
Do we need a version though? I didn't see it advertised in our other SDKs. We'll need to update analytics to parse the version out for groupping