diff --git a/exchangelib/__init__.py b/exchangelib/__init__.py index cecb108..026f841 100644 --- a/exchangelib/__init__.py +++ b/exchangelib/__init__.py @@ -12,6 +12,7 @@ from .items import AcceptItem, TentativelyAcceptItem, DeclineItem, CalendarItem, CancelCalendarItem, Contact, \ DistributionList, Message, PostItem, Task from .properties import Body, HTMLBody, ItemId, Mailbox, Attendee, Room, RoomList, UID +from .protocol import BaseProtocol from .restriction import Q from .transport import BASIC, DIGEST, NTLM, GSSAPI from .version import Build, Version @@ -38,6 +39,9 @@ 'Build', 'Version', ] +# Set a default user agent, e.g. "exchangelib/3.1.1" +BaseProtocol.USERAGENT = "%s/%s (%s)" % (__name__, __version__) + def close_connections(): from .autodiscover import close_connections as close_autodiscover_connections diff --git a/exchangelib/protocol.py b/exchangelib/protocol.py index cdffe94..abbd51e 100644 --- a/exchangelib/protocol.py +++ b/exchangelib/protocol.py @@ -48,6 +48,8 @@ class BaseProtocol(object): CONNECTIONS_PER_SESSION = 1 # Timeout for HTTP requests TIMEOUT = 120 + # The User-Agent header to use for HTTP requests. Override this to set an app-specific one + USERAGENT = None # The adapter class to use for HTTP requests. Override this if you need e.g. proxy support or specific TLS versions HTTP_ADAPTER_CLS = requests.adapters.HTTPAdapter @@ -133,6 +135,7 @@ def create_session(self): session.auth = get_auth_instance(credentials=self.credentials, auth_type=self.auth_type) # Create a copy of the headers because headers are mutable and session users may modify headers session.headers.update(DEFAULT_HEADERS.copy()) + session.headers['User-Agent'] = self.USERAGENT session.mount('http://', adapter=self.get_adapter()) session.mount('https://', adapter=self.get_adapter()) log.debug('Server %s: Created session %s', self.server, session.session_id)