1616from stringcase import pascalcase , camelcase , snakecase
1717from tzlocal import get_localzone
1818from zoneinfo import ZoneInfoNotFoundError , ZoneInfo
19- from .utils import ME_RESOURCE , BaseTokenBackend , FileSystemTokenBackend , Token
19+ from .utils import ME_RESOURCE , BaseTokenBackend , FileSystemTokenBackend , Token , get_windows_tz
2020import datetime as dt
2121
2222log = logging .getLogger (__name__ )
@@ -100,20 +100,12 @@ def __init__(self, *, protocol_url: Optional[str] = None,
100100 self .use_default_casing : bool = True if casing_function is None else False
101101 self .casing_function : Callable = casing_function or camelcase
102102
103+ # get_localzone() from tzlocal will try to get the system local timezone and if not will return UTC
104+ self ._timezone : ZoneInfo = get_localzone ()
105+
103106 if timezone :
104- if isinstance (timezone , str ):
105- # convert string to ZoneInfo
106- try :
107- timezone = ZoneInfo (timezone )
108- except ZoneInfoNotFoundError as e :
109- log .error (f'Timezone { timezone } could not be found.' )
110- raise e
111- else :
112- if not isinstance (timezone , ZoneInfo ):
113- raise ValueError (f'The timezone parameter must be either a string or a valid ZoneInfo instance.' )
107+ self .timezone = timezone # property setter will convert this timezone to ZoneInfo if a string is provided
114108
115- # get_localzone() from tzlocal will try to get the system local timezone and if not will return UTC
116- self .timezone : ZoneInfo = timezone or get_localzone ()
117109 self .max_top_value : int = 500 # Max $top parameter value
118110
119111 # define any keyword that can be different in this protocol
@@ -122,6 +114,28 @@ def __init__(self, *, protocol_url: Optional[str] = None,
122114 # outlook = #Microsoft.OutlookServices.FileAttachment')
123115 self .keyword_data_store : dict = {}
124116
117+ @property
118+ def timezone (self ):
119+ return self ._timezone
120+
121+ @timezone .setter
122+ def timezone (self , timezone : Union [str , ZoneInfo ]):
123+ self ._update_timezone (timezone )
124+
125+ def _update_timezone (self , timezone : Union [str , ZoneInfo ]):
126+ """Sets the timezone. This is not done in the setter as you can't call super from a overriden setter """
127+ if isinstance (timezone , str ):
128+ # convert string to ZoneInfo
129+ try :
130+ timezone = ZoneInfo (timezone )
131+ except ZoneInfoNotFoundError as e :
132+ log .error (f'Timezone { timezone } could not be found.' )
133+ raise e
134+ else :
135+ if not isinstance (timezone , ZoneInfo ):
136+ raise ValueError (f'The timezone parameter must be either a string or a valid ZoneInfo instance.' )
137+ self ._timezone = timezone
138+
125139 def get_service_keyword (self , keyword : str ) -> str :
126140 """ Returns the data set to the key in the internal data-key dict
127141
@@ -226,12 +240,16 @@ def __init__(self, api_version='v1.0', default_resource=None,
226240
227241 self .keyword_data_store ['message_type' ] = 'microsoft.graph.message'
228242 self .keyword_data_store ['event_message_type' ] = 'microsoft.graph.eventMessage'
229- self .keyword_data_store [
230- 'file_attachment_type' ] = '#microsoft.graph.fileAttachment'
231- self .keyword_data_store [
232- 'item_attachment_type' ] = '#microsoft.graph.itemAttachment'
243+ self .keyword_data_store ['file_attachment_type' ] = '#microsoft.graph.fileAttachment'
244+ self .keyword_data_store ['item_attachment_type' ] = '#microsoft.graph.itemAttachment'
245+ self .keyword_data_store ['prefer_timezone_header' ] = f'outlook.timezone="{ get_windows_tz (self ._timezone )} "'
233246 self .max_top_value = 999 # Max $top parameter value
234247
248+ @Protocol .timezone .setter
249+ def timezone (self , timezone : Union [str , ZoneInfo ]):
250+ super ()._update_timezone (timezone )
251+ self .keyword_data_store ['prefer_timezone_header' ] = f'outlook.timezone="{ get_windows_tz (self ._timezone )} "'
252+
235253
236254class MSOffice365Protocol (Protocol ):
237255 """ A Microsoft Office 365 Protocol Implementation
@@ -261,18 +279,18 @@ def __init__(self, api_version='v2.0', default_resource=None,
261279 protocol_scope_prefix = self ._oauth_scope_prefix ,
262280 ** kwargs )
263281
264- self .keyword_data_store [
265- 'message_type' ] = 'Microsoft.OutlookServices.Message'
266- self .keyword_data_store [
267- 'event_message_type' ] = 'Microsoft.OutlookServices.EventMessage'
268- self .keyword_data_store [
269- 'file_attachment_type' ] = '#Microsoft.OutlookServices.' \
270- 'FileAttachment'
271- self .keyword_data_store [
272- 'item_attachment_type' ] = '#Microsoft.OutlookServices.' \
273- 'ItemAttachment'
282+ self .keyword_data_store ['message_type' ] = 'Microsoft.OutlookServices.Message'
283+ self .keyword_data_store ['event_message_type' ] = 'Microsoft.OutlookServices.EventMessage'
284+ self .keyword_data_store ['file_attachment_type' ] = '#Microsoft.OutlookServices.FileAttachment'
285+ self .keyword_data_store ['item_attachment_type' ] = '#Microsoft.OutlookServices.ItemAttachment'
286+ self .keyword_data_store ['prefer_timezone_header' ] = f'outlook.timezone="{ get_windows_tz (self .timezone )} "'
274287 self .max_top_value = 999 # Max $top parameter value
275288
289+ @Protocol .timezone .setter
290+ def timezone (self , timezone : Union [str , ZoneInfo ]):
291+ super ()._update_timezone (timezone )
292+ self .keyword_data_store ['prefer_timezone_header' ] = f'outlook.timezone="{ get_windows_tz (self ._timezone )} "'
293+
276294
277295class MSBusinessCentral365Protocol (Protocol ):
278296 """ A Microsoft Business Central Protocol Implementation
@@ -314,12 +332,16 @@ def __init__(self, api_version='v1.0', default_resource=None, environment=None,
314332
315333 self .keyword_data_store ['message_type' ] = 'microsoft.graph.message'
316334 self .keyword_data_store ['event_message_type' ] = 'microsoft.graph.eventMessage'
317- self .keyword_data_store [
318- 'file_attachment_type' ] = '#microsoft.graph.fileAttachment'
319- self .keyword_data_store [
320- 'item_attachment_type' ] = '#microsoft.graph.itemAttachment'
335+ self .keyword_data_store ['file_attachment_type' ] = '#microsoft.graph.fileAttachment'
336+ self .keyword_data_store ['item_attachment_type' ] = '#microsoft.graph.itemAttachment'
337+ self .keyword_data_store ['prefer_timezone_header' ] = f'outlook.timezone="{ get_windows_tz (self .timezone )} "'
321338 self .max_top_value = 999 # Max $top parameter value
322339
340+ @Protocol .timezone .setter
341+ def timezone (self , timezone : Union [str , ZoneInfo ]):
342+ super ()._update_timezone (timezone )
343+ self .keyword_data_store ['prefer_timezone_header' ] = f'outlook.timezone="{ get_windows_tz (self ._timezone )} "'
344+
323345
324346class Connection :
325347 """ Handles all communication (requests) between the app and the server """
0 commit comments