Skip to content

Commit a7ef224

Browse files
adamtheturtleclaude
andcommitted
Refactor generate_vumark_instance to use _target_api_request
Adds extra_headers parameter to _target_api_request and content field to Response, allowing generate_vumark_instance to reuse shared auth/signing logic instead of duplicating it. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent e48dd8c commit a7ef224

File tree

3 files changed

+17
-36
lines changed

3 files changed

+17
-36
lines changed

src/vws/query.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def query(
156156
headers=dict(requests_response.headers),
157157
request_body=requests_response.request.body,
158158
tell_position=requests_response.raw.tell(),
159+
content=bytes(requests_response.content),
159160
)
160161

161162
if response.status_code == HTTPStatus.REQUEST_ENTITY_TOO_LARGE:

src/vws/response.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ class Response:
1616
headers: dict[str, str]
1717
request_body: bytes | str | None
1818
tell_position: int
19+
content: bytes

src/vws/vws.py

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def _target_api_request(
7373
request_path: str,
7474
base_vws_url: str,
7575
request_timeout_seconds: float | tuple[float, float],
76+
extra_headers: dict[str, str] | None = None,
7677
) -> Response:
7778
"""Make a request to the Vuforia Target API.
7879
@@ -90,6 +91,7 @@ def _target_api_request(
9091
request_timeout_seconds: The timeout for the request, as used by
9192
``requests.request``. This can be a float to set both the
9293
connect and read timeouts, or a (connect, read) tuple.
94+
extra_headers: Additional headers to include in the request.
9395
9496
Returns:
9597
The response to the request made by `requests`.
@@ -110,6 +112,7 @@ def _target_api_request(
110112
"Authorization": signature_string,
111113
"Date": date_string,
112114
"Content-Type": content_type,
115+
**(extra_headers or {}),
113116
}
114117

115118
url = urljoin(base=base_vws_url, url=request_path)
@@ -129,6 +132,7 @@ def _target_api_request(
129132
headers=dict(requests_response.headers),
130133
request_body=requests_response.request.body,
131134
tell_position=requests_response.raw.tell(),
135+
content=bytes(requests_response.content),
132136
)
133137

134138

@@ -752,54 +756,29 @@ def generate_vumark_instance(
752756
request_data = json.dumps(obj={"instance_id": instance_id}).encode(
753757
encoding="utf-8",
754758
)
755-
date_string = rfc_1123_date()
756759

757-
signature_string = authorization_header(
758-
access_key=self._server_access_key,
759-
secret_key=self._server_secret_key,
760-
method=HTTPMethod.POST,
761-
content=request_data,
760+
response = _target_api_request(
762761
content_type=content_type,
763-
date=date_string,
764-
request_path=request_path,
765-
)
766-
767-
headers = {
768-
"Authorization": signature_string,
769-
"Date": date_string,
770-
"Content-Type": content_type,
771-
"Accept": accept,
772-
}
773-
774-
url = urljoin(base=self._base_vws_url, url=request_path)
775-
776-
requests_response = requests.request(
762+
server_access_key=self._server_access_key,
763+
server_secret_key=self._server_secret_key,
777764
method=HTTPMethod.POST,
778-
url=url,
779-
headers=headers,
780765
data=request_data,
781-
timeout=self._request_timeout_seconds,
766+
request_path=request_path,
767+
base_vws_url=self._base_vws_url,
768+
request_timeout_seconds=self._request_timeout_seconds,
769+
extra_headers={"Accept": accept},
782770
)
783771

784-
if requests_response.status_code == HTTPStatus.OK:
785-
return bytes(requests_response.content)
786-
787-
response = Response(
788-
text=requests_response.text,
789-
url=requests_response.url,
790-
status_code=requests_response.status_code,
791-
headers=dict(requests_response.headers),
792-
request_body=requests_response.request.body,
793-
tell_position=requests_response.raw.tell(),
794-
)
772+
if response.status_code == HTTPStatus.OK:
773+
return response.content
795774

796775
if (
797-
requests_response.status_code == HTTPStatus.TOO_MANY_REQUESTS
776+
response.status_code == HTTPStatus.TOO_MANY_REQUESTS
798777
): # pragma: no cover
799778
raise TooManyRequestsError(response=response)
800779

801780
if (
802-
requests_response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR
781+
response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR
803782
): # pragma: no cover
804783
raise ServerError(response=response)
805784

0 commit comments

Comments
 (0)