|
| 1 | +""" |
| 2 | +Tools for interacting with Vuforia APIs. |
| 3 | +""" |
| 4 | + |
| 5 | +import base64 |
| 6 | +import io |
| 7 | +import json |
| 8 | +from typing import Union |
| 9 | +from urllib.parse import urljoin |
| 10 | + |
| 11 | +import requests |
| 12 | +from requests import Response |
| 13 | + |
| 14 | +from vws._authorization import authorization_header, rfc_1123_date |
| 15 | + |
| 16 | + |
| 17 | +def _target_api_request( |
| 18 | + server_access_key: bytes, |
| 19 | + server_secret_key: bytes, |
| 20 | + method: str, |
| 21 | + content: bytes, |
| 22 | + request_path: str, |
| 23 | + base_vws_url: str, |
| 24 | +) -> Response: |
| 25 | + """ |
| 26 | + Make a request to the Vuforia Target API. |
| 27 | +
|
| 28 | + This uses `requests` to make a request against https://vws.vuforia.com. |
| 29 | + The content type of the request will be `application/json`. |
| 30 | +
|
| 31 | + Args: |
| 32 | + server_access_key: A VWS server access key. |
| 33 | + server_secret_key: A VWS server secret key. |
| 34 | + method: The HTTP method which will be used in the request. |
| 35 | + content: The request body which will be used in the request. |
| 36 | + request_path: The path to the endpoint which will be used in the |
| 37 | + request. |
| 38 | +
|
| 39 | + base_vws_url: The base URL for the VWS API. |
| 40 | +
|
| 41 | + Returns: |
| 42 | + The response to the request made by `requests`. |
| 43 | + """ |
| 44 | + date = rfc_1123_date() |
| 45 | + content_type = 'application/json' |
| 46 | + |
| 47 | + signature_string = authorization_header( |
| 48 | + access_key=server_access_key, |
| 49 | + secret_key=server_secret_key, |
| 50 | + method=method, |
| 51 | + content=content, |
| 52 | + content_type=content_type, |
| 53 | + date=date, |
| 54 | + request_path=request_path, |
| 55 | + ) |
| 56 | + |
| 57 | + headers = { |
| 58 | + 'Authorization': signature_string, |
| 59 | + 'Date': date, |
| 60 | + 'Content-Type': content_type, |
| 61 | + } |
| 62 | + |
| 63 | + url = urljoin(base=base_vws_url, url=request_path) |
| 64 | + |
| 65 | + response = requests.request( |
| 66 | + method=method, |
| 67 | + url=url, |
| 68 | + headers=headers, |
| 69 | + data=content, |
| 70 | + ) |
| 71 | + |
| 72 | + return response |
| 73 | + |
| 74 | + |
| 75 | +class VWS: |
| 76 | + """ |
| 77 | + An interface to Vuforia Web Services APIs. |
| 78 | + """ |
| 79 | + |
| 80 | + def __init__( |
| 81 | + self, |
| 82 | + server_access_key: str, |
| 83 | + server_secret_key: str, |
| 84 | + base_vws_url: str = 'https://vws.vuforia.com', |
| 85 | + ) -> None: |
| 86 | + """ |
| 87 | + Args: |
| 88 | + server_access_key: A VWS server access key. |
| 89 | + server_secret_key: A VWS server secret key. |
| 90 | + base_vws_url: The base URL for the VWS API. |
| 91 | + """ |
| 92 | + self._server_access_key = server_access_key.encode() |
| 93 | + self._server_secret_key = server_secret_key.encode() |
| 94 | + self._base_vws_url = base_vws_url |
| 95 | + |
| 96 | + def add_target( |
| 97 | + self, |
| 98 | + name: str, |
| 99 | + width: Union[int, float], |
| 100 | + image: io.BytesIO, |
| 101 | + ) -> str: |
| 102 | + """ |
| 103 | + Add a target to a Vuforia Web Services database. |
| 104 | +
|
| 105 | + Args: |
| 106 | + name: The name of the target. |
| 107 | + width: The width of the target. |
| 108 | + image: The image of the target. |
| 109 | +
|
| 110 | + Returns: |
| 111 | + The target ID of the new target. |
| 112 | + """ |
| 113 | + image_data = image.getvalue() |
| 114 | + image_data_encoded = base64.b64encode(image_data).decode('ascii') |
| 115 | + metadata_encoded = None |
| 116 | + |
| 117 | + data = { |
| 118 | + 'name': name, |
| 119 | + 'width': width, |
| 120 | + 'image': image_data_encoded, |
| 121 | + 'application_metadata': metadata_encoded, |
| 122 | + } |
| 123 | + |
| 124 | + content = bytes(json.dumps(data), encoding='utf-8') |
| 125 | + |
| 126 | + response = _target_api_request( |
| 127 | + server_access_key=self._server_access_key, |
| 128 | + server_secret_key=self._server_secret_key, |
| 129 | + method='POST', |
| 130 | + content=content, |
| 131 | + request_path='/targets', |
| 132 | + base_vws_url=self._base_vws_url, |
| 133 | + ) |
| 134 | + |
| 135 | + return str(response.json()['target_id']) |
0 commit comments