Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ refresh_session = client.drive_refresh_session(item_id)
close_session = client.drive_close_session(item_id)
```

#### Download the contents of a specific item
```
contents_bytes = client.drive_download_contents(item_id)
```

#### Get a Drive item resource
```
drive_item_dict = client.drive_get_item(item_id)
```

#### Get worksheets
```
get_worksheets = client.excel_get_worksheets(item_id)
Expand Down Expand Up @@ -254,4 +264,4 @@ update_range = client.excel_update_range(item_id, worksheets_id)
## Tests
```
test/test.py
```
```
23 changes: 22 additions & 1 deletion microsoftgraph/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,22 @@ def drive_close_session(self, item_id, **kwargs):
url = "https://graph.microsoft.com/beta/me/drive/items/{0}/workbook/closeSession".format(item_id)
return self._post(url, **kwargs)

@token_required
def drive_download_contents(self, item_id, params=None, **kwargs):
url = "https://graph.microsoft.com/beta/me/drive/items/{0}/content".format(item_id)
return self._get(url, params=params, **kwargs)

@token_required
def drive_get_item(self, item_id, params=None, **kwargs):
url = "https://graph.microsoft.com/beta/me/drive/items/{0}".format(item_id)
return self._get(url, params=params, **kwargs)

@token_required
def drive_upload_item(self, item_id, params=None, **kwargs):
url = "https://graph.microsoft.com/beta/me/drive/items/{0}/content".format(item_id)
kwargs['headers'] = {'Content-Type': 'text/plain'}
return self._put(url, params=params, **kwargs)

# Excel
@token_required
def excel_get_worksheets(self, item_id, params=None, **kwargs):
Expand Down Expand Up @@ -600,7 +616,7 @@ def _parse(self, response):
if 'application/json' in response.headers['Content-Type']:
r = response.json()
else:
r = response.text
r = response.content
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this change the file contents returns as a str. When a file is downloaded as str (I tested an xlsx file), it will be corrupted. Changing to response.content returns bytes instead and the downloaded file opens correctly.

if status_code in (200, 201, 202):
return r
elif status_code == 204:
Expand Down Expand Up @@ -648,4 +664,9 @@ def _parse(self, response):
elif status_code == 509:
raise exceptions.BandwidthLimitExceeded(r)
else:
if r['error']['innerError']['code'] == 'lockMismatch':
# File is currently locked due to being open in the web browser
# while attempting to reupload a new version to the drive.
# Thus temporarily unavailable.
raise exceptions.ServiceUnavailable(r)
raise exceptions.UnknownError(r)