Skip to content

Commit 3be8a1a

Browse files
authored
Merge pull request #22 from GearPlug/rev-drive-workbook
Rev drive workbook
2 parents c60dbbe + cf6dc1a commit 3be8a1a

File tree

5 files changed

+154
-109
lines changed

5 files changed

+154
-109
lines changed

microsoftgraph/client.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(
4848

4949
self.base_url = self.RESOURCE + self.api_version + "/"
5050
self.token = None
51+
self.workbook_session_id = None
5152
self.paginate = paginate
5253

5354
self.calendar = Calendar(self)
@@ -166,6 +167,14 @@ def set_token(self, token: dict) -> None:
166167
"""
167168
self.token = token
168169

170+
def set_workbook_session_id(self, workbook_session_id: dict) -> None:
171+
"""Sets the Workbook Session Id token for its use in this library.
172+
173+
Args:
174+
token (dict): Workbook Session ID.
175+
"""
176+
self.workbook_session_id = workbook_session_id
177+
169178
def _paginate_response(self, response: dict, **kwargs) -> dict:
170179
"""Some queries against Microsoft Graph return multiple pages of data either due to server-side paging or due to
171180
the use of the $top query parameter to specifically limit the page size in a request. When a result set spans
@@ -180,7 +189,7 @@ def _paginate_response(self, response: dict, **kwargs) -> dict:
180189
Returns:
181190
dict: Graph API Response.
182191
"""
183-
if not self.paginate:
192+
if not self.paginate or not isinstance(response.data, dict):
184193
return response
185194
while "@odata.nextLink" in response.data:
186195
data = response.data["value"]
@@ -224,51 +233,51 @@ def _parse(self, response):
224233
if status_code in (200, 201, 202, 204, 206):
225234
return r
226235
elif status_code == 400:
227-
raise exceptions.BadRequest(r)
236+
raise exceptions.BadRequest(r.data)
228237
elif status_code == 401:
229-
raise exceptions.Unauthorized(r)
238+
raise exceptions.Unauthorized(r.data)
230239
elif status_code == 403:
231-
raise exceptions.Forbidden(r)
240+
raise exceptions.Forbidden(r.data)
232241
elif status_code == 404:
233-
raise exceptions.NotFound(r)
242+
raise exceptions.NotFound(r.data)
234243
elif status_code == 405:
235-
raise exceptions.MethodNotAllowed(r)
244+
raise exceptions.MethodNotAllowed(r.data)
236245
elif status_code == 406:
237-
raise exceptions.NotAcceptable(r)
246+
raise exceptions.NotAcceptable(r.data)
238247
elif status_code == 409:
239-
raise exceptions.Conflict(r)
248+
raise exceptions.Conflict(r.data)
240249
elif status_code == 410:
241-
raise exceptions.Gone(r)
250+
raise exceptions.Gone(r.data)
242251
elif status_code == 411:
243-
raise exceptions.LengthRequired(r)
252+
raise exceptions.LengthRequired(r.data)
244253
elif status_code == 412:
245-
raise exceptions.PreconditionFailed(r)
254+
raise exceptions.PreconditionFailed(r.data)
246255
elif status_code == 413:
247-
raise exceptions.RequestEntityTooLarge(r)
256+
raise exceptions.RequestEntityTooLarge(r.data)
248257
elif status_code == 415:
249-
raise exceptions.UnsupportedMediaType(r)
258+
raise exceptions.UnsupportedMediaType(r.data)
250259
elif status_code == 416:
251-
raise exceptions.RequestedRangeNotSatisfiable(r)
260+
raise exceptions.RequestedRangeNotSatisfiable(r.data)
252261
elif status_code == 422:
253-
raise exceptions.UnprocessableEntity(r)
262+
raise exceptions.UnprocessableEntity(r.data)
254263
elif status_code == 429:
255-
raise exceptions.TooManyRequests(r)
264+
raise exceptions.TooManyRequests(r.data)
256265
elif status_code == 500:
257-
raise exceptions.InternalServerError(r)
266+
raise exceptions.InternalServerError(r.data)
258267
elif status_code == 501:
259-
raise exceptions.NotImplemented(r)
268+
raise exceptions.NotImplemented(r.data)
260269
elif status_code == 503:
261-
raise exceptions.ServiceUnavailable(r)
270+
raise exceptions.ServiceUnavailable(r.data)
262271
elif status_code == 504:
263-
raise exceptions.GatewayTimeout(r)
272+
raise exceptions.GatewayTimeout(r.data)
264273
elif status_code == 507:
265-
raise exceptions.InsufficientStorage(r)
274+
raise exceptions.InsufficientStorage(r.data)
266275
elif status_code == 509:
267-
raise exceptions.BandwidthLimitExceeded(r)
276+
raise exceptions.BandwidthLimitExceeded(r.data)
268277
else:
269278
if r["error"]["innerError"]["code"] == "lockMismatch":
270279
# File is currently locked due to being open in the web browser
271280
# while attempting to reupload a new version to the drive.
272281
# Thus temporarily unavailable.
273-
raise exceptions.ServiceUnavailable(r)
274-
raise exceptions.UnknownError(r)
282+
raise exceptions.ServiceUnavailable(r.data)
283+
raise exceptions.UnknownError(r.data)

microsoftgraph/decorators.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,14 @@ def helper(*args, **kwargs):
1111
return func(*args, **kwargs)
1212

1313
return helper
14+
15+
16+
def workbook_session_id_required(func):
17+
@wraps(func)
18+
def helper(*args, **kwargs):
19+
module = args[0]
20+
if not module._client.workbook_session_id:
21+
raise TokenRequired("You must set the Workbook Session Id.")
22+
return func(*args, **kwargs)
23+
24+
return helper

microsoftgraph/files.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def drive_specific_folder(self, folder_id: str, params: dict = None) -> Response
5858
Returns:
5959
Response: Microsoft Graph Response.
6060
"""
61-
url = self._client.base_url + "me/drive/items/{0}/children".format(folder_id)
62-
return self._client._get(url, params=params)
61+
url = "me/drive/items/{}/children".format(folder_id)
62+
return self._client._get(self._client.base_url + url, params=params)
6363

6464
@token_required
6565
def drive_get_item(self, item_id: str, params: dict = None, **kwargs) -> Response:
@@ -75,8 +75,8 @@ def drive_get_item(self, item_id: str, params: dict = None, **kwargs) -> Respons
7575
Returns:
7676
Response: Microsoft Graph Response.
7777
"""
78-
url = self._client.base_url + "me/drive/items/{0}".format(item_id)
79-
return self._client._get(url, params=params, **kwargs)
78+
url = "me/drive/items/{}".format(item_id)
79+
return self._client._get(self._client.base_url + url, params=params, **kwargs)
8080

8181
@token_required
8282
def drive_download_contents(self, item_id: str, params: dict = None, **kwargs) -> Response:
@@ -92,8 +92,8 @@ def drive_download_contents(self, item_id: str, params: dict = None, **kwargs) -
9292
Returns:
9393
Response: Microsoft Graph Response.
9494
"""
95-
url = self._client.base_url + "me/drive/items/{0}/content".format(item_id)
96-
return self._client._get(url, params=params, **kwargs)
95+
url = "me/drive/items/{}/content".format(item_id)
96+
return self._client._get(self._client.base_url + url, params=params, **kwargs)
9797

9898
@token_required
9999
def drive_download_shared_contents(self, share_id: str, params: dict = None, **kwargs) -> Response:
@@ -111,7 +111,7 @@ def drive_download_shared_contents(self, share_id: str, params: dict = None, **k
111111
"""
112112
base64_value = base64.b64encode(share_id.encode()).decode()
113113
encoded_share_url = "u!" + base64_value.rstrip("=").replace("/", "_").replace("+", "-")
114-
url = self._client.base_url + "shares/{0}/driveItem".format(encoded_share_url)
114+
url = self._client.base_url + "shares/{}/driveItem".format(encoded_share_url)
115115
drive_item = self._client._get(url)
116116
file_download_url = drive_item["@microsoft.graph.downloadUrl"]
117117
return drive_item["name"], requests.get(file_download_url).content
@@ -148,6 +148,24 @@ def drive_upload_item(self, item_id: str, params: dict = None, **kwargs) -> Resp
148148
Returns:
149149
Response: Microsoft Graph Response.
150150
"""
151-
url = self._client.base_url + "me/drive/items/{0}/content".format(item_id)
152151
kwargs["headers"] = {"Content-Type": "text/plain"}
153-
return self._client._put(url, params=params, **kwargs)
152+
url = "me/drive/items/{}/content".format(item_id)
153+
return self._client._put(self._client.base_url + url, params=params, **kwargs)
154+
155+
@token_required
156+
def search_items(self, q: str, params: dict = None, **kwargs) -> Response:
157+
"""Search the hierarchy of items for items matching a query. You can search within a folder hierarchy, a whole
158+
drive, or files shared with the current user.
159+
160+
https://docs.microsoft.com/en-us/graph/api/driveitem-search?view=graph-rest-1.0&tabs=http
161+
162+
Args:
163+
q (str): The query text used to search for items. Values may be matched across several fields including
164+
filename, metadata, and file content.
165+
params (dict, optional): Query. Defaults to None.
166+
167+
Returns:
168+
Response: Microsoft Graph Response.
169+
"""
170+
url = "me/drive/root/search(q='{}')".format(q)
171+
return self._client._get(self._client.base_url + url, params=params, **kwargs)

0 commit comments

Comments
 (0)