diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f81935..a32f28d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## 1.2.0 (2026-09-10) + +- Added `GetCertificate` and `UploadCertificate` for the webstack certificate API. `GetCertificate` + returns the stored PEM chain and raises `APIServerError` when nothing is stored under that id; the + private key is write only and never comes back. `UploadCertificate` creates or replaces a + certificate from a PEM bundle of the chain and, optionally, its matching private key. +- `APICall` accepts `parseJsonResponse`, which returns the response body as `bytes` instead of + decoding it as JSON. Endpoints that serve another content type on success, such as the certificate + download, can now go through `APICall` and get its error handling; error responses are still + decoded as JSON either way. + ## 1.1.2 (2026-09-01) - `LazyGraphQuery` no longer requests `meta.totalCount` on every graph list query. The count is diff --git a/python/mujinwebstackclient/controllerwebclientraw.py b/python/mujinwebstackclient/controllerwebclientraw.py index f3f64ab..6b97669 100644 --- a/python/mujinwebstackclient/controllerwebclientraw.py +++ b/python/mujinwebstackclient/controllerwebclientraw.py @@ -419,6 +419,7 @@ def APICall( files: Optional[Dict[str, Any]] = None, timeout: float = 5, apiVersion: str = 'v1', + parseJsonResponse: bool = True, ) -> Any: path = '/api/%s/%s' % (apiVersion, path.lstrip('/')) if apiVersion == 'v1' and not path.endswith('/'): @@ -470,10 +471,11 @@ def APICall( response = self.Request(method, path, params=params, data=data, files=files, headers=headers, timeout=timeout) - # Try to parse response + # Errors always come back as JSON, even from endpoints that serve another content type on + # success, so decode the body whenever it could be carrying one raw = response.content.decode('utf-8', 'replace').strip() content: Optional[Dict[str, Any]] = None - if len(raw) > 0: + if len(raw) > 0 and (parseJsonResponse or response.status_code >= 400): content = self.DecodeJSON(raw) # First check error @@ -502,6 +504,10 @@ def APICall( log.error('response status code is %d, expecting %d for %s %s: %s', response.status_code, expectedStatusCode, method, path, raw) raise APIServerError(_('Unexpected server response %d: %s') % (response.status_code, raw)) + # Hand back the body untouched for endpoints whose successful response is not JSON + if not parseJsonResponse: + return response.content + return content def CallGraphAPI( diff --git a/python/mujinwebstackclient/version.py b/python/mujinwebstackclient/version.py index 8e31c60..8ae153d 100644 --- a/python/mujinwebstackclient/version.py +++ b/python/mujinwebstackclient/version.py @@ -1,3 +1,3 @@ -__version__ = '1.1.2' +__version__ = '1.2.0' # Do not forget to update CHANGELOG.md diff --git a/python/mujinwebstackclient/webstackclient.py b/python/mujinwebstackclient/webstackclient.py index 78ce824..687341b 100644 --- a/python/mujinwebstackclient/webstackclient.py +++ b/python/mujinwebstackclient/webstackclient.py @@ -27,6 +27,9 @@ log = logging.getLogger(__name__) +# Content type the certificate API speaks in both directions +CERTIFICATE_CONTENT_TYPE = 'application/x-pem-file' + def GetFilenameFromURI(uri, mujinpath): """Returns the filesystem path that the URI points to. @@ -874,6 +877,38 @@ def DownloadBlob(self, blobId, timeout=5): raise WebstackClientError(response.content.decode('utf-8'), response=response.content) return response + # + # Certificate related + # + + def GetCertificate(self, certificateId, timeout=5): + # type: (str, float) -> bytes + """Downloads a stored TLS certificate as PEM. + + The private key is write only, so what comes back is the leaf certificate followed by any + intermediates, never the key it may have been uploaded with. + + :param certificateId: id the certificate is stored under + :param timeout: number of seconds to wait for the response + :return: the PEM encoded certificate chain + """ + return self._webclient.APICall('GET', 'certificate/%s' % certificateId, headers={'Accept': CERTIFICATE_CONTENT_TYPE}, timeout=timeout, apiVersion='v2', parseJsonResponse=False) + + def UploadCertificate(self, certificateId, pemData, timeout=5): + # type: (str, bytes, float) -> Any + """Creates or replaces a TLS certificate from a PEM bundle. + + The bundle holds the leaf certificate followed by any intermediates, and optionally the + matching private key, which is stored but never served back. A key that does not match the + leaf is rejected. Any name and description already set on the certificate are preserved. + + :param certificateId: id to store the certificate under + :param pemData: PEM bundle of the certificate chain, optionally with its private key + :param timeout: number of seconds to wait for the response + :return: the stored certificate and its derived metadata, without any key material + """ + return self._webclient.APICall('PUT', 'certificate/%s' % certificateId, data=pemData, headers={'Content-Type': CERTIFICATE_CONTENT_TYPE}, expectedStatusCode=200, timeout=timeout, apiVersion='v2') + # # Log related #