Skip to content

Commit 14940f0

Browse files
Merge pull request #1119 from adamtheturtle/no-base64
Do not encode application metadata as base64 - require user to do that
2 parents 8144aed + 71eca15 commit 14940f0

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

src/vws/vws.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def add_target(
142142
width: Union[int, float],
143143
image: io.BytesIO,
144144
active_flag: bool,
145-
application_metadata: Optional[bytes],
145+
application_metadata: Optional[str],
146146
) -> str:
147147
"""
148148
Add a target to a Vuforia Web Services database.
@@ -157,7 +157,9 @@ def add_target(
157157
image: The image of the target.
158158
active_flag: Whether or not the target is active for query.
159159
application_metadata: The application metadata of the target.
160-
This will be base64 encoded.
160+
This must be base64 encoded, for example by using::
161+
162+
base64.b64encode('input_string').decode('ascii')
161163
162164
Returns:
163165
The target ID of the new target.
@@ -185,18 +187,13 @@ def add_target(
185187
"""
186188
image_data = image.getvalue()
187189
image_data_encoded = base64.b64encode(image_data).decode('ascii')
188-
if application_metadata is None:
189-
metadata_encoded = None
190-
else:
191-
metadata_encoded_str = base64.b64encode(application_metadata)
192-
metadata_encoded = metadata_encoded_str.decode('ascii')
193190

194191
data = {
195192
'name': name,
196193
'width': width,
197194
'image': image_data_encoded,
198195
'active_flag': active_flag,
199-
'application_metadata': metadata_encoded,
196+
'application_metadata': application_metadata,
200197
}
201198

202199
content = bytes(json.dumps(data), encoding='utf-8')
@@ -478,7 +475,7 @@ def update_target(
478475
width: Optional[Union[int, float]] = None,
479476
image: Optional[io.BytesIO] = None,
480477
active_flag: Optional[bool] = None,
481-
application_metadata: Optional[bytes] = None,
478+
application_metadata: Optional[str] = None,
482479
) -> None:
483480
"""
484481
Add a target to a Vuforia Web Services database.
@@ -494,8 +491,11 @@ def update_target(
494491
image: The image of the target.
495492
active_flag: Whether or not the target is active for query.
496493
application_metadata: The application metadata of the target.
497-
This will be base64 encoded. Giving ``None`` will not change
498-
the application metadata.
494+
This must be base64 encoded, for example by using::
495+
496+
base64.b64encode('input_string').decode('ascii')
497+
498+
Giving ``None`` will not change the application metadata.
499499
500500
Raises:
501501
~vws.exceptions.AuthenticationFailure: The secret key is not
@@ -531,9 +531,7 @@ def update_target(
531531
data['active_flag'] = active_flag
532532

533533
if application_metadata is not None:
534-
metadata_encoded_str = base64.b64encode(application_metadata)
535-
metadata_encoded = metadata_encoded_str.decode('ascii')
536-
data['application_metadata'] = metadata_encoded
534+
data['application_metadata'] = application_metadata
537535

538536
content = bytes(json.dumps(data), encoding='utf-8')
539537

tests/test_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def test_metadata_too_large(
225225
width=1,
226226
image=high_quality_image,
227227
active_flag=True,
228-
application_metadata=b'a' * 1024 * 1024,
228+
application_metadata='a' * 1024 * 1024 * 10,
229229
)
230230

231231
assert exc.value.response.status_code == codes.UNPROCESSABLE_ENTITY

tests/test_vws.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
Tests for helper functions for managing a Vuforia database.
33
"""
44

5+
import base64
56
import io
67
from typing import Optional
78

89
import pytest
910
from mock_vws import MockVWS
1011
from mock_vws.database import VuforiaDatabase
1112

12-
from vws import VWS
13+
from vws import VWS, CloudRecoService
1314
from vws.exceptions import TargetProcessingTimeout
1415

1516

@@ -26,23 +27,40 @@ def test_add_target(
2627
high_quality_image: io.BytesIO,
2728
active_flag: bool,
2829
application_metadata: Optional[bytes],
30+
cloud_reco_client: CloudRecoService,
2931
) -> None:
3032
"""
3133
No exception is raised when adding one target.
3234
"""
3335
name = 'x'
3436
width = 1
37+
if application_metadata is None:
38+
encoded_metadata = None
39+
else:
40+
encoded_metadata_bytes = base64.b64encode(application_metadata)
41+
encoded_metadata = encoded_metadata_bytes.decode('utf-8')
42+
3543
target_id = vws_client.add_target(
3644
name=name,
3745
width=width,
3846
image=high_quality_image,
39-
application_metadata=application_metadata,
47+
application_metadata=encoded_metadata,
4048
active_flag=active_flag,
4149
)
4250
target_record = vws_client.get_target_record(target_id=target_id)
4351
assert target_record['name'] == name
4452
assert target_record['width'] == width
4553
assert target_record['active_flag'] is active_flag
54+
vws_client.wait_for_target_processed(target_id=target_id)
55+
matching_targets = cloud_reco_client.query(image=high_quality_image)
56+
if active_flag:
57+
[matching_target] = matching_targets
58+
assert matching_target['target_id'] == target_id
59+
query_target_data = matching_target['target_data']
60+
query_metadata = query_target_data['application_metadata']
61+
assert query_metadata == encoded_metadata
62+
else:
63+
assert matching_targets == []
4664

4765
def test_add_two_targets(
4866
self,
@@ -475,7 +493,7 @@ def test_update_target(
475493
width=2,
476494
active_flag=False,
477495
image=image_file_failed_state,
478-
application_metadata=b'a',
496+
application_metadata=base64.b64encode(b'a').decode('ascii'),
479497
)
480498

481499
vws_client.wait_for_target_processed(target_id=target_id)

0 commit comments

Comments
 (0)