Skip to content

Commit b09ef85

Browse files
committed
#788: batch request fixes and enhancements
1 parent cc9c7bb commit b09ef85

File tree

7 files changed

+37
-14
lines changed

7 files changed

+37
-14
lines changed

examples/sharepoint/listitems/create_batch.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22
Demonstrates how to create multiple list items
33
"""
44

5+
from typing import List
6+
7+
from office365.runtime.client_object import ClientObject
58
from office365.sharepoint.client_context import ClientContext
9+
from office365.sharepoint.listitems.listitem import ListItem
610
from tests import create_unique_name, test_client_credentials, test_team_site_url
711

812

9-
def print_progress(items_count):
13+
def print_progress(return_types):
14+
# type: (List[ClientObject]) -> None
15+
items_count = len([t for t in return_types if isinstance(t, ListItem)])
1016
print("{0} list items has been created".format(items_count))
1117

1218

1319
ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
1420
tasks_list = ctx.web.lists.get_by_title("Company Tasks")
1521

16-
num_of_items = 1024
22+
num_of_items = 512
1723
item_props = {"Title": create_unique_name("Task")}
1824
task_items = [tasks_list.add_item(item_props) for idx in range(0, num_of_items)]
1925
ctx.execute_batch(success_callback=print_progress)

examples/sharepoint/lists/import_list.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
from typing import List
2+
13
from faker import Faker
24

5+
from office365.runtime.client_object import ClientObject
36
from office365.sharepoint.client_context import ClientContext
7+
from office365.sharepoint.listitems.listitem import ListItem
48
from tests import test_team_site_url, test_user_credentials
59

610

7-
def print_progress(items_count):
11+
def print_progress(return_types):
12+
# type: (List[ClientObject]) -> None
13+
items_count = len([t for t in return_types if isinstance(t, ListItem)])
814
print("{0} list items has been created".format(items_count))
915

1016

@@ -41,7 +47,7 @@ def run(context):
4147
# idx, len(contacts_data), contact_item.properties["Title"]
4248
# )
4349
# )
44-
ctx.execute_batch(items_per_batch=2, success_callback=print_progress)
50+
ctx.execute_batch(items_per_batch=10, success_callback=print_progress)
4551

4652

4753
if __name__ == "__main__":

examples/sharepoint/webs/clear_web.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
33
"""
44

5+
from typing import List
6+
57
from office365.sharepoint.client_context import ClientContext
68
from tests import test_client_credentials, test_site_url
79

810

9-
def print_progress(num_deleted):
10-
# type: (int) -> None
11-
print("{0} deleted.".format(num_deleted))
11+
def print_progress(deleted_lists):
12+
# type: (List) -> None
13+
print("{0} deleted.".format(len(deleted_lists)))
1214

1315

1416
ctx = ClientContext(test_site_url).with_credentials(test_client_credentials)

office365/graph_client.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,19 @@ def _acquire_token():
229229

230230
return GraphClient(_acquire_token)
231231

232-
def execute_batch(self, items_per_batch=100):
232+
def execute_batch(self, items_per_batch=100, success_callback=None):
233233
"""Constructs and submit a batch request
234234
235235
:param int items_per_batch: Maximum to be selected for bulk operation
236+
:param (List[ClientObject|ClientResult])-> None success_callback: A success callback
236237
"""
237238
batch_request = ODataV4BatchRequest(V4JsonFormat())
238239
batch_request.beforeExecute += self._authenticate_request
239240
while self.has_pending_request:
240241
qry = self._get_next_query(items_per_batch)
241242
batch_request.execute_query(qry)
243+
if callable(success_callback):
244+
success_callback(qry.return_type)
242245
return self
243246

244247
def pending_request(self):

office365/runtime/queries/batch.py

+4
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@ def has_change_sets(self):
6161
@property
6262
def url(self):
6363
return "{0}/$batch".format(self.context.service_root_url())
64+
65+
@property
66+
def return_type(self):
67+
return [q.return_type for q in self._queries]

office365/sharepoint/client_context.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from office365.runtime.auth.client_credential import ClientCredential
99
from office365.runtime.auth.token_response import TokenResponse
1010
from office365.runtime.auth.user_credential import UserCredential
11+
from office365.runtime.client_object import ClientObject
1112
from office365.runtime.client_result import ClientResult
1213
from office365.runtime.client_runtime_context import ClientRuntimeContext
1314
from office365.runtime.compat import get_absolute_url, urlparse
@@ -197,11 +198,11 @@ def with_credentials(self, credentials, environment="commercial"):
197198
return self
198199

199200
def execute_batch(self, items_per_batch=100, success_callback=None):
200-
# type: (int, Callable[[int], None]) -> Self
201+
# type: (int, Callable[[List[ClientObject|ClientResult]], None]) -> Self
201202
"""
202203
Construct and submit to a server a batch request
203204
:param int items_per_batch: Maximum to be selected for bulk operation
204-
:param (int)-> None success_callback: A success callback
205+
:param (List[ClientObject|ClientResult])-> None success_callback: A success callback
205206
"""
206207
batch_request = ODataBatchV3Request(JsonLightFormat())
207208
batch_request.beforeExecute += self._authenticate_request
@@ -210,7 +211,7 @@ def execute_batch(self, items_per_batch=100, success_callback=None):
210211
qry = self._get_next_query(items_per_batch)
211212
batch_request.execute_query(qry)
212213
if callable(success_callback):
213-
success_callback(items_per_batch)
214+
success_callback(qry.return_type)
214215
return self
215216

216217
def pending_request(self):

office365/sharepoint/entity.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from typing import TYPE_CHECKING, Callable
1+
from typing import TYPE_CHECKING, Callable, List
22

33
from typing_extensions import Self
44

55
from office365.runtime.auth.client_credential import ClientCredential
66
from office365.runtime.auth.user_credential import UserCredential
77
from office365.runtime.client_object import ClientObject
8+
from office365.runtime.client_result import ClientResult
89
from office365.runtime.paths.v3.entity import EntityPath
910
from office365.runtime.queries.delete_entity import DeleteEntityQuery
1011
from office365.runtime.queries.update_entity import UpdateEntityQuery
@@ -22,13 +23,13 @@ def execute_query_with_incremental_retry(self, max_retry=5):
2223
return self
2324

2425
def execute_batch(self, items_per_batch=100, success_callback=None):
25-
# type: (int, Callable[[int], None]) -> Self
26+
# type: (int, Callable[[List[ClientObject|ClientResult]], None]) -> Self
2627
"""Construct and submit to a server a batch request"""
2728
return self.context.execute_batch(items_per_batch, success_callback)
2829

2930
def with_credentials(self, credentials):
3031
# type: (UserCredential|ClientCredential) -> Self
31-
""" """
32+
"""Initializes a client to acquire a token via user or client credentials"""
3233
self.context.with_credentials(credentials)
3334
return self
3435

0 commit comments

Comments
 (0)