11import warnings
2- from typing import Optional
3- from typing import Union
4- from typing import cast
2+
3+ import importlib .metadata
4+
5+ from typing import Optional , Union , cast
56
67from pydantic import TypeAdapter
78
@@ -34,6 +35,10 @@ class MailtrapClient:
3435 DEFAULT_PORT = 443
3536 BULK_HOST = BULK_HOST
3637 SANDBOX_HOST = SANDBOX_HOST
38+ DEFAULT_USER_AGENT = (
39+ f"mailtrap-python/{ importlib .metadata .version ('mailtrap' )} "
40+ "(https://github.com/railsware/mailtrap-python)"
41+ )
3742
3843 def __init__ (
3944 self ,
@@ -44,6 +49,7 @@ def __init__(
4449 sandbox : bool = False ,
4550 account_id : Optional [str ] = None ,
4651 inbox_id : Optional [str ] = None ,
52+ user_agent : Optional [str ] = None ,
4753 ) -> None :
4854 self .token = token
4955 self .api_host = api_host
@@ -52,6 +58,9 @@ def __init__(
5258 self .sandbox = sandbox
5359 self .account_id = account_id
5460 self .inbox_id = inbox_id
61+ self ._user_agent = (
62+ user_agent if user_agent is not None else self .DEFAULT_USER_AGENT
63+ )
5564
5665 self ._validate_itself ()
5766
@@ -147,9 +156,7 @@ def headers(self) -> dict[str, str]:
147156 return {
148157 "Authorization" : f"Bearer { self .token } " ,
149158 "Content-Type" : "application/json" ,
150- "User-Agent" : (
151- "mailtrap-python (https://github.com/railsware/mailtrap-python)"
152- ),
159+ "User-Agent" : self ._user_agent ,
153160 }
154161
155162 @property
0 commit comments