Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Now get_drives accepts limit, batch order_by and query parameters
  • Loading branch information
Alejandro Casanovas committed Sep 2, 2019
1 parent 8739a8f commit 3c9885a
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions O365/drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ def get_item_by_path(self, item_path):
# reference the current drive_id
url = self.build_url(
self._endpoints.get('get_item_by_path').format(id=self.object_id,
item_path=item_path))
item_path=item_path))
else:
# we don't know the drive_id so go to the default drive
url = self.build_url(
Expand All @@ -1504,7 +1504,7 @@ def get_item_by_path(self, item_path):

# Everything received from cloud must be passed as self._cloud_data_key
return self._classifier(data)(parent=self,
**{self._cloud_data_key: data})
**{self._cloud_data_key: data})

def get_special_folder(self, name):
""" Returns the specified Special Folder
Expand Down Expand Up @@ -1735,17 +1735,50 @@ def get_drive(self, drive_id):
main_resource=self.main_resource,
**{self._cloud_data_key: drive})

def get_drives(self):
""" Returns a collection of drives"""
def get_drives(self, limit=None, *, query=None, order_by=None,
batch=None):
""" Returns a collection of drives
:param int limit: max no. of items to get. Over 999 uses batch.
:param query: applies a OData filter to the request
:type query: Query or str
:param order_by: orders the result set based on this condition
:type order_by: Query or str
:param int batch: batch size, retrieves items in
batches allowing to retrieve more items than the limit.
:return: list of drives in this Storage
:rtype: list[Drive] or Pagination
"""

url = self.build_url(self._endpoints.get('list_drives'))

response = self.con.get(url)
if limit is None or limit > self.protocol.max_top_value:
batch = self.protocol.max_top_value

params = {'$top': batch if batch else limit}

if order_by:
params['$orderby'] = order_by

if query:
if isinstance(query, str):
params['$filter'] = query
else:
params.update(query.as_params())

response = self.con.get(url, params=params)
if not response:
return []

data = response.json()

# Everything received from cloud must be passed as self._cloud_data_key
return [self.drive_constructor(parent=self, **{self._cloud_data_key: drive})
for drive in data.get('value', [])]

drives = [self.drive_constructor(parent=self, **{self._cloud_data_key: drive}) for
drive in data.get('value', [])]
next_link = data.get(NEXT_LINK_KEYWORD, None)
if batch and next_link:
return Pagination(parent=self, data=drives, constructor=self.drive_constructor,
next_link=next_link, limit=limit)
else:
return drives

0 comments on commit 3c9885a

Please sign in to comment.