Skip to content

Commit 4bdab9d

Browse files
adamtheturtleclaude
andcommitted
Extract TooManyRequests and ServerError checks to target_api_request
Move HTTP status code checks for TooManyRequests (429) and ServerError (5xx) from VWS.make_request and VuMarkService.generate_instance_uuid into target_api_request, eliminating duplication and centralizing error handling. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
1 parent 60e5a6e commit 4bdab9d

File tree

3 files changed

+26
-31
lines changed

3 files changed

+26
-31
lines changed

src/vws/_vws_request.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
API.
33
"""
44

5+
from http import HTTPStatus
6+
57
from beartype import BeartypeConf, beartype
68
from vws_auth_tools import authorization_header, rfc_1123_date
79

10+
from vws.exceptions.custom_exceptions import ServerError
11+
from vws.exceptions.vws_exceptions import TooManyRequestsError
812
from vws.response import Response
913
from vws.transports import Transport
1014

@@ -45,6 +49,12 @@ def target_api_request(
4549
4650
Returns:
4751
The response to the request.
52+
53+
Raises:
54+
~vws.exceptions.custom_exceptions.ServerError:
55+
There is an error with Vuforia's servers.
56+
~vws.exceptions.vws_exceptions.TooManyRequestsError:
57+
Vuforia is rate limiting access.
4858
"""
4959
date_string = rfc_1123_date()
5060

@@ -67,10 +77,23 @@ def target_api_request(
6777

6878
url = base_vws_url.rstrip("/") + request_path
6979

70-
return transport(
80+
response = transport(
7181
method=method,
7282
url=url,
7383
headers=headers,
7484
data=data,
7585
request_timeout=request_timeout_seconds,
7686
)
87+
88+
if (
89+
response.status_code == HTTPStatus.TOO_MANY_REQUESTS
90+
): # pragma: no cover
91+
# The Vuforia API returns a 429 response with no JSON body.
92+
raise TooManyRequestsError(response=response)
93+
94+
if (
95+
response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR
96+
): # pragma: no cover
97+
raise ServerError(response=response)
98+
99+
return response

src/vws/vumark_service.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from beartype import BeartypeConf, beartype
77

88
from vws._vws_request import target_api_request
9-
from vws.exceptions.custom_exceptions import ServerError
109
from vws.exceptions.vws_exceptions import (
1110
AuthenticationFailureError,
1211
BadRequestError,
@@ -17,7 +16,6 @@
1716
InvalidTargetTypeError,
1817
RequestTimeTooSkewedError,
1918
TargetStatusNotSuccessError,
20-
TooManyRequestsError,
2119
UnknownTargetError,
2220
)
2321
from vws.transports import RequestsTransport, Transport
@@ -119,17 +117,6 @@ def generate_vumark_instance(
119117
transport=self._transport,
120118
)
121119

122-
if (
123-
response.status_code == HTTPStatus.TOO_MANY_REQUESTS
124-
): # pragma: no cover
125-
# The Vuforia API returns a 429 response with no JSON body.
126-
raise TooManyRequestsError(response=response)
127-
128-
if (
129-
response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR
130-
): # pragma: no cover
131-
raise ServerError(response=response)
132-
133120
if response.status_code == HTTPStatus.OK:
134121
return response.content
135122

src/vws/vws.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44
import json
55
import time
66
from datetime import date
7-
from http import HTTPMethod, HTTPStatus
7+
from http import HTTPMethod
88

99
from beartype import BeartypeConf, beartype
1010

1111
from vws._image_utils import ImageType as _ImageType
1212
from vws._image_utils import get_image_data as _get_image_data
1313
from vws._vws_request import target_api_request
14-
from vws.exceptions.custom_exceptions import (
15-
ServerError,
16-
TargetProcessingTimeoutError,
17-
)
14+
from vws.exceptions.custom_exceptions import TargetProcessingTimeoutError
1815
from vws.exceptions.vws_exceptions import (
1916
AuthenticationFailureError,
2017
BadImageError,
@@ -32,7 +29,6 @@
3229
TargetQuotaReachedError,
3330
TargetStatusNotSuccessError,
3431
TargetStatusProcessingError,
35-
TooManyRequestsError,
3632
UnknownTargetError,
3733
)
3834
from vws.reports import (
@@ -129,17 +125,6 @@ def make_request(
129125
transport=self._transport,
130126
)
131127

132-
if (
133-
response.status_code == HTTPStatus.TOO_MANY_REQUESTS
134-
): # pragma: no cover
135-
# The Vuforia API returns a 429 response with no JSON body.
136-
raise TooManyRequestsError(response=response)
137-
138-
if (
139-
response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR
140-
): # pragma: no cover
141-
raise ServerError(response=response)
142-
143128
result_code = json.loads(s=response.text)["result_code"]
144129

145130
if result_code == expected_result_code:

0 commit comments

Comments
 (0)