Skip to content

fix(api): Don't try to parse raw downloads #687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions gitlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,16 @@ def http_request(self, verb, path, query_data={}, post_data=None,
error_message=error_message,
response_body=result.content)

def http_get(self, path, query_data={}, streamed=False, **kwargs):
def http_get(self, path, query_data={}, streamed=False, raw=False,
**kwargs):
"""Make a GET request to the Gitlab server.

Args:
path (str): Path or full URL to query ('/projects' or
'http://whatever/v4/api/projecs')
query_data (dict): Data to send as query parameters
streamed (bool): Whether the data should be streamed
raw (bool): If True do not try to parse the output as json
**kwargs: Extra options to send to the server (e.g. sudo)

Returns:
Expand All @@ -538,8 +540,10 @@ def http_get(self, path, query_data={}, streamed=False, **kwargs):
"""
result = self.http_request('get', path, query_data=query_data,
streamed=streamed, **kwargs)
if (result.headers['Content-Type'] == 'application/json' and
not streamed):

if (result.headers['Content-Type'] == 'application/json'
and not streamed
and not raw):
try:
return result.json()
except Exception:
Expand Down
21 changes: 11 additions & 10 deletions gitlab/v4/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
"""
path = '/snippets/%s/raw' % self.get_id()
result = self.manager.gitlab.http_get(path, streamed=streamed,
**kwargs)
raw=True, **kwargs)
return utils.response_content(result, streamed, action, chunk_size)


Expand Down Expand Up @@ -1365,7 +1365,7 @@ def artifacts(self, streamed=False, action=None, chunk_size=1024,
"""
path = '%s/%s/artifacts' % (self.manager.path, self.get_id())
result = self.manager.gitlab.http_get(path, streamed=streamed,
**kwargs)
raw=True, **kwargs)
return utils.response_content(result, streamed, action, chunk_size)

@cli.register_custom_action('ProjectJob')
Expand Down Expand Up @@ -1393,7 +1393,7 @@ def artifact(self, path, streamed=False, action=None, chunk_size=1024,
"""
path = '%s/%s/artifacts/%s' % (self.manager.path, self.get_id(), path)
result = self.manager.gitlab.http_get(path, streamed=streamed,
**kwargs)
raw=True, **kwargs)
return utils.response_content(result, streamed, action, chunk_size)

@cli.register_custom_action('ProjectJob')
Expand All @@ -1419,7 +1419,7 @@ def trace(self, streamed=False, action=None, chunk_size=1024, **kwargs):
"""
path = '%s/%s/trace' % (self.manager.path, self.get_id())
result = self.manager.gitlab.http_get(path, streamed=streamed,
**kwargs)
raw=True, **kwargs)
return utils.response_content(result, streamed, action, chunk_size)


Expand Down Expand Up @@ -2654,7 +2654,7 @@ def raw(self, file_path, ref, streamed=False, action=None, chunk_size=1024,
path = '%s/%s/raw' % (self.path, file_path)
query_data = {'ref': ref}
result = self.gitlab.http_get(path, query_data=query_data,
streamed=streamed, **kwargs)
streamed=streamed, raw=True, **kwargs)
return utils.response_content(result, streamed, action, chunk_size)


Expand Down Expand Up @@ -2897,7 +2897,7 @@ def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
"""
path = "%s/%s/raw" % (self.manager.path, self.get_id())
result = self.manager.gitlab.http_get(path, streamed=streamed,
**kwargs)
raw=True, **kwargs)
return utils.response_content(result, streamed, action, chunk_size)


Expand Down Expand Up @@ -3174,7 +3174,7 @@ def download(self, streamed=False, action=None, chunk_size=1024, **kwargs):
"""
path = '/projects/%d/export/download' % self.project_id
result = self.manager.gitlab.http_get(path, streamed=streamed,
**kwargs)
raw=True, **kwargs)
return utils.response_content(result, streamed, action, chunk_size)


Expand Down Expand Up @@ -3315,7 +3315,7 @@ def repository_raw_blob(self, sha, streamed=False, action=None,
"""
path = '/projects/%s/repository/blobs/%s/raw' % (self.get_id(), sha)
result = self.manager.gitlab.http_get(path, streamed=streamed,
**kwargs)
raw=True, **kwargs)
return utils.response_content(result, streamed, action, chunk_size)

@cli.register_custom_action('Project', ('from_', 'to'))
Expand Down Expand Up @@ -3391,7 +3391,8 @@ def repository_archive(self, sha=None, streamed=False, action=None,
if sha:
query_data['sha'] = sha
result = self.manager.gitlab.http_get(path, query_data=query_data,
streamed=streamed, **kwargs)
raw=True, streamed=streamed,
**kwargs)
return utils.response_content(result, streamed, action, chunk_size)

@cli.register_custom_action('Project', ('forked_from_id', ))
Expand Down Expand Up @@ -3674,7 +3675,7 @@ def snapshot(self, wiki=False, streamed=False, action=None,
"""
path = '/projects/%d/snapshot' % self.get_id()
result = self.manager.gitlab.http_get(path, streamed=streamed,
**kwargs)
raw=True, **kwargs)
return utils.response_content(result, streamed, action, chunk_size)

@cli.register_custom_action('Project', ('scope', 'search'))
Expand Down