Skip to content

Commit 4f8d901

Browse files
authored
Merge pull request #1252 from python-gitlab/feat/multipart-uploads
feat: support multipart uploads
2 parents fd179d4 + 2fa3004 commit 4f8d901

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

gitlab/__init__.py

+8-6
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,11 @@ 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")
503+
post_data["avatar"] = files.get("avatar")
504+
data = MultipartEncoder(post_data)
505+
opts["headers"]["Content-type"] = data.content_type
502506
else:
503507
json = post_data
504508
data = None
@@ -509,9 +513,7 @@ def http_request(
509513
# The Requests behavior is right but it seems that web servers don't
510514
# always agree with this decision (this is the case with a default
511515
# gitlab installation)
512-
req = requests.Request(
513-
verb, url, json=json, data=data, params=params, files=files, **opts
514-
)
516+
req = requests.Request(verb, url, json=json, data=data, params=params, **opts)
515517
prepped = self.session.prepare_request(req)
516518
prepped.url = utils.sanitized_url(prepped.url)
517519
settings = self.session.merge_environment_settings(

gitlab/v4/objects/__init__.py

+3-3
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

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
requests==2.24.0
2+
requests-toolbelt>=0.9.1

setup.py

+1-1
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)