diff --git a/src/tgclients/crud.py b/src/tgclients/crud.py index 4e94d73b0918cbfee95e5cddba32f984662012cb..6d9fe91b65333db23ce7a2af4edfd041c63e685f 100644 --- a/src/tgclients/crud.py +++ b/src/tgclients/crud.py @@ -91,7 +91,12 @@ class TextgridCrudRequest: return self._handle_response(response) def create_resource( - self, sid: str, project_id: str, data: Union[str, IO[Any]], metadata: Union[str, IO[Any]] + self, + sid: str, + project_id: str, + data: Union[str, IO[Any]], + metadata: Union[str, IO[Any]], + uri: Optional[str] = None, ) -> Response: """Create a TextGrid object. @@ -100,6 +105,7 @@ class TextgridCrudRequest: project_id (str): Project ID data (Union[str, IO[Any]]): the data metadata (Union[str, IO[Any]]): the metadata + uri (Optional[str]): optionally set a TextGrid URI to use for new object (see get_uri method) Raises: TextgridCrudException: if HTTP status code >= 400 @@ -108,7 +114,7 @@ class TextgridCrudRequest: Response: HTTP response from service with metadata from newly created object """ encoder = self._prepare_multipart(metadata, data) - params = {'sessionId': sid, 'projectId': project_id, 'createRevision': 'false'} + params = {'sessionId': sid, 'projectId': project_id, 'createRevision': 'false', 'uri': uri} response = self._requests.post( self._url + '/' + 'create', params=params, @@ -360,7 +366,12 @@ class TextgridCrud(TextgridCrudRequest): super().__init__(config, for_publication) def create_resource( - self, sid: str, project_id: str, data: Union[str, IO[Any]], metadata: MetadataContainerType + self, + sid: str, + project_id: str, + data: Union[str, IO[Any]], + metadata: MetadataContainerType, + uri: Optional[str] = None, ) -> MetadataContainerType: """Create a TextGrid object. @@ -369,6 +380,7 @@ class TextgridCrud(TextgridCrudRequest): project_id (str): Project ID data (Union[str, IO[Any]]): the data metadata (MetadataContainerType): the metadata + uri (Optional[str]): optionally set a TextGrid URI to use for new object (see get_uri method) Raises: TextgridCrudException: if HTTP status code >= 400 @@ -377,7 +389,7 @@ class TextgridCrud(TextgridCrudRequest): MetadataContainerType: metadata for newly created object """ metadata_string = self._serializer.render(metadata) - response = super().create_resource(sid, project_id, data, metadata_string) + response = super().create_resource(sid, project_id, data, metadata_string, uri=uri) return self._parser.parse(BytesIO(response.content), MetadataContainerType) def create_revision( # noqa: PLR0913 diff --git a/tests/integration/test_crud_integration.py b/tests/integration/test_crud_integration.py index da429edfd67f0afd8c1b91ba821e20bd89da630f..0c412c846af8cd78073e20bd733c2005b67c72ad 100644 --- a/tests/integration/test_crud_integration.py +++ b/tests/integration/test_crud_integration.py @@ -209,3 +209,21 @@ class TestTextgridCrudIntegration: assert len(uris) == 2 for uri in uris: assert uri.startswith('textgrid:') + + @staticmethod + def test_get_uri_and_create(crud): + sid = os.getenv('SESSION_ID') + project_id = os.getenv('PROJECT_ID') + uris = crud.get_uri(sid, 1) + assert len(uris) == 1 + assert uris[0].startswith('textgrid:') + + now = datetime.now().strftime('%Y-%m-%d-%H:%M:%S') + metadata = TextgridMetadata().build(title='test ' + now, mimetype='text/xml') + data = '<content>here</content>' + + res = crud.create_resource(sid, project_id, data, metadata, uri=uris[0]) + textgrid_uri = res.object_value.generic.generated.textgrid_uri.value + assert textgrid_uri == f'{uris[0]}.0' + res = crud.delete_resource(sid, textgrid_uri) + assert res.status_code == 204