Skip to content
Snippets Groups Projects
Verified Commit 4ffeda23 authored by Ubbo Veentjer's avatar Ubbo Veentjer
Browse files

feat: add uri param to tgcrud create api method

parent 9bcc36fe
No related branches found
No related tags found
1 merge request!78Resolve "add uri param to tgcrud create API"
Pipeline #494345 passed
......@@ -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
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment