Skip to content

Commit d719c7f

Browse files
committed
Make wait for confirmation more ergonomic
Problem: The uage of wait_for_confirmation requires an extra import and a clumsy invocation. Solution: Added wait_for_confimation as method in the assets and evenst classes. Signed-off-by: Paul Hewlett <phewlett76@gmail.com>
1 parent 5d2bc7d commit d719c7f

6 files changed

Lines changed: 86 additions & 1 deletion

File tree

archivist/assets.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@ def create_from_data(self, data: Dict, *, confirm: bool = False) -> Asset:
166166

167167
return wait_for_confirmation(self, asset["identity"]) # type: ignore
168168

169+
def wait_for_confirmation(self, identity: str) -> bool:
170+
"""Wait for asset to be confirmed.
171+
172+
Waits asset to be confirmed.
173+
174+
Args:
175+
identity (str): identity of asset
176+
177+
Returns:
178+
True if asset is confirmed.
179+
180+
"""
181+
return wait_for_confirmation(self, identity)
182+
169183
def read(self, identity: str) -> Asset:
170184
"""Read asset
171185

archivist/events.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,20 @@ def create_from_data(self, asset_id: str, data: Dict, *, confirm=False) -> Event
174174

175175
return wait_for_confirmation(self, event["identity"]) # type: ignore
176176

177+
def wait_for_confirmation(self, identity: str) -> bool:
178+
"""Wait for event to be confirmed.
179+
180+
Waits for event to be confirmed.
181+
182+
Args:
183+
identity (str): identity of event
184+
185+
Returns:
186+
True if event is confirmed.
187+
188+
"""
189+
return wait_for_confirmation(self, identity)
190+
177191
def read(self, identity: str) -> Event:
178192
"""Read event
179193

examples/create_asset.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ def create_asset(arch):
6767
return arch.assets.create(
6868
behaviours, attrs, storage_integrity=storage_integrity, confirm=True
6969
)
70+
# alternatively if some work can be done whilst the asset is confirmed then this call can be
71+
# replaced by a two-step alternative:
72+
73+
# asset = arch.assets.create(
74+
# behaviours, attrs, storage_integrity=storage_integrity, confirm=False
75+
# )
76+
77+
# ... do something else here
78+
# and then wait for confirmation
79+
80+
# self.arch.assets.wait_for_confirmation(asset['identity']))
7081

7182

7283
def main():

examples/create_event.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ def create_event(arch, asset):
5151
"conformance_report": "blobs/e2a1d16c-03cd-45a1-8cd0-690831df1273",
5252
}
5353

54-
return arch.events.create(asset["identity"], props=props, attrs=attrs)
54+
return arch.events.create(asset["identity"], props=props, attrs=attrs, confirm=True)
55+
# alternatively if some work can be done whilst the event is confirmed then this call can be
56+
# replaced by a two-step alternative:
57+
58+
# event = arch.events.create(asset["identity"], props=props, attrs=attrs, confirm=False)
59+
60+
# ... do something else here
61+
# and then wait for confirmation
62+
63+
# self.arch.events.wait_for_confirmation(event['identity'])
5564

5665

5766
def create_asset(arch):

unittests/testassets.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,23 @@ def test_assets_create_with_confirmation(self):
188188
msg="CREATE method called incorrectly",
189189
)
190190

191+
def test_assets_create_with_explicit_confirmation(self):
192+
"""
193+
Test asset creation
194+
"""
195+
with mock.patch.object(
196+
self.arch._session, "post"
197+
) as mock_post, mock.patch.object(self.arch._session, "get") as mock_get:
198+
mock_post.return_value = MockResponse(200, **RESPONSE)
199+
mock_get.return_value = MockResponse(200, **RESPONSE)
200+
asset = self.arch.assets.create(BEHAVIOURS, ATTRS, confirm=False)
201+
self.arch.assets.wait_for_confirmation(asset["identity"])
202+
self.assertEqual(
203+
asset,
204+
RESPONSE,
205+
msg="CREATE method called incorrectly",
206+
)
207+
191208
def test_assets_create_with_confirmation_no_confirmed_status(self):
192209
"""
193210
Test asset confirmation

unittests/testevents.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
# pylint: disable=missing-docstring
2626
# pylint: disable=protected-access
2727
# pylint: disable=unused-variable
28+
# pylint: disable=too-many-public-methods
2829

2930
ASSET_ID = f"{ASSETS_LABEL}/xxxxxxxxxxxxxxxxxxxx"
3031

@@ -333,6 +334,25 @@ def test_events_create_with_confirmation(self):
333334
msg="CREATE method called incorrectly",
334335
)
335336

337+
def test_events_create_with_explicit_confirmation(self):
338+
"""
339+
Test event creation
340+
"""
341+
with mock.patch.object(
342+
self.arch._session, "post"
343+
) as mock_post, mock.patch.object(self.arch._session, "get") as mock_get:
344+
345+
mock_post.return_value = MockResponse(200, **RESPONSE)
346+
mock_get.return_value = MockResponse(200, **RESPONSE)
347+
348+
event = self.arch.events.create(ASSET_ID, PROPS, EVENT_ATTRS, confirm=False)
349+
self.arch.events.wait_for_confirmation(event["identity"])
350+
self.assertEqual(
351+
event,
352+
RESPONSE,
353+
msg="CREATE method called incorrectly",
354+
)
355+
336356
def test_events_create_with_confirmation_no_confirmed_status(self):
337357
"""
338358
Test asset confirmation

0 commit comments

Comments
 (0)