O365 / python-o365

A simple python library to interact with Microsoft Graph and Office 365 API
Apache License 2.0
1.6k stars 411 forks source link

Add support for multiple Prefer headers in Connection class #1042

Closed Invincibear closed 5 months ago

Invincibear commented 5 months ago

Added support for multiple Prefer headers in the Connection class. Clarified unsupported method error message.

Added support for multiple Prefer headers in the Connection class

Assuming something like:

schedule = Schedule()
schedule.con.default_headers['Prefer'] = 'IdType="ImmutableId"'

Using a method that has a Prefer header hardcoded in the request, like Calendar.get_event() which specifies response = self.con.get(url, params=params, ={'Prefer': 'outlook.timezone="UTC"'}) would override Connection.default_headers['Prefer']. This change concatenates the values.

Before:

2024-01-11 11:38:04,410 DEBUG    Supplying headers {'Prefer': 'outlook.timezone="UTC"}

After:

2024-01-11 11:38:04,410 DEBUG    Supplying headers {'Prefer': 'outlook.timezone="UTC", IdType="ImmutableId"'}

Clarified unsupported method error message

Assuming something like:

_allowed_methods = ['get', 'post', 'put', 'patch', 'delete']

No error when using a valid method:

method = 'get'
if method not in _allowed_methods:
    raise ValueError('Method must be one of: {}'.format(_allowed_methods))

Improved error message when using an invalid method:

method = 'this should fail'
if method not in _allowed_methods:
    raise ValueError('Method must be one of: {}'.format(_allowed_methods))
Traceback (most recent call last):
  File "/snap/pycharm-professional/364/plugins/python/helpers/pydev/pydevconsole.py", line 364, in runcode
    coro = func()
           ^^^^^^
  File "<input>", line 4, in <module>
ValueError: Method must be one of: ['get', 'post', 'put', 'patch', 'delete']
alejcas commented 5 months ago

Thanks!