-
Notifications
You must be signed in to change notification settings - Fork 2
Add VuMark Generation API endpoint (#473) #2878
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
Changes from 8 commits
3efd7a7
139282e
f193a40
dcb8ec3
a24762c
e2db9b5
0edf26a
3c6ec7b
99fef3e
37095d4
7704c8b
4ee8d50
b536a1d
fc1e301
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,6 +100,7 @@ resjsonarr | |
| rfc | ||
| rgb | ||
| str | ||
| svg | ||
| timestamp | ||
| todo | ||
| travis | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,17 @@ | |
| TargetStatusProcessingError, | ||
| ValidatorError, | ||
| ) | ||
| from mock_vws._vumark_generators import ( | ||
| generate_pdf, | ||
| generate_png, | ||
| generate_svg, | ||
| ) | ||
| from mock_vws._vumark_validators import ( | ||
| validate_accept_header, | ||
| validate_instance_id, | ||
| validate_target_status_success, | ||
| validate_target_type, | ||
| ) | ||
| from mock_vws.image_matchers import ImageMatcher | ||
| from mock_vws.target import Target | ||
| from mock_vws.target_manager import TargetManager | ||
|
|
@@ -38,7 +49,7 @@ | |
|
|
||
| _ROUTES: set[Route] = set() | ||
|
|
||
| _ResponseType = tuple[int, Mapping[str, str], str] | ||
| _ResponseType = tuple[int, Mapping[str, str], str | 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.
|
||
| _P = ParamSpec("_P") | ||
|
|
||
|
|
||
|
|
@@ -187,6 +198,7 @@ def add_target(self, request: PreparedRequest) -> _ResponseType: | |
| processing_time_seconds=self._processing_time_seconds, | ||
| application_metadata=application_metadata, | ||
| target_tracking_rater=self._target_tracking_rater, | ||
| target_type=database.default_target_type, | ||
| ) | ||
| database.targets.add(new_target) | ||
|
|
||
|
|
@@ -702,7 +714,7 @@ def target_summary(self, request: PreparedRequest) -> _ResponseType: | |
| "previous_month_recos": target.previous_month_recos, | ||
| } | ||
| body_json = json_dump(body=body) | ||
| headers = { | ||
| target_summary_headers = { | ||
| "Connection": "keep-alive", | ||
| "Content-Length": str(object=len(body_json)), | ||
| "Content-Type": "application/json", | ||
|
|
@@ -714,4 +726,97 @@ def target_summary(self, request: PreparedRequest) -> _ResponseType: | |
| "x-content-type-options": "nosniff", | ||
| } | ||
|
|
||
| return HTTPStatus.OK, headers, body_json | ||
| return HTTPStatus.OK, target_summary_headers, body_json | ||
|
|
||
| @route( | ||
| path_pattern=f"/targets/{_TARGET_ID_PATTERN}/instances", | ||
| http_methods={HTTPMethod.POST}, | ||
| ) | ||
| def generate_vumark_instance( | ||
| self, | ||
| request: PreparedRequest, | ||
| ) -> _ResponseType: | ||
| """Generate a VuMark instance image. | ||
|
|
||
| Fake implementation of | ||
| https://developer.vuforia.com/library/vuforia-engine/web-api/vumark-generation-web-api/ | ||
| """ | ||
| try: | ||
| run_services_validators( | ||
| request_headers=request.headers, | ||
| request_body=_body_bytes(request=request), | ||
| request_method=request.method or "", | ||
| request_path=request.path_url, | ||
| databases=self._target_manager.databases, | ||
| ) | ||
| except ValidatorError as exc: | ||
| return exc.status_code, exc.headers, exc.response_text | ||
|
|
||
| # Validate Accept header | ||
| try: | ||
| accept_header = validate_accept_header( | ||
| request_headers=request.headers, | ||
| ) | ||
| except ValidatorError as exc: | ||
| return exc.status_code, exc.headers, exc.response_text | ||
|
|
||
| # Extract and validate instance_id from request body | ||
| request_json: dict[str, Any] = json.loads(s=request.body or b"{}") | ||
| try: | ||
| instance_id = validate_instance_id( | ||
| instance_id=request_json.get("instance_id"), | ||
| ) | ||
| except ValidatorError as exc: | ||
| return exc.status_code, exc.headers, exc.response_text | ||
|
|
||
| # Look up the target and validate type and status | ||
| database = get_database_matching_server_keys( | ||
| request_headers=request.headers, | ||
| request_body=_body_bytes(request=request), | ||
| request_method=request.method or "", | ||
| request_path=request.path_url, | ||
| databases=self._target_manager.databases, | ||
| ) | ||
|
|
||
| split_path = request.path_url.split(sep="/") | ||
| target_id = split_path[-2] | ||
| target = database.get_target(target_id=target_id) | ||
|
|
||
| try: | ||
| validate_target_type(target=target) | ||
| except ValidatorError as exc: | ||
| return exc.status_code, exc.headers, exc.response_text | ||
|
|
||
| try: | ||
| validate_target_status_success(target=target) | ||
| except ValidatorError as exc: | ||
| return exc.status_code, exc.headers, exc.response_text | ||
|
|
||
| # Generate the appropriate image format | ||
| if accept_header == "image/svg+xml": | ||
| content = generate_svg(instance_id=instance_id) | ||
| content_type = "image/svg+xml" | ||
| elif accept_header == "image/png": | ||
| content = generate_png(instance_id=instance_id) | ||
| content_type = "image/png" | ||
| else: # PDF | ||
| content = generate_pdf(instance_id=instance_id) | ||
| content_type = "application/pdf" | ||
|
|
||
| date = email.utils.formatdate( | ||
| timeval=None, | ||
| localtime=False, | ||
| usegmt=True, | ||
| ) | ||
| headers = { | ||
| "Connection": "keep-alive", | ||
| "Content-Length": str(object=len(content)), | ||
| "Content-Type": content_type, | ||
| "Date": date, | ||
| "server": "envoy", | ||
| "x-envoy-upstream-service-time": "5", | ||
| "strict-transport-security": "max-age=31536000", | ||
| "x-aws-region": "us-east-2, us-west-2", | ||
| "x-content-type-options": "nosniff", | ||
| } | ||
| return HTTPStatus.OK, headers, content | ||


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.
Not sure why we need this