Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

refactor: Migrate MultipartEncoder to RequestsBackend#2421

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nejch merged 2 commits intopython-gitlab:mainfromlmilbaum:MultipartEncoder
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes fromall 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
37 changes: 3 additions & 34 deletionsgitlab/client.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,6 @@

import requests
import requests.utils
from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore

import gitlab
import gitlab.config
Expand DownExpand Up@@ -637,38 +636,6 @@ def _check_redirects(result: requests.Response) -> None:
)
)

@staticmethod
def _prepare_send_data(
files: Optional[Dict[str, Any]] = None,
post_data: Optional[Union[Dict[str, Any], bytes]] = None,
raw: bool = False,
) -> Tuple[
Optional[Union[Dict[str, Any], bytes]],
Optional[Union[Dict[str, Any], MultipartEncoder]],
str,
]:
if files:
if post_data is None:
post_data = {}
else:
# booleans does not exists for data (neither for MultipartEncoder):
# cast to string int to avoid: 'bool' object has no attribute 'encode'
if TYPE_CHECKING:
assert isinstance(post_data, dict)
for k, v in post_data.items():
if isinstance(v, bool):
post_data[k] = str(int(v))
post_data["file"] = files.get("file")
post_data["avatar"] = files.get("avatar")

data = MultipartEncoder(post_data)
return (None, data, data.content_type)

if raw and post_data:
return (None, post_data, "application/octet-stream")

return (post_data, None, "application/json")

def http_request(
self,
verb: str,
Expand DownExpand Up@@ -746,7 +713,9 @@ def http_request(
retry_transient_errors = self.retry_transient_errors

# We need to deal with json vs. data when uploading files
json, data, content_type = self._prepare_send_data(files, post_data, raw)
json, data, content_type = self.http_backend.prepare_send_data(
files, post_data, raw
)
opts["headers"]["Content-type"] = content_type

cur_retries = 0
Expand Down
34 changes: 33 additions & 1 deletiongitlab/http_backends/requests_backend.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Any, Dict, Optional, Union
from typing import Any, Dict, Optional,Tuple, TYPE_CHECKING,Union

import requests
from requests.structures import CaseInsensitiveDict
Expand DownExpand Up@@ -39,6 +39,38 @@ def __init__(self, session: Optional[requests.Session] = None) -> None:
def client(self) -> requests.Session:
return self._client

@staticmethod
def prepare_send_data(
files: Optional[Dict[str, Any]] = None,
post_data: Optional[Union[Dict[str, Any], bytes]] = None,
raw: bool = False,
) -> Tuple[
Optional[Union[Dict[str, Any], bytes]],
Optional[Union[Dict[str, Any], MultipartEncoder]],
str,
]:
if files:
if post_data is None:
post_data = {}
else:
# booleans does not exists for data (neither for MultipartEncoder):
# cast to string int to avoid: 'bool' object has no attribute 'encode'
if TYPE_CHECKING:
assert isinstance(post_data, dict)
for k, v in post_data.items():
if isinstance(v, bool):
post_data[k] = str(int(v))
post_data["file"] = files.get("file")
post_data["avatar"] = files.get("avatar")

data = MultipartEncoder(post_data)
return (None, data, data.content_type)

if raw and post_data:
return (None, post_data, "application/octet-stream")

return (post_data, None, "application/json")

def http_request(
self,
method: str,
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp