Skip to content

Commit

Permalink
Remove redundant streaming in download of file (#77)
Browse files Browse the repository at this point in the history
In PR #71 a streaming mechanism has been implemented for the endpoint
`/nodes/{nodes_id}/download`, this however is already taken care by
FastAPI StreamingResponse and is therefore redundant and can be removed.
  • Loading branch information
agoscinski authored Nov 26, 2024
1 parent 2fd5693 commit 87cf264
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 12 deletions.
3 changes: 0 additions & 3 deletions aiida_restapi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,3 @@
'disabled': False,
}
}

# The chunks size for streaming data for download
DOWNLOAD_CHUNK_SIZE = 1024
4 changes: 1 addition & 3 deletions aiida_restapi/routers/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from pydantic import ValidationError

from aiida_restapi import models, resources
from aiida_restapi.config import DOWNLOAD_CHUNK_SIZE

from .auth import get_current_active_user

Expand Down Expand Up @@ -73,8 +72,7 @@ async def download_node(nodes_id: int, download_format: Optional[str] = None) ->

def stream() -> Generator[bytes, None, None]:
with io.BytesIO(exported_bytes) as handler:
while chunk := handler.read(DOWNLOAD_CHUNK_SIZE):
yield chunk
yield from handler

return StreamingResponse(stream(), media_type=f'application/{download_format}')

Expand Down
8 changes: 2 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,9 @@ def default_nodes():

@pytest.fixture(scope='function')
def array_data_node():
"""Populate database with downloadable node (implmenting a _prepare_* function).
For testing the chunking of the streaming we create an array that needs to be splitted int two chunks."""
"""Populate database with downloadable node (implmenting a _prepare_* function)."""

from aiida_restapi.config import DOWNLOAD_CHUNK_SIZE

nb_elements = DOWNLOAD_CHUNK_SIZE // 64 + 1
return orm.ArrayData(np.arange(nb_elements, dtype=np.int64)).store()
return orm.ArrayData(np.arange(4)).store()


@pytest.fixture(scope='function')
Expand Down

0 comments on commit 87cf264

Please sign in to comment.