-
Notifications
You must be signed in to change notification settings - Fork 7
Add VuMark generation support #2858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b87b0e6
Add VuMark generation support
adamtheturtle 6373dae
Add BadRequestError and bump vws-python-mock to 2026.2.18.2
adamtheturtle 64c1869
Add 'enum' to spelling private dict
adamtheturtle 913a449
Assert format-specific content in VuMark tests
adamtheturtle ee84b96
Remove default for accept parameter in generate_vumark_instance
adamtheturtle cb175d4
Remove now-unnecessary vulture ignore for SVG and PDF enum members
adamtheturtle 44445d1
Add BadRequest, InvalidAcceptHeader, InvalidInstanceId to spelling dict
adamtheturtle e7552e1
Use double backticks for result codes in exception docstrings
adamtheturtle 7bae5a5
Refactor generate_vumark_instance to use _target_api_request
adamtheturtle 064f482
Move 429/5xx handling into _target_api_request
adamtheturtle e8453c4
Use make_request in generate_vumark_instance
adamtheturtle c553cd1
Move 429/5xx handling back to make_request
adamtheturtle ff0f22f
Make extra_headers required in _target_api_request
adamtheturtle a2b6332
Address PR comments: VuMark target type and target_id fix
adamtheturtle 0fc38aa
Move generate_vumark_instance to a new VuMarkService class
adamtheturtle e3fbac2
Fix CI failures: spelling, is_vumark_template, test placeholder
adamtheturtle b8a2a2b
Move target_api_request to shared _vws_request module
adamtheturtle 080e79f
Fix VuMarkTarget import: use mock_vws.target not mock_vws.database
adamtheturtle 90c00a1
Remove dead expected_result_code=None branch from VWS.make_request
adamtheturtle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| """A library for Vuforia Web Services.""" | ||
|
|
||
| from .query import CloudRecoService | ||
| from .vumark_service import VuMarkService | ||
| from .vws import VWS | ||
|
|
||
| __all__ = [ | ||
| "VWS", | ||
| "CloudRecoService", | ||
| "VuMarkService", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| """Internal helper for making authenticated requests to the Vuforia Target | ||
| API. | ||
| """ | ||
|
|
||
| from urllib.parse import urljoin | ||
|
|
||
| import requests | ||
| from beartype import BeartypeConf, beartype | ||
| from vws_auth_tools import authorization_header, rfc_1123_date | ||
|
|
||
| from vws.response import Response | ||
|
|
||
|
|
||
| @beartype(conf=BeartypeConf(is_pep484_tower=True)) | ||
| def target_api_request( | ||
| *, | ||
| content_type: str, | ||
| server_access_key: str, | ||
| server_secret_key: str, | ||
| method: str, | ||
| data: bytes, | ||
| request_path: str, | ||
| base_vws_url: str, | ||
| request_timeout_seconds: float | tuple[float, float], | ||
| extra_headers: dict[str, str], | ||
| ) -> Response: | ||
| """Make a request to the Vuforia Target API. | ||
|
|
||
| This uses `requests` to make a request against https://vws.vuforia.com. | ||
|
|
||
| Args: | ||
| content_type: The content type of the request. | ||
| server_access_key: A VWS server access key. | ||
| server_secret_key: A VWS server secret key. | ||
| method: The HTTP method which will be used in the request. | ||
| data: The request body which will be used in the request. | ||
| request_path: The path to the endpoint which will be used in the | ||
| request. | ||
| base_vws_url: The base URL for the VWS API. | ||
| request_timeout_seconds: The timeout for the request, as used by | ||
| ``requests.request``. This can be a float to set both the | ||
| connect and read timeouts, or a (connect, read) tuple. | ||
| extra_headers: Additional headers to include in the request. | ||
|
|
||
| Returns: | ||
| The response to the request made by `requests`. | ||
| """ | ||
| date_string = rfc_1123_date() | ||
|
|
||
| signature_string = authorization_header( | ||
| access_key=server_access_key, | ||
| secret_key=server_secret_key, | ||
| method=method, | ||
| content=data, | ||
| content_type=content_type, | ||
| date=date_string, | ||
| request_path=request_path, | ||
| ) | ||
|
|
||
| headers = { | ||
| "Authorization": signature_string, | ||
| "Date": date_string, | ||
| "Content-Type": content_type, | ||
| **extra_headers, | ||
| } | ||
|
|
||
| url = urljoin(base=base_vws_url, url=request_path) | ||
|
|
||
| requests_response = requests.request( | ||
| method=method, | ||
| url=url, | ||
| headers=headers, | ||
| data=data, | ||
| timeout=request_timeout_seconds, | ||
| ) | ||
|
|
||
| return Response( | ||
| text=requests_response.text, | ||
| url=requests_response.url, | ||
| status_code=requests_response.status_code, | ||
| headers=dict(requests_response.headers), | ||
| request_body=requests_response.request.body, | ||
| tell_position=requests_response.raw.tell(), | ||
| content=bytes(requests_response.content), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,3 +16,4 @@ class Response: | |
| headers: dict[str, str] | ||
| request_body: bytes | str | None | ||
| tell_position: int | ||
| content: bytes | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| """Tools for managing ``VWS.generate_vumark_instance``'s ``accept``.""" | ||
|
|
||
| from enum import StrEnum, unique | ||
|
|
||
| from beartype import beartype | ||
|
|
||
|
|
||
| @beartype | ||
| @unique | ||
| class VuMarkAccept(StrEnum): | ||
| """ | ||
| Options for the ``accept`` parameter of | ||
| ``VWS.generate_vumark_instance``. | ||
| """ | ||
|
|
||
| PNG = "image/png" | ||
| SVG = "image/svg+xml" | ||
| PDF = "application/pdf" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| """Interface to the Vuforia VuMark Generation Web API.""" | ||
|
|
||
| import json | ||
| from http import HTTPMethod, HTTPStatus | ||
|
|
||
| from beartype import BeartypeConf, beartype | ||
|
|
||
| from vws._vws_request import target_api_request | ||
| from vws.exceptions.custom_exceptions import ServerError | ||
| from vws.exceptions.vws_exceptions import ( | ||
| AuthenticationFailureError, | ||
| BadRequestError, | ||
| DateRangeError, | ||
| FailError, | ||
| InvalidAcceptHeaderError, | ||
| InvalidInstanceIdError, | ||
| InvalidTargetTypeError, | ||
| RequestTimeTooSkewedError, | ||
| TargetStatusNotSuccessError, | ||
| TooManyRequestsError, | ||
| UnknownTargetError, | ||
| ) | ||
| from vws.vumark_accept import VuMarkAccept | ||
|
|
||
|
|
||
| @beartype(conf=BeartypeConf(is_pep484_tower=True)) | ||
| class VuMarkService: | ||
| """An interface to the Vuforia VuMark Generation Web API.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| server_access_key: str, | ||
| server_secret_key: str, | ||
| base_vws_url: str = "https://vws.vuforia.com", | ||
| request_timeout_seconds: float | tuple[float, float] = 30.0, | ||
| ) -> None: | ||
| """ | ||
| Args: | ||
| server_access_key: A VWS server access key. | ||
| server_secret_key: A VWS server secret key. | ||
| base_vws_url: The base URL for the VWS API. | ||
| request_timeout_seconds: The timeout for each HTTP request, as | ||
| used by ``requests.request``. This can be a float to set | ||
| both the connect and read timeouts, or a (connect, read) | ||
| tuple. | ||
| """ | ||
| self._server_access_key = server_access_key | ||
| self._server_secret_key = server_secret_key | ||
| self._base_vws_url = base_vws_url | ||
| self._request_timeout_seconds = request_timeout_seconds | ||
|
|
||
| def generate_vumark_instance( | ||
| self, | ||
| *, | ||
| target_id: str, | ||
| instance_id: str, | ||
| accept: VuMarkAccept, | ||
| ) -> bytes: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No default accept format despite stated behaviorLow Severity
|
||
| """Generate a VuMark instance image. | ||
|
|
||
| See | ||
| https://developer.vuforia.com/library/vuforia-engine/web-api/vumark-generation-web-api/ | ||
| for parameter details. | ||
|
|
||
| Args: | ||
| target_id: The ID of the VuMark target. | ||
| instance_id: The instance ID to encode in the VuMark. | ||
| accept: The image format to return. | ||
|
|
||
| Returns: | ||
| The VuMark instance image bytes. | ||
|
|
||
| Raises: | ||
| ~vws.exceptions.vws_exceptions.AuthenticationFailureError: The | ||
| secret key is not correct. | ||
| ~vws.exceptions.vws_exceptions.FailError: There was an error with | ||
| the request. For example, the given access key does not match a | ||
| known database. | ||
| ~vws.exceptions.vws_exceptions.InvalidAcceptHeaderError: The | ||
| Accept header value is not supported. | ||
| ~vws.exceptions.vws_exceptions.InvalidInstanceIdError: The | ||
| instance ID is invalid. For example, it may be empty. | ||
| ~vws.exceptions.vws_exceptions.InvalidTargetTypeError: The target | ||
| is not a VuMark template target. | ||
| ~vws.exceptions.vws_exceptions.RequestTimeTooSkewedError: There is | ||
| an error with the time sent to Vuforia. | ||
| ~vws.exceptions.vws_exceptions.TargetStatusNotSuccessError: The | ||
| target is not in the success state. | ||
| ~vws.exceptions.vws_exceptions.UnknownTargetError: The given target | ||
| ID does not match a target in the database. | ||
| ~vws.exceptions.custom_exceptions.ServerError: There is an error | ||
| with Vuforia's servers. | ||
| ~vws.exceptions.vws_exceptions.TooManyRequestsError: Vuforia is | ||
| rate limiting access. | ||
| """ | ||
| request_path = f"/targets/{target_id}/instances" | ||
| content_type = "application/json" | ||
| request_data = json.dumps(obj={"instance_id": instance_id}).encode( | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| response = target_api_request( | ||
| content_type=content_type, | ||
| server_access_key=self._server_access_key, | ||
| server_secret_key=self._server_secret_key, | ||
| method=HTTPMethod.POST, | ||
| data=request_data, | ||
| request_path=request_path, | ||
| base_vws_url=self._base_vws_url, | ||
| request_timeout_seconds=self._request_timeout_seconds, | ||
| extra_headers={"Accept": accept}, | ||
| ) | ||
|
|
||
| if ( | ||
| response.status_code == HTTPStatus.TOO_MANY_REQUESTS | ||
| ): # pragma: no cover | ||
| # The Vuforia API returns a 429 response with no JSON body. | ||
| raise TooManyRequestsError(response=response) | ||
|
|
||
| if ( | ||
| response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR | ||
| ): # pragma: no cover | ||
| raise ServerError(response=response) | ||
|
|
||
| if response.status_code == HTTPStatus.OK: | ||
| return response.content | ||
|
|
||
| result_code = json.loads(s=response.text)["result_code"] | ||
|
|
||
| exception = { | ||
| "AuthenticationFailure": AuthenticationFailureError, | ||
| "BadRequest": BadRequestError, | ||
| "DateRangeError": DateRangeError, | ||
| "Fail": FailError, | ||
| "InvalidAcceptHeader": InvalidAcceptHeaderError, | ||
| "InvalidInstanceId": InvalidInstanceIdError, | ||
| "InvalidTargetType": InvalidTargetTypeError, | ||
| "RequestTimeTooSkewed": RequestTimeTooSkewedError, | ||
| "TargetStatusNotSuccess": TargetStatusNotSuccessError, | ||
| "UnknownTarget": UnknownTargetError, | ||
| }[result_code] | ||
|
|
||
| raise exception(response=response) | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Brittle target ID parsing from URLs
Medium Severity
The
target_idproperties now returnpath.split(\"/\")[2], which can raiseIndexErroror return the wrong segment if the URL path has a prefix (e.g., a base URL with a path like/v1) or an unexpected shape. This can break error reporting forUnknownTargetErrorand related exceptions.Additional Locations (2)
src/vws/exceptions/vws_exceptions.py#L67-L75src/vws/exceptions/vws_exceptions.py#L157-L165