Skip to content

Commit

Permalink
Merge pull request #62 from ecederstrand/patch-1
Browse files Browse the repository at this point in the history
Allow customizing the 'User-Agent' header
  • Loading branch information
yusra-ahmed-zz authored Mar 10, 2021
2 parents c7b34c2 + b5700d1 commit 464307e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions exchangelib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions exchangelib/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 464307e

Please sign in to comment.