Skip to content

Commit 5a4a8ed

Browse files
committed
DriveItem.copy method & example updates
1 parent 9eefb90 commit 5a4a8ed

File tree

5 files changed

+38
-37
lines changed

5 files changed

+38
-37
lines changed

examples/onedrive/files/copy_file.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
client = GraphClient.with_username_and_password(
1111
test_tenant, test_client_id, test_username, test_password
1212
)
13-
source_path = "archive/Sample.rtf"
14-
new_name = "Sample (copy).rtf"
13+
# source_path = "archive/Sample.rtf"
14+
local_path = "../../data/Financial Sample.xlsx"
15+
source_file = client.me.drive.root.upload_file(local_path).execute_query()
16+
# new_name = "Sample (copy).rtf"
17+
# source_file = client.me.drive.root.get_by_path(source_path) # source file item
1518
target_path = "archive/2018"
16-
source_file_item = client.me.drive.root.get_by_path(source_path) # source file item
17-
target_folder_item = client.me.drive.root.get_by_path(target_path) # target folder item
19+
target_folder = client.me.drive.root.get_by_path(target_path)
1820
# result = source_file_item.copy(name=new_name).execute_query() # copy to the same folder with a different name
19-
result = source_file_item.copy(
20-
parent=target_folder_item
21+
result = source_file.copy(
22+
parent=target_folder
2123
).execute_query() # copy to another folder
2224
print(result.value)

office365/onedrive/driveitems/driveItem.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ def convert(self, format_name):
469469
return self.get_content(format_name)
470470

471471
def copy(self, name=None, parent=None, conflict_behavior=ConflictBehavior.Fail):
472+
# type: (str, ItemReference|"DriveItem", str) -> ClientResult[str]
472473
"""Asynchronously creates a copy of an driveItem (including any children), under a new parent item or with a
473474
new name.
474475
@@ -480,44 +481,40 @@ def copy(self, name=None, parent=None, conflict_behavior=ConflictBehavior.Fail):
480481
481482
Returns location for details about how to monitor the progress of the copy, upon accepting the request.
482483
"""
483-
return_type = ClientResult(self.context) # type: ClientResult[str]
484+
return_type = ClientResult(self.context, str())
484485

485-
def _create_request(request):
486-
# type: (RequestOptions) -> None
487-
request.url += "?@microsoft.graph.conflictBehavior={0}".format(
488-
conflict_behavior
489-
)
486+
def _copy(parent_reference):
487+
# type: (ItemReference) -> None
488+
489+
def _create_request(request):
490+
# type: (RequestOptions) -> None
491+
request.url += "?@microsoft.graph.conflictBehavior={0}".format(
492+
conflict_behavior
493+
)
494+
495+
def _process_response(resp):
496+
# type: (requests.Response) -> None
497+
resp.raise_for_status()
498+
location = resp.headers.get("Location", None)
499+
if location is None:
500+
return
501+
return_type.set_property("__value", location)
490502

491-
def _process_response(resp):
492-
# type: (requests.Response) -> None
493-
resp.raise_for_status()
494-
location = resp.headers.get("Location", None)
495-
if location is None:
496-
return
497-
return_type.set_property("__value", location)
498-
499-
def _create_and_add_query(parent_reference):
500-
"""
501-
:param office365.onedrive.listitems.item_reference.ItemReference or None parent_reference: Reference to the
502-
parent item the copy will be created in.
503-
"""
504503
payload = {"name": name, "parentReference": parent_reference}
505-
self.context.before_execute(_create_request)
506-
self.context.after_execute(_process_response)
507504
qry = ServiceOperationQuery(self, "copy", None, payload, None, return_type)
508-
self.context.add_query(qry)
505+
self.context.add_query(qry).before_execute(_create_request).after_execute(_process_response)
509506

510507
if isinstance(parent, DriveItem):
511508

512509
def _drive_item_loaded():
513510
parent_reference = ItemReference(
514511
drive_id=parent.parent_reference.driveId, _id=parent.id
515512
)
516-
_create_and_add_query(parent_reference)
513+
_copy(parent_reference)
517514

518515
parent.ensure_property("parentReference", _drive_item_loaded)
519516
else:
520-
_create_and_add_query(parent)
517+
_copy(parent)
521518
return return_type
522519

523520
def move(self, name=None, parent=None, conflict_behavior=ConflictBehavior.Fail):
@@ -668,7 +665,7 @@ def permanent_delete(self):
668665
return self
669666

670667
def restore(self, parent_reference=None, name=None):
671-
# type: (ItemReference or None, str or None) -> DriveItem
668+
# type: (Optional[ItemReference], str) -> "DriveItem"
672669
"""
673670
Restore a driveItem that has been deleted and is currently in the recycle bin.
674671
NOTE: This functionality is currently only available for OneDrive Personal.

office365/onedrive/storage/__init__.py

Whitespace-only changes.

office365/onedrive/storage/storage.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from office365.entity import Entity
2+
3+
4+
class Storage(Entity):
5+
"""Facilitates the structures of fileStorageContainers."""

office365/onedrive/workbooks/workbook.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,12 @@ def refresh_session(self, session_id):
5353
"""Use this API to refresh an existing workbook session.
5454
:param str session_id: Identifier of the workbook session
5555
"""
56-
qry = ServiceOperationQuery(self, "refreshSession")
57-
self.context.add_query(qry)
58-
5956
def _construct_request(request):
6057
# type: (RequestOptions) -> None
6158
request.set_header("workbook-session-id", session_id)
6259

63-
self.context.before_execute(_construct_request)
60+
qry = ServiceOperationQuery(self, "refreshSession")
61+
self.context.add_query(qry).before_execute(_construct_request)
6462
return self
6563

6664
def close_session(self, session_id):
@@ -73,8 +71,7 @@ def _construct_request(request):
7371
# type: (RequestOptions) -> None
7472
request.set_header("workbook-session-id", session_id)
7573

76-
self.context.before_execute(_construct_request)
77-
self.context.add_query(qry)
74+
self.context.add_query(qry).before_execute(_construct_request)
7875
return self
7976

8077
@property

0 commit comments

Comments
 (0)