Skip to content

Commit aab1e24

Browse files
committed
Assets create should use Properties
Problem: Assets create method allows user to chosse behaviours. This feature is deprecated in favour of using a properties argumant. Solution: Remove the use of behaviours in the arg list and replace with a properties argument. Currently only the storage integrity setting is allowed in the properties argument. storage integrity is removed from the arguments to assets.create. This will be consistent with other methods across the different endpoints of the API. Signed-off-by: Paul Hewlett <phewlett76@gmail.com>
1 parent e82387d commit aab1e24

6 files changed

Lines changed: 45 additions & 70 deletions

File tree

.github/workflows/python-package.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ jobs:
2727
- name: Run integrity checks
2828
run: |
2929
./scripts/version.sh
30-
pycodestyle --format=pylint archivist unittests examples
31-
python3 -m pylint --rcfile=pylintrc archivist unittests examples
32-
black archivist examples unittests
30+
pycodestyle --format=pylint archivist unittests examples functests
31+
python3 -m pylint --rcfile=pylintrc archivist unittests examples functests
32+
black archivist examples unittests functests
3333
modified=$(git status -s | wc -l)
3434
if [ $modified -gt 0 ]
3535
then

archivist/assets.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222
"""
2323

2424
import logging
25-
from typing import Dict, List, Optional
25+
from typing import Dict, Optional
2626
from copy import deepcopy
2727

28-
from archivist.storage_integrity import StorageIntegrity
2928
from archivist.type_aliases import NoneOnError
3029

3130
# pylint:disable=unused-import # To prevent cyclical import errors forward referencing is used
@@ -46,6 +45,13 @@
4645
#: be changed.
4746
DEFAULT_PAGE_SIZE = 500
4847

48+
# These are now hardcoded and not user-selectable. Eventually they will be removed from
49+
# the backend API and removed from this package.
50+
BEHAVIOURS = [
51+
"Attachments",
52+
"RecordEvidence",
53+
]
54+
4955
LOGGER = logging.getLogger(__name__)
5056

5157

@@ -112,35 +118,28 @@ def __init__(self, archivist: "type_helper.Archivist"):
112118

113119
def create(
114120
self,
115-
behaviours: List,
121+
props: Dict,
116122
attrs: Dict,
117123
*,
118-
storage_integrity: StorageIntegrity = StorageIntegrity.TENANT_STORAGE,
119124
confirm: bool = False,
120125
) -> Asset:
121126
"""Create asset
122127
123-
Creates asset with defined behaviours and attributes.
128+
Creates asset with defined properties and attributes.
124129
125130
Args:
126-
behaviours (list): list of accepted behaviours for this asset.
131+
props (list): Properties - usually only the storage_integrity setting
127132
attrs (dict): attributes of created asset.
128-
storage_integrity (StorageIntegrity): store asset on either ledger or tenant storage
129133
confirm (bool): if True wait for asset to be confirmed.
130134
131135
Returns:
132136
:class:`Asset` instance
133137
134138
"""
135139
LOGGER.debug("Create Asset %s", attrs)
136-
return self.create_from_data(
137-
{
138-
"behaviours": behaviours,
139-
"storage_integrity": storage_integrity.name,
140-
"attributes": attrs,
141-
},
142-
confirm=confirm,
143-
)
140+
data = self.__query(props, attrs)
141+
data["behaviours"] = BEHAVIOURS
142+
return self.create_from_data(data, confirm=confirm)
144143

145144
def create_from_data(self, data: Dict, *, confirm: bool = False) -> Asset:
146145
"""Create asset

examples/create_asset.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,27 @@ def create_asset(arch):
3636
"some_custom_attribute": "value" # You can add any custom value as long as
3737
# it does not start with arc_
3838
}
39-
behaviours = [
40-
"Attachments",
41-
"RecordEvidence",
42-
]
4339
#
4440
# store asset on the DLT or not. If DLT is not enabled for the user an error will occur if
4541
# StorageIntegrity.LEDGER is specified. If unspecified then TENANT_STORAGE is used
4642
# i.e. not stored on the DLT...
4743
# storage_integrity = StorageIntegrity.TENANT_STORAGE
48-
storage_integrity = StorageIntegrity.LEDGER
44+
props = {
45+
"storage_integrity": StorageIntegrity.LEDGER,
46+
}
4947

