From 94aea524a23ac428259bae327a1fccdd2f5b841d Mon Sep 17 00:00:00 2001 From: Gauvain Pocentek Date: Sun, 17 Jul 2016 14:09:39 +0200 Subject: Allow to stream the downloads when appropriate Some API calls will download possibly large data, resulting in a high memory usage and out-of-memory errors. For these API calls use the requests streaming capabilities and download chunked data. The caller is responsible of providing a callable to actually store the data. The default callable just prints the data on stdout. --- gitlab/utils.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 gitlab/utils.py (limited to 'gitlab/utils.py') diff --git a/gitlab/utils.py b/gitlab/utils.py new file mode 100644 index 0000000..181ca20 --- /dev/null +++ b/gitlab/utils.py @@ -0,0 +1,15 @@ +class _StdoutStream(object): + def __call__(self, chunk): + print(chunk) + + +def response_content(response, streamed, action, chunk_size): + if streamed is False: + return response.content + + if action is None: + action = _StdoutStream() + + for chunk in response.iter_content(chunk_size=chunk_size): + if chunk: + action(chunk) -- cgit v1.2.1