Mercurial > hg > orthanc-book
changeset 1134:9c063c145c0e
python http streaming
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Wed, 22 Jan 2025 16:30:58 +0100 (3 months ago) |
parents | 9d77d6ded4ad |
children | 63906b666462 |
files | Sphinx/source/plugins/python.rst Sphinx/source/plugins/python/extend-api-with-streaming.py |
diffstat | 2 files changed, 33 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/Sphinx/source/plugins/python.rst Wed Jan 22 13:56:11 2025 +0100 +++ b/Sphinx/source/plugins/python.rst Wed Jan 22 16:30:58 2025 +0100 @@ -334,6 +334,7 @@ $ curl http://localhost:8042/toto ok +**Note:** from v5.0, it is possible to stream :ref:`stream HTTP answers <python_stream_anwsers>` Overriding the core REST API ............................ @@ -974,6 +975,18 @@ :language: python +.. _python_stream_anwsers: + +Streaming HTTP answers (new in 5.0) +................................... + +Starting from v 5.0, it is possible to stream HTTP answers when +implementing a REST API route: + +.. literalinclude:: python/extend-api-with-streaming.py + :language: python + + Performance and concurrency ---------------------------
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sphinx/source/plugins/python/extend-api-with-streaming.py Wed Jan 22 16:30:58 2025 +0100 @@ -0,0 +1,20 @@ +import orthanc +import requests + +TOKEN = orthanc.GenerateRestApiAuthorizationToken() + +def OnRestArchive(output, uri, **request): + study_id = request['groups'][0] + print(f"Accessing archive for study: {study_id}") + + # put your business logic here + + with requests.get(f"http://localhost:8043/studies/{study_id}/archive", headers = { 'Authorization' : TOKEN }, stream = True) as r: + output.StartStreamAnswer('application/zip') + + for buffer in r.iter_content(16*1024, False): + print(len(buffer)) + output.SendStreamChunk(buffer) + + +orthanc.RegisterRestCallback('/studies/(.*)/my-archive', OnRestArchive) \ No newline at end of file