50-
# The first argument is the behaviours of the asset
51-
# The second argument is the attributes of the asset
52-
# The third argument indicates whether the asset is stored on the DLT or not.
53-
# If not specifed the asset is not stored on the DLT (TENANT_STORAGE)
54-
# The fourth argument is wait for confirmation:
48+
# The first argument are the properties of the asset
49+
# The second argument are the attributes of the asset
50+
# The third argument is wait for confirmation:
5551
# If @confirm@ is True then this function will not
5652
# return until the asset is confirmed on the blockchain and ready
5753
# to accept events (or an error occurs)
5854
#
59-
# If storage_integrity = StorageIntegrity.LEDGER:
60-
# After an asset is submitted to the blockchain,
61-
# it will be in the "Pending" status.
62-
# Once it is added to the blockchain, the status will be changed to "Confirmed"
63-
#
64-
# If storage_integrity = StorageIntegrity.TENANT_STORAGE:
65-
# The asset is simply stored in the backend (and not on the blockchain)
66-
# and, once stored, the status will be changed to "Confirmed".
67-
return arch.assets.create(
68-
behaviours, attrs, storage_integrity=storage_integrity, confirm=True
69-
)
55+
return arch.assets.create(props, attrs, confirm=True)
7056
# alternatively if some work can be done whilst the asset is confirmed then this call can be
7157
# replaced by a two-step alternative:
7258

73-
# asset = arch.assets.create(
74-
# behaviours, attrs, storage_integrity=storage_integrity, confirm=False
75-
# )
59+
# asset = arch.assets.create(props, attrs, confirm=False)
7660

7761
# ... do something else here
7862
# and then wait for confirmation

examples/create_event.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,8 @@ def create_asset(arch):
8989
"some_custom_attribute": "value" # You can add any custom value as long as
9090
# it does not start with arc_
9191
}
92-
behaviours = [
93-
"Attachments",
94-
"RecordEvidence",
95-
]
96-
97-
# The first argument is the behaviours of the asset
92+
props = {}
93+
# The first argument is the properties of the asset
9894
# The second argument is the attributes of the asset
9995
# The third argument is wait for confirmation:
10096
# If @confirm@ is True then this function will not
@@ -103,7 +99,7 @@ def create_asset(arch):
10399
# After an asset is submitted to the blockchain (submitted),
104100
# it will be in the "Pending" status.
105101
# Once it is added to the blockchain, the status will be changed to "Confirmed"
106-
return arch.assets.create(behaviours, attrs, confirm=True)
102+
return arch.assets.create(props, attrs, confirm=True)
107103

108104

109105
def main():

functests/execassets.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
# pylint: disable=missing-docstring
1414
# pylint: disable=unused-variable
1515

