diff options
author | Gauvain Pocentek <gauvain@pocentek.net> | 2016-07-17 14:09:39 +0200 |
---|---|---|
committer | Gauvain Pocentek <gauvain@pocentek.net> | 2016-07-17 14:09:39 +0200 |
commit | 94aea524a23ac428259bae327a1fccdd2f5b841d (patch) | |
tree | f8c3c8a90f4baf8ef271f6df3572de7991bbe22c /gitlab/utils.py | |
parent | 8e6a9442324926ed1dec0a8bfaf77792e4bdb10f (diff) | |
download | gitlab-94aea524a23ac428259bae327a1fccdd2f5b841d.tar.gz |
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.
Diffstat (limited to 'gitlab/utils.py')
-rw-r--r-- | gitlab/utils.py | 15 |
1 files changed, 15 insertions, 0 deletions
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) |