From 2e36a9cf5fb6e9de8cc930928853dacc3923ccb5 Mon Sep 17 00:00:00 2001 From: vgrem Date: Sat, 16 Mar 2024 16:28:25 +0200 Subject: [PATCH] OneDrive examples update --- examples/onedrive/folders/download.py | 28 +++++++++++++++++++ .../{download_files.py => download_custom.py} | 6 ++-- 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 examples/onedrive/folders/download.py rename examples/onedrive/folders/{download_files.py => download_custom.py} (82%) diff --git a/examples/onedrive/folders/download.py b/examples/onedrive/folders/download.py new file mode 100644 index 000000000..37b0bea4b --- /dev/null +++ b/examples/onedrive/folders/download.py @@ -0,0 +1,28 @@ +""" +Download the contents of the driveItem (folder facet) + +https://learn.microsoft.com/en-us/graph/api/driveitem-get-content?view=graph-rest-1.0 +""" + +import os +import tempfile + +from office365.graph_client import GraphClient +from office365.onedrive.driveitems.driveItem import DriveItem +from tests import test_client_id, test_password, test_tenant, test_username + + +def print_progress(downloaded_file): + # type: (DriveItem) -> None + print("File {0} has been downloaded..".format(downloaded_file.web_url)) + + +client = GraphClient.with_username_and_password( + test_tenant, test_client_id, test_username, test_password +) +folder_item = client.me.drive.root.get_by_path("archive") + +zip_path = os.path.join(tempfile.mkdtemp(), "download.zip") +with open(zip_path, "wb") as f: + folder_item.download_folder(f, print_progress).execute_query() +print("Folder has been downloaded to {0}".format(zip_path)) diff --git a/examples/onedrive/folders/download_files.py b/examples/onedrive/folders/download_custom.py similarity index 82% rename from examples/onedrive/folders/download_files.py rename to examples/onedrive/folders/download_custom.py index fbbfc9c69..27fa2409d 100644 --- a/examples/onedrive/folders/download_files.py +++ b/examples/onedrive/folders/download_custom.py @@ -9,9 +9,11 @@ from office365.graph_client import GraphClient from office365.onedrive.driveitems.driveItem import DriveItem -from tests.graph_case import acquire_token_by_username_password +from tests import test_client_id, test_password, test_tenant, test_username -client = GraphClient(acquire_token_by_username_password) +client = GraphClient.with_username_and_password( + test_tenant, test_client_id, test_username, test_password +) folder_item = client.me.drive.root.get_by_path("archive").get().execute_query() with tempfile.TemporaryDirectory() as local_path: