# HG changeset patch # User Alain Mazy # Date 1737559858 -3600 # Node ID 9c063c145c0ea2f6d75a3b2c2d25d1b36f16baa0 # Parent 9d77d6ded4adecc617abb42ae5bce072c8c57c3b python http streaming diff -r 9d77d6ded4ad -r 9c063c145c0e Sphinx/source/plugins/python.rst --- 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 ` 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 --------------------------- diff -r 9d77d6ded4ad -r 9c063c145c0e Sphinx/source/plugins/python/extend-api-with-streaming.py --- /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