Skip to content

Commit

Permalink
Merge pull request #22 from GearPlug/rev-drive-workbook
Browse files Browse the repository at this point in the history
Rev drive workbook
  • Loading branch information
ingmferrer authored Dec 20, 2021
2 parents c60dbbe + cf6dc1a commit 3be8a1a
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 109 deletions.
57 changes: 33 additions & 24 deletions microsoftgraph/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(

self.base_url = self.RESOURCE + self.api_version + "/"
self.token = None
self.workbook_session_id = None
self.paginate = paginate

self.calendar = Calendar(self)
Expand Down Expand Up @@ -166,6 +167,14 @@ def set_token(self, token: dict) -> None:
"""
self.token = token

def set_workbook_session_id(self, workbook_session_id: dict) -> None:
"""Sets the Workbook Session Id token for its use in this library.
Args:
token (dict): Workbook Session ID.
"""
self.workbook_session_id = workbook_session_id

def _paginate_response(self, response: dict, **kwargs) -> dict:
"""Some queries against Microsoft Graph return multiple pages of data either due to server-side paging or due to
the use of the $top query parameter to specifically limit the page size in a request. When a result set spans
Expand All @@ -180,7 +189,7 @@ def _paginate_response(self, response: dict, **kwargs) -> dict:
Returns:
dict: Graph API Response.
"""
if not self.paginate:
if not self.paginate or not isinstance(response.data, dict):
return response
while "@odata.nextLink" in response.data:
data = response.data["value"]
Expand Down Expand Up @@ -224,51 +233,51 @@ def _parse(self, response):
if status_code in (200, 201, 202, 204, 206):
return r
elif status_code == 400:
raise exceptions.BadRequest(r)
raise exceptions.BadRequest(r.data)
elif status_code == 401:
raise exceptions.Unauthorized(r)
raise exceptions.Unauthorized(r.data)
elif status_code == 403:
raise exceptions.Forbidden(r)
raise exceptions.Forbidden(r.data)
elif status_code == 404:
raise exceptions.NotFound(r)
raise exceptions.NotFound(r.data)
elif status_code == 405:
raise exceptions.MethodNotAllowed(r)
raise exceptions.MethodNotAllowed(r.data)
elif status_code == 406:
raise exceptions.NotAcceptable(r)
raise exceptions.NotAcceptable(r.data)
elif status_code == 409:
raise exceptions.Conflict(r)
raise exceptions.Conflict(r.data)
elif status_code == 410:
raise exceptions.Gone(r)
raise exceptions.Gone(r.data)
elif status_code == 411:
raise exceptions.LengthRequired(r)
raise exceptions.LengthRequired(r.data)
elif status_code == 412:
raise exceptions.PreconditionFailed(r)
raise exceptions.PreconditionFailed(r.data)
elif status_code == 413:
raise exceptions.RequestEntityTooLarge(r)
raise exceptions.RequestEntityTooLarge(r.data)
elif status_code == 415:
raise exceptions.UnsupportedMediaType(r)
raise exceptions.UnsupportedMediaType(r.data)
elif status_code == 416:
raise exceptions.RequestedRangeNotSatisfiable(r)
raise exceptions.RequestedRangeNotSatisfiable(r.data)
elif status_code == 422:
raise exceptions.UnprocessableEntity(r)
raise exceptions.UnprocessableEntity(r.data)
elif status_code == 429:
raise exceptions.TooManyRequests(r)
raise exceptions.TooManyRequests(r.data)
elif status_code == 500:
raise exceptions.InternalServerError(r)
raise exceptions.InternalServerError(r.data)
elif status_code == 501:
raise exceptions.NotImplemented(r)
raise exceptions.NotImplemented(r.data)
elif status_code == 503:
raise exceptions.ServiceUnavailable(r)
raise exceptions.ServiceUnavailable(r.data)
elif status_code == 504:
raise exceptions.GatewayTimeout(r)
raise exceptions.GatewayTimeout(r.data)
elif status_code == 507:
raise exceptions.InsufficientStorage(r)
raise exceptions.InsufficientStorage(r.data)
elif status_code == 509:
raise exceptions.BandwidthLimitExceeded(r)
raise exceptions.BandwidthLimitExceeded(r.data)
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)
raise exceptions.ServiceUnavailable(r.data)
raise exceptions.UnknownError(r.data)
11 changes: 11 additions & 0 deletions microsoftgraph/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ def helper(*args, **kwargs):
return func(*args, **kwargs)

return helper


def workbook_session_id_required(func):
@wraps(func)
def helper(*args, **kwargs):
module = args[0]
if not module._client.workbook_session_id:
raise TokenRequired("You must set the Workbook Session Id.")
return func(*args, **kwargs)

return helper
36 changes: 27 additions & 9 deletions microsoftgraph/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def drive_specific_folder(self, folder_id: str, params: dict = None) -> Response
Returns:
Response: Microsoft Graph Response.
"""
url = self._client.base_url + "me/drive/items/{0}/children".format(folder_id)
return self._client._get(url, params=params)
url = "me/drive/items/{}/children".format(folder_id)
return self._client._get(self._client.base_url + url, params=params)

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

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

@token_required
def drive_download_shared_contents(self, share_id: str, params: dict = None, **kwargs) -> Response:
Expand All @@ -111,7 +111,7 @@ def drive_download_shared_contents(self, share_id: str, params: dict = None, **k
"""
base64_value = base64.b64encode(share_id.encode()).decode()
encoded_share_url = "u!" + base64_value.rstrip("=").replace("/", "_").replace("+", "-")
url = self._client.base_url + "shares/{0}/driveItem".format(encoded_share_url)
url = self._client.base_url + "shares/{}/driveItem".format(encoded_share_url)
drive_item = self._client._get(url)
file_download_url = drive_item["@microsoft.graph.downloadUrl"]
return drive_item["name"], requests.get(file_download_url).content
Expand Down Expand Up @@ -148,6 +148,24 @@ def drive_upload_item(self, item_id: str, params: dict = None, **kwargs) -> Resp
Returns:
Response: Microsoft Graph Response.
"""
url = self._client.base_url + "me/drive/items/{0}/content".format(item_id)
kwargs["headers"] = {"Content-Type": "text/plain"}
return self._client._put(url, params=params, **kwargs)
url = "me/drive/items/{}/content".format(item_id)
return self._client._put(self._client.base_url + url, params=params, **kwargs)

@token_required
def search_items(self, q: str, params: dict = None, **kwargs) -> Response:
"""Search the hierarchy of items for items matching a query. You can search within a folder hierarchy, a whole
drive, or files shared with the current user.
https://docs.microsoft.com/en-us/graph/api/driveitem-search?view=graph-rest-1.0&tabs=http
Args:
q (str): The query text used to search for items. Values may be matched across several fields including
filename, metadata, and file content.
params (dict, optional): Query. Defaults to None.
Returns:
Response: Microsoft Graph Response.
"""
url = "me/drive/root/search(q='{}')".format(q)
return self._client._get(self._client.base_url + url, params=params, **kwargs)
Loading

0 comments on commit 3be8a1a

Please sign in to comment.