Skip to content

Commit 3de072b

Browse files
committed
Fix lint issues
1 parent 5443d81 commit 3de072b

File tree

3 files changed

+75
-14
lines changed

3 files changed

+75
-14
lines changed

src/vws/exceptions.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Custom exceptions for Vuforia errors.
3+
4+
Generated with the script ``_generate_exceptions.py``.
5+
"""
6+
7+
from requests import Response
8+
9+
10+
class UnknownTarget(Exception):
11+
"""
12+
Exception raised when Vuforia returns a response with a result code
13+
'UnknownTarget'.
14+
"""
15+
16+
def __init__(self, response: Response) -> None:
17+
"""
18+
Args:
19+
response: The response to a request to Vuforia.
20+
"""
21+
super().__init__()
22+
self.response = response

src/vws/vws.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import base64
66
import io
77
import json
8+
from enum import Enum
89
from time import sleep
910
from typing import Dict, List, Union
1011
from urllib.parse import urljoin
@@ -14,6 +15,7 @@
1415
from requests import Response
1516

1617
from vws._authorization import authorization_header, rfc_1123_date
18+
from vws.exceptions import UnknownTarget
1719

1820

1921
def _target_api_request(
@@ -74,6 +76,39 @@ def _target_api_request(
7476
return response
7577

7678

79+
class _ResultCodes(Enum):
80+
"""
81+
Constants representing various VWS result codes.
82+
83+
See
84+
https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Interperete-VWS-API-Result-Codes
85+
86+
Some codes here are not documented in the above link.
87+
"""
88+
89+
SUCCESS = 'Success'
90+
TARGET_CREATED = 'TargetCreated'
91+
AUTHENTICATION_FAILURE = 'AuthenticationFailure'
92+
REQUEST_TIME_TOO_SKEWED = 'RequestTimeTooSkewed'
93+
TARGET_NAME_EXIST = 'TargetNameExist'
94+
UNKNOWN_TARGET = 'UnknownTarget'
95+
BAD_IMAGE = 'BadImage'
96+
IMAGE_TOO_LARGE = 'ImageTooLarge'
97+
METADATA_TOO_LARGE = 'MetadataTooLarge'
98+
DATE_RANGE_ERROR = 'DateRangeError'
99+
FAIL = 'Fail'
100+
TARGET_STATUS_PROCESSING = 'TargetStatusProcessing'
101+
REQUEST_QUOTA_REACHED = 'RequestQuotaReached'
102+
TARGET_STATUS_NOT_SUCCESS = 'TargetStatusNotSuccess'
103+
PROJECT_INACTIVE = 'ProjectInactive'
104+
INACTIVE_PROJECT = 'InactiveProject'
105+
106+
107+
_EXCEPTIONS = {
108+
_ResultCodes.UNKNOWN_TARGET: UnknownTarget,
109+
}
110+
111+
77112
class VWS:
78113
"""
79114
An interface to Vuforia Web Services APIs.
@@ -162,7 +197,12 @@ def get_target_record(self, target_id: str) -> Dict[str, Union[str, int]]:
162197
base_vws_url=self._base_vws_url,
163198
)
164199

165-
return dict(response.json()['target_record'])
200+
result_code = response.json()['result_code']
201+
if _ResultCodes(result_code) == _ResultCodes.SUCCESS:
202+
return dict(response.json()['target_record'])
203+
204+
exception = _EXCEPTIONS[_ResultCodes(result_code)]
205+
raise exception(response=response)
166206

167207
@timeout_decorator.timeout(seconds=60 * 5)
168208
def wait_for_target_processed(self, target_id: str) -> None:

tests/test_unknown_target.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
"""
2-
Tests for passing invalid target IDs to endpoints which
3-
require a target ID to be given.
2+
Tests for passing invalid target IDs to helpers which require a target ID to
3+
be given.
44
"""
55

6-
from functools import partial
7-
86
from vws import VWS
97

108

119
class TestInvalidGivenID:
1210
"""
13-
Tests for giving an invalid ID to endpoints which require a target ID to
14-
be given.
11+
Tests for giving an invalid ID to helpers which require a target ID to be
12+
given.
1513
"""
1614

17-
def test_invalid_given_id(
18-
self,
19-
client: VWS,
20-
):
21-
get_target_record = partial(client.get_target_record)
22-
15+
def test_invalid_given_id(self, client: VWS) -> None:
16+
"""
17+
Giving an invalid ID to a helper which requires a target ID to be given
18+
causes an ``UnknownTarget`` exception to be raised.
19+
"""
2320
funcs = (
24-
get_target_record,
21+
client.get_target_record,
22+
client.get_target_summary_report,
23+
client.delete_target,
2524
)
2625

2726
for func in funcs:

0 commit comments

Comments
 (0)