Skip to content

Commit 544a2d8

Browse files
committed
feat: support multipart uploads
1 parent 265dbbd commit 544a2d8

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

gitlab/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
from gitlab.const import * # noqa
2828
from gitlab.exceptions import * # noqa
2929
from gitlab import utils # noqa
30+
from requests_toolbelt.multipart.encoder import MultipartEncoder
31+
3032

3133
__title__ = "python-gitlab"
32-
__version__ = "2.5.0"
34+
__version__ = "2.6.0"
3335
__author__ = "Gauvain Pocentek"
3436
__email__ = "gauvainpocentek@gmail.com"
3537
__license__ = "LGPL3"
@@ -496,9 +498,10 @@ def http_request(
496498

497499
# We need to deal with json vs. data when uploading files
498500
if files:
499-
data = post_data
500501
json = None
501-
del opts["headers"]["Content-type"]
502+
post_data["file"] = files.get("file") or files.get("avatar")
503+
data = MultipartEncoder(post_data)
504+
opts["headers"]["Content-type"] = data.content_type
502505
else:
503506
json = post_data
504507
data = None
@@ -509,9 +512,7 @@ def http_request(
509512
# The Requests behavior is right but it seems that web servers don't
510513
# always agree with this decision (this is the case with a default
511514
# gitlab installation)
512-
req = requests.Request(
513-
verb, url, json=json, data=data, params=params, files=files, **opts
514-
)
515+
req = requests.Request(verb, url, json=json, data=data, params=params, **opts)
515516
prepped = self.session.prepare_request(req)
516517
prepped.url = utils.sanitized_url(prepped.url)
517518
settings = self.session.merge_environment_settings(

gitlab/v4/objects/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,7 @@ def import_group(self, file, path, name, parent_id=None, **kwargs):
16151615
Returns:
16161616
dict: A representation of the import status.
16171617
"""
1618-
files = {"file": ("file.tar.gz", file)}
1618+
files = {"file": ("file.tar.gz", file, "application/octet-stream")}
16191619
data = {"path": path, "name": name}
16201620
if parent_id is not None:
16211621
data["parent_id"] = parent_id
@@ -5488,8 +5488,8 @@ def import_project(
54885488
Returns:
54895489
dict: A representation of the import status.
54905490
"""
5491-
files = {"file": ("file.tar.gz", file)}
5492-
data = {"path": path, "overwrite": overwrite}
5491+
files = {"file": ("file.tar.gz", file, "application/octet-stream")}
5492+
data = {"path": path, "overwrite": str(overwrite)}
54935493
if override_params:
54945494
for k, v in override_params.items():
54955495
data["override_params[%s]" % k] = v

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
requests==2.24.0
2+
requests-toolbelt>=0.9.1

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_version():
2626
license="LGPLv3",
2727
url="https://github.com/python-gitlab/python-gitlab",
2828
packages=find_packages(),
29-
install_requires=["requests>=2.22.0"],
29+
install_requires=["requests>=2.22.0", "requests-toolbelt>=0.9.1"],
3030
python_requires=">=3.6.0",
3131
entry_points={"console_scripts": ["gitlab = gitlab.cli:main"]},
3232
classifiers=[

0 commit comments

Comments
 (0)