| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a788cff commit 61e0fae
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | from __future__ import annotations | |
| 2 | 2 | ||
| 3 | 3 | import dataclasses | |
| 4 | - from typing import Any, Dict, Optional, TYPE_CHECKING, Union | ||
| 4 | + from typing import Any, BinaryIO, Dict, Optional, TYPE_CHECKING, Union | ||
| 5 | 5 | ||
| 6 | 6 | import requests | |
| 7 | 7 | from requests import PreparedRequest | |
@@ -94,7 +94,7 @@ def client(self) -> requests.Session: | |||
| 94 | 94 | @staticmethod | |
| 95 | 95 | def prepare_send_data( | |
| 96 | 96 | files: Optional[Dict[str, Any]] = None, | |
| 97 | - post_data: Optional[Union[Dict[str, Any], bytes]] = None, | ||
| 97 | + post_data: Optional[Union[Dict[str, Any], bytes, BinaryIO]] = None, | ||
| 98 | 98 | raw: bool = False, | |
| 99 | 99 | ) -> SendData: | |
| 100 | 100 | if files: | |
@@ -121,6 +121,9 @@ def prepare_send_data( | |||
| 121 | 121 | if raw and post_data: | |
| 122 | 122 | return SendData(data=post_data, content_type="application/octet-stream") | |
| 123 | 123 | ||
| 124 | + if TYPE_CHECKING: | ||
| 125 | + assert not isinstance(post_data, BinaryIO) | ||
| 126 | + | ||
| 124 | 127 | return SendData(json=post_data, content_type="application/json") | |
| 125 | 128 | ||
| 126 | 129 | def http_request( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,18 @@ | |||
| 3 | 3 | import os | |
| 4 | 4 | import re | |
| 5 | 5 | import time | |
| 6 | - from typing import Any, cast, Dict, List, Optional, Tuple, Type, TYPE_CHECKING, Union | ||
| 6 | + from typing import ( | ||
| 7 | + Any, | ||
| 8 | + BinaryIO, | ||
| 9 | + cast, | ||
| 10 | + Dict, | ||
| 11 | + List, | ||
| 12 | + Optional, | ||
| 13 | + Tuple, | ||
| 14 | + Type, | ||
| 15 | + TYPE_CHECKING, | ||
| 16 | + Union, | ||
| 17 | + ) | ||
| 7 | 18 | from urllib import parse | |
| 8 | 19 | ||
| 9 | 20 | import requests | |
@@ -612,7 +623,7 @@ def http_request( | |||
| 612 | 623 | verb: str, | |
| 613 | 624 | path: str, | |
| 614 | 625 | query_data: Optional[Dict[str, Any]] = None, | |
| 615 | - post_data: Optional[Union[Dict[str, Any], bytes]] = None, | ||
| 626 | + post_data: Optional[Union[Dict[str, Any], bytes, BinaryIO]] = None, | ||
| 616 | 627 | raw: bool = False, | |
| 617 | 628 | streamed: bool = False, | |
| 618 | 629 | files: Optional[Dict[str, Any]] = None, | |
@@ -993,7 +1004,7 @@ def http_put( | |||
| 993 | 1004 | self, | |
| 994 | 1005 | path: str, | |
| 995 | 1006 | query_data: Optional[Dict[str, Any]] = None, | |
| 996 | - post_data: Optional[Union[Dict[str, Any], bytes]] = None, | ||
| 1007 | + post_data: Optional[Union[Dict[str, Any], bytes, BinaryIO]] = None, | ||
| 997 | 1008 | raw: bool = False, | |
| 998 | 1009 | files: Optional[Dict[str, Any]] = None, | |
| 999 | 1010 | **kwargs: Any, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,7 +5,16 @@ | |||
| 5 | 5 | """ | |
| 6 | 6 | ||
| 7 | 7 | from pathlib import Path | |
| 8 | - from typing import Any, Callable, cast, Iterator, Optional, TYPE_CHECKING, Union | ||
| 8 | + from typing import ( | ||
| 9 | + Any, | ||
| 10 | + BinaryIO, | ||
| 11 | + Callable, | ||
| 12 | + cast, | ||
| 13 | + Iterator, | ||
| 14 | + Optional, | ||
| 15 | + TYPE_CHECKING, | ||
| 16 | + Union, | ||
| 17 | + ) | ||
| 9 | 18 | ||
| 10 | 19 | import requests | |
| 11 | 20 | ||
@@ -46,8 +55,9 @@ def upload( | |||
| 46 | 55 | package_name: str, | |
| 47 | 56 | package_version: str, | |
| 48 | 57 | file_name: str, | |
| 49 | - path: Union[str, Path], | ||
| 58 | + path: Optional[Union[str, Path]] = None, | ||
| 50 | 59 | select: Optional[str] = None, | |
| 60 | + data: Optional[Union[bytes, BinaryIO]] = None, | ||
| 51 | 61 | **kwargs: Any, | |
| 52 | 62 | ) -> GenericPackage: | |
| 53 | 63 | """Upload a file as a generic package. | |
@@ -64,19 +74,34 @@ def upload( | |||
| 64 | 74 | Raises: | |
| 65 | 75 | GitlabConnectionError: If the server cannot be reached | |
| 66 | 76 | GitlabUploadError: If the file upload fails | |
| 67 | - GitlabUploadError: If ``filepath`` cannot be read | ||
| 77 | + GitlabUploadError: If ``path`` cannot be read | ||
| 78 | + GitlabUploadError: If both ``path`` and ``data`` are passed | ||
| 68 | 79 | ||
| 69 | 80 | Returns: | |
| 70 | 81 | An object storing the metadata of the uploaded package. | |
| 71 | 82 | ||
| 72 | 83 | https://docs.gitlab.com/ee/user/packages/generic_packages/ | |
| 73 | 84 | """ | |
| 74 | 85 | ||
| 75 | - try: | ||
| 76 | - with open(path, "rb") as f: | ||
| 77 | - file_data = f.read() | ||
| 78 | - except OSError as e: | ||
| 79 | - raise exc.GitlabUploadError(f"Failed to read package file {path}") from e | ||
| 86 | + if path is None and data is None: | ||
| 87 | + raise exc.GitlabUploadError("No file contents or path specified") | ||
| 88 | + | ||
| 89 | + if path is not None and data is not None: | ||
| 90 | + raise exc.GitlabUploadError("File contents and file path specified") | ||
| 91 | + | ||
| 92 | + file_data: Optional[Union[bytes, BinaryIO]] = data | ||
| 93 | + | ||
| 94 | + if not file_data: | ||
| 95 | + if TYPE_CHECKING: | ||
| 96 | + assert path is not None | ||
| 97 | + | ||
| 98 | + try: | ||
| 99 | + with open(path, "rb") as f: | ||
| 100 | + file_data = f.read() | ||
| 101 | + except OSError as e: | ||
| 102 | + raise exc.GitlabUploadError( | ||
| 103 | + f"Failed to read package file {path}" | ||
| 104 | + ) from e | ||
| 80 | 105 | ||
| 81 | 106 | url = f"{self._computed_path}/{package_name}/{package_version}/{file_name}" | |
| 82 | 107 | query_data = {} if select is None else {"select": select} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -38,6 +38,38 @@ def test_upload_generic_package(tmp_path, project): | |||
| 38 | 38 | assert package.message == "201 Created" | |
| 39 | 39 | ||
| 40 | 40 | ||
| 41 | + def test_download_generic_package_bytes(tmp_path, project): | ||
| 42 | + path = tmp_path / file_name | ||
| 43 | + | ||
| 44 | + path.write_text(file_content) | ||
| 45 | + | ||
| 46 | + package = project.generic_packages.upload( | ||
| 47 | + package_name=package_name, | ||
| 48 | + package_version=package_version, | ||
| 49 | + file_name=file_name, | ||
| 50 | + data=path.read_bytes(), | ||
| 51 | + ) | ||
| 52 | + | ||
| 53 | + assert isinstance(package, GenericPackage) | ||
| 54 | + assert package.message == "201 Created" | ||
| 55 | + | ||
| 56 | + | ||
| 57 | + def test_download_generic_package_file(tmp_path, project): | ||
| 58 | + path = tmp_path / file_name | ||
| 59 | + | ||
| 60 | + path.write_text(file_content) | ||
| 61 | + | ||
| 62 | + package = project.generic_packages.upload( | ||
| 63 | + package_name=package_name, | ||
| 64 | + package_version=package_version, | ||
| 65 | + file_name=file_name, | ||
| 66 | + data=path.open(mode="rb"), | ||
| 67 | + ) | ||
| 68 | + | ||
| 69 | + assert isinstance(package, GenericPackage) | ||
| 70 | + assert package.message == "201 Created" | ||
| 71 | + | ||
| 72 | + | ||
| 41 | 73 | def test_upload_generic_package_select(tmp_path, project): | |
| 42 | 74 | path = tmp_path / file_name2 | |
| 43 | 75 | path.write_text(file_content) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,7 @@ | |||
| 6 | 6 | import pytest | |
| 7 | 7 | import responses | |
| 8 | 8 | ||
| 9 | + from gitlab import exceptions as exc | ||
| 9 | 10 | from gitlab.v4.objects import ( | |
| 10 | 11 | GenericPackage, | |
| 11 | 12 | GroupPackage, | |
@@ -281,6 +282,74 @@ def test_upload_generic_package(tmp_path, project, resp_upload_generic_package): | |||
| 281 | 282 | assert isinstance(package, GenericPackage) | |
| 282 | 283 | ||
| 283 | 284 | ||
| 285 | + def test_upload_generic_package_nonexistent_path(tmp_path, project): | ||
| 286 | + with pytest.raises(exc.GitlabUploadError): | ||
| 287 | + project.generic_packages.upload( | ||
| 288 | + package_name=package_name, | ||
| 289 | + package_version=package_version, | ||
| 290 | + file_name=file_name, | ||
| 291 | + path="bad", | ||
| 292 | + ) | ||
| 293 | + | ||
| 294 | + | ||
| 295 | + def test_upload_generic_package_no_file_and_no_data(tmp_path, project): | ||
| 296 | + path = tmp_path / file_name | ||
| 297 | + | ||
| 298 | + path.write_text(file_content, encoding="utf-8") | ||
| 299 | + | ||
| 300 | + with pytest.raises(exc.GitlabUploadError): | ||
| 301 | + project.generic_packages.upload( | ||
| 302 | + package_name=package_name, | ||
| 303 | + package_version=package_version, | ||
| 304 | + file_name=file_name, | ||
| 305 | + ) | ||
| 306 | + | ||
| 307 | + | ||
| 308 | + def test_upload_generic_package_file_and_data(tmp_path, project): | ||
| 309 | + path = tmp_path / file_name | ||
| 310 | + | ||
| 311 | + path.write_text(file_content, encoding="utf-8") | ||
| 312 | + | ||
| 313 | + with pytest.raises(exc.GitlabUploadError): | ||
| 314 | + project.generic_packages.upload( | ||
| 315 | + package_name=package_name, | ||
| 316 | + package_version=package_version, | ||
| 317 | + file_name=file_name, | ||
| 318 | + path=path, | ||
| 319 | + data=path.read_bytes(), | ||
| 320 | + ) | ||
| 321 | + | ||
| 322 | + | ||
| 323 | + def test_upload_generic_package_bytes(tmp_path, project, resp_upload_generic_package): | ||
| 324 | + path = tmp_path / file_name | ||
| 325 | + | ||
| 326 | + path.write_text(file_content, encoding="utf-8") | ||
| 327 | + | ||
| 328 | + package = project.generic_packages.upload( | ||
| 329 | + package_name=package_name, | ||
| 330 | + package_version=package_version, | ||
| 331 | + file_name=file_name, | ||
| 332 | + data=path.read_bytes(), | ||
| 333 | + ) | ||
| 334 | + | ||
| 335 | + assert isinstance(package, GenericPackage) | ||
| 336 | + | ||
| 337 | + | ||
| 338 | + def test_upload_generic_package_file(tmp_path, project, resp_upload_generic_package): | ||
| 339 | + path = tmp_path / file_name | ||
| 340 | + | ||
| 341 | + path.write_text(file_content, encoding="utf-8") | ||
| 342 | + | ||
| 343 | + package = project.generic_packages.upload( | ||
| 344 | + package_name=package_name, | ||
| 345 | + package_version=package_version, | ||
| 346 | + file_name=file_name, | ||
| 347 | + data=path.open(mode="rb"), | ||
| 348 | + ) | ||
| 349 | + | ||
| 350 | + assert isinstance(package, GenericPackage) | ||
| 351 | + | ||
| 352 | + | ||
| 284 | 353 | def test_download_generic_package(project, resp_download_generic_package): | |
| 285 | 354 | package = project.generic_packages.download( | |
| 286 | 355 | package_name=package_name, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments