Skip to content

Commit

Permalink
DriveItem.copy method & example updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Jun 16, 2024
1 parent 9eefb90 commit 5a4a8ed
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 37 deletions.
14 changes: 8 additions & 6 deletions examples/onedrive/files/copy_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
source_path = "archive/Sample.rtf"
new_name = "Sample (copy).rtf"
# source_path = "archive/Sample.rtf"
local_path = "../../data/Financial Sample.xlsx"
source_file = client.me.drive.root.upload_file(local_path).execute_query()
# new_name = "Sample (copy).rtf"
# source_file = client.me.drive.root.get_by_path(source_path) # source file item
target_path = "archive/2018"
source_file_item = client.me.drive.root.get_by_path(source_path) # source file item
target_folder_item = client.me.drive.root.get_by_path(target_path) # target folder item
target_folder = client.me.drive.root.get_by_path(target_path)
# result = source_file_item.copy(name=new_name).execute_query() # copy to the same folder with a different name
result = source_file_item.copy(
parent=target_folder_item
result = source_file.copy(
parent=target_folder
).execute_query() # copy to another folder
print(result.value)
47 changes: 22 additions & 25 deletions office365/onedrive/driveitems/driveItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ def convert(self, format_name):
return self.get_content(format_name)

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

def _create_request(request):
# type: (RequestOptions) -> None
request.url += "[email protected]={0}".format(
conflict_behavior
)
def _copy(parent_reference):
# type: (ItemReference) -> None

def _create_request(request):
# type: (RequestOptions) -> None
request.url += "[email protected]={0}".format(
conflict_behavior
)

def _process_response(resp):
# type: (requests.Response) -> None
resp.raise_for_status()
location = resp.headers.get("Location", None)
if location is None:
return
return_type.set_property("__value", location)

def _process_response(resp):
# type: (requests.Response) -> None
resp.raise_for_status()
location = resp.headers.get("Location", None)
if location is None:
return
return_type.set_property("__value", location)

def _create_and_add_query(parent_reference):
"""
:param office365.onedrive.listitems.item_reference.ItemReference or None parent_reference: Reference to the
parent item the copy will be created in.
"""
payload = {"name": name, "parentReference": parent_reference}
self.context.before_execute(_create_request)
self.context.after_execute(_process_response)
qry = ServiceOperationQuery(self, "copy", None, payload, None, return_type)
self.context.add_query(qry)
self.context.add_query(qry).before_execute(_create_request).after_execute(_process_response)

if isinstance(parent, DriveItem):

def _drive_item_loaded():
parent_reference = ItemReference(
drive_id=parent.parent_reference.driveId, _id=parent.id
)
_create_and_add_query(parent_reference)
_copy(parent_reference)

parent.ensure_property("parentReference", _drive_item_loaded)
else:
_create_and_add_query(parent)
_copy(parent)
return return_type

def move(self, name=None, parent=None, conflict_behavior=ConflictBehavior.Fail):
Expand Down Expand Up @@ -668,7 +665,7 @@ def permanent_delete(self):
return self

def restore(self, parent_reference=None, name=None):
# type: (ItemReference or None, str or None) -> DriveItem
# type: (Optional[ItemReference], str) -> "DriveItem"
"""
Restore a driveItem that has been deleted and is currently in the recycle bin.
NOTE: This functionality is currently only available for OneDrive Personal.
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions office365/onedrive/storage/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from office365.entity import Entity


class Storage(Entity):
"""Facilitates the structures of fileStorageContainers."""
9 changes: 3 additions & 6 deletions office365/onedrive/workbooks/workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,12 @@ def refresh_session(self, session_id):
"""Use this API to refresh an existing workbook session.
:param str session_id: Identifier of the workbook session
"""
qry = ServiceOperationQuery(self, "refreshSession")
self.context.add_query(qry)

def _construct_request(request):
# type: (RequestOptions) -> None
request.set_header("workbook-session-id", session_id)

self.context.before_execute(_construct_request)
qry = ServiceOperationQuery(self, "refreshSession")
self.context.add_query(qry).before_execute(_construct_request)
return self

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

self.context.before_execute(_construct_request)
self.context.add_query(qry)
self.context.add_query(qry).before_execute(_construct_request)
return self

@property
Expand Down

0 comments on commit 5a4a8ed

Please sign in to comment.