16-
BEHAVIOURS = [
17-
"RecordEvidence",
18-
"Attachments",
19-
]
16+
PROPS = {}
2017
ATTRS = {
2118
"arc_firmware_version": "1.0",
2219
"arc_serial_number": "vtl-x4-07",
@@ -46,7 +43,7 @@ def test_asset_create_tenant_storage(self):
4643
Test asset creation on tenant storage
4744
"""
4845
asset = self.arch.assets.create(
49-
BEHAVIOURS,
46+
PROPS,
5047
self.attrs,
5148
confirm=True,
5249
)
@@ -61,9 +58,10 @@ def test_asset_create_ledger(self):
6158
Test asset creation on ledger
6259
"""
6360
asset = self.arch.assets.create(
64-
BEHAVIOURS,
61+
{
62+
"storage_integrity": StorageIntegrity.LEDGER,
63+
},
6564
self.attrs,
66-
storage_integrity=StorageIntegrity.LEDGER,
6765
confirm=True,
6866
)
6967
self.assertEqual(

unittests/testassets.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from unittest import TestCase, mock
88

99
from archivist.archivist import Archivist
10-
from archivist.assets import DEFAULT_PAGE_SIZE
10+
from archivist.assets import BEHAVIOURS, DEFAULT_PAGE_SIZE
1111
from archivist.constants import (
1212
ASSETS_LABEL,
1313
ASSETS_SUBPATH,
@@ -25,10 +25,6 @@
2525
# pylint: disable=unused-variable
2626

2727

28-
BEHAVIOURS = [
29-
"RecordEvidence",
30-
"Attachments",
31-
]
3228
PRIMARY_IMAGE = {
3329
"arc_display_name": "arc_primary_image",
3430
"arc_attachment_identity": "blobs/87b1a84c-1c6f-442b-923e-a97516f4d275",
@@ -76,11 +72,13 @@
7672
IDENTITY = f"{ASSETS_LABEL}/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
7773
SUBPATH = f"{ASSETS_SUBPATH}/{ASSETS_LABEL}"
7874

79-
# TBD: add properties as well
75+
PROPS = {
76+
"storage_integrity": StorageIntegrity.TENANT_STORAGE.name,
77+
}
8078
REQUEST = {
81-
"behaviours": BEHAVIOURS,
8279
"storage_integrity": StorageIntegrity.TENANT_STORAGE.name,
8380
"attributes": ATTRS,
81+
"behaviours": BEHAVIOURS,
8482
}
8583
REQUEST_DATA = json.dumps(REQUEST)
8684

@@ -136,7 +134,7 @@ def test_assets_create(self):
136134
with mock.patch.object(self.arch._session, "post") as mock_post:
137135
mock_post.return_value = MockResponse(200, **RESPONSE)
138136

139-
asset = self.arch.assets.create(BEHAVIOURS, ATTRS, confirm=False)
137+
asset = self.arch.assets.create(PROPS, ATTRS, confirm=False)
140138
self.assertEqual(
141139
tuple(mock_post.call_args),
142140
(
@@ -178,7 +176,7 @@ def test_assets_create_with_confirmation(self):
178176
) as mock_post, mock.patch.object(self.arch._session, "get") as mock_get:
179177
mock_post.return_value = MockResponse(200, **RESPONSE)
180178
mock_get.return_value = MockResponse(200, **RESPONSE)
181-
asset = self.arch.assets.create(BEHAVIOURS, ATTRS, confirm=True)
179+
asset = self.arch.assets.create(PROPS, ATTRS, confirm=True)
182180
self.assertEqual(
183181
asset,
184182
RESPONSE,
@@ -194,7 +192,7 @@ def test_assets_create_with_explicit_confirmation(self):
194192
) as mock_post, mock.patch.object(self.arch._session, "get") as mock_get:
195193
mock_post.return_value = MockResponse(200, **RESPONSE)
196194
mock_get.return_value = MockResponse(200, **RESPONSE)
197-
asset = self.arch.assets.create(BEHAVIOURS, ATTRS, confirm=False)
195+
asset = self.arch.assets.create(PROPS, ATTRS, confirm=False)
198196
self.arch.assets.wait_for_confirmation(asset["identity"])
199197
self.assertEqual(
200198
asset,
@@ -213,7 +211,7 @@ def test_assets_create_with_confirmation_no_confirmed_status(self):
213211
mock_get.return_value = MockResponse(200, **RESPONSE_NO_CONFIRMATION)
214212

215213
with self.assertRaises(ArchivistUnconfirmedError):
216-
asset = self.arch.assets.create(BEHAVIOURS, ATTRS, confirm=True)
214+
asset = self.arch.assets.create(PROPS, ATTRS, confirm=True)
217215

218216
def test_assets_create_with_confirmation_pending_status(self):
219217
"""
@@ -227,7 +225,7 @@ def test_assets_create_with_confirmation_pending_status(self):
227225
MockResponse(200, **RESPONSE_PENDING),
228226
MockResponse(200, **RESPONSE),
229227
]
230-
asset = self.arch.assets.create(BEHAVIOURS, ATTRS, confirm=True)
228+
asset = self.arch.assets.create(PROPS, ATTRS, confirm=True)
231229
self.assertEqual(
232230
asset,
233231
RESPONSE,
@@ -247,7 +245,7 @@ def test_assets_create_with_confirmation_failed_status(self):
247245
MockResponse(200, **RESPONSE_FAILED),
248246
]
249247
with self.assertRaises(ArchivistUnconfirmedError):
250-
asset = self.arch.assets.create(BEHAVIOURS, ATTRS, confirm=True)
248+
asset = self.arch.assets.create(PROPS, ATTRS, confirm=True)
251249

252250
def test_assets_create_with_confirmation_always_pending_status(self):
253251
"""
@@ -267,7 +265,7 @@ def test_assets_create_with_confirmation_always_pending_status(self):
267265
MockResponse(200, **RESPONSE_PENDING),
268266
]
269267
with self.assertRaises(ArchivistUnconfirmedError):
270-
asset = self.arch.assets.create(BEHAVIOURS, ATTRS, confirm=True)
268+
asset = self.arch.assets.create(PROPS, ATTRS, confirm=True)
271269

272270
def test_assets_read_with_out_primary_image(self):
273271
"""

0 commit comments

Comments
 (0)