Skip to content

Commit af31e46

Browse files
authored
Merge pull request #23 from gdcc/add-proxy-option
Add proxy option to `upload` method
2 parents 693467d + 3fd867f commit af31e46

5 files changed

Lines changed: 69 additions & 7 deletions

File tree

.github/workflows/test.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ on: [push, pull_request]
55
jobs:
66
build:
77
runs-on: ubuntu-latest
8+
9+
services:
10+
squid:
11+
image: ubuntu/squid:latest
12+
ports:
13+
- 3128:3128
14+
815
strategy:
916
max-parallel: 4
1017
matrix:
11-
python-version: ['3.8', '3.9', '3.10', '3.11']
18+
python-version: ["3.8", "3.9", "3.10", "3.11"]
1219

1320
env:
1421
PORT: 8080
@@ -29,8 +36,8 @@ jobs:
2936
poetry install --with test
3037
- name: Test with pytest
3138
env:
32-
API_TOKEN: ${{ steps.dataverse.outputs.api_token }}
33-
BASE_URL: ${{ steps.dataverse.outputs.base_url }}
34-
DVUPLOADER_TESTING: "true"
39+
API_TOKEN: ${{ steps.dataverse.outputs.api_token }}
40+
BASE_URL: ${{ steps.dataverse.outputs.base_url }}
41+
DVUPLOADER_TESTING: "true"
3542
run: |
3643
python3 -m poetry run pytest

dvuploader/directupload.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ async def direct_upload(
3636
progress,
3737
pbars,
3838
n_parallel_uploads: int,
39+
proxy: Optional[str] = None,
3940
) -> None:
4041
"""
4142
Perform parallel direct upload of files to the specified Dataverse repository.
@@ -48,7 +49,7 @@ async def direct_upload(
4849
progress: Progress object to track upload progress.
4950
pbars: List of progress bars for each file.
5051
n_parallel_uploads (int): Number of concurrent uploads to perform.
51-
52+
proxy (str): The proxy to use for the upload.
5253
Returns:
5354
None
5455
"""
@@ -58,6 +59,7 @@ async def direct_upload(
5859
session_params = {
5960
"timeout": None,
6061
"limits": httpx.Limits(max_connections=n_parallel_uploads),
62+
"proxy": proxy,
6163
}
6264

6365
async with httpx.AsyncClient(**session_params) as session:

dvuploader/dvuploader.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def upload(
5858
n_parallel_uploads: int = 1,
5959
force_native: bool = False,
6060
replace_existing: bool = True,
61+
proxy: Optional[str] = None,
6162
) -> None:
6263
"""
6364
Uploads the files to the specified Dataverse repository.
@@ -70,6 +71,7 @@ def upload(
7071
this restricts parallel chunks per upload. Use n_jobs to control parallel files.
7172
force_native (bool): Forces the use of the native upload method instead of direct upload.
7273
replace_existing (bool): Whether to replace files that already exist in the dataset.
74+
proxy (str): The proxy to use for the upload.
7375
7476
Returns:
7577
None

dvuploader/nativeupload.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import tempfile
77
import tenacity
8-
from typing import List, Tuple, Dict
8+
from typing import List, Optional, Tuple, Dict
99

1010
from rich.progress import Progress, TaskID
1111

@@ -29,6 +29,7 @@ async def native_upload(
2929
n_parallel_uploads: int,
3030
pbars,
3131
progress,
32+
proxy: Optional[str] = None,
3233
):
3334
"""
3435
Executes native uploads for the given files in parallel.
@@ -41,7 +42,7 @@ async def native_upload(
4142
n_parallel_uploads (int): The number of parallel uploads to execute.
4243
pbars: List of progress bar IDs to track upload progress.
4344
progress: Progress object to manage progress bars.
44-
45+
proxy (str): The proxy to use for the upload.
4546
Returns:
4647
None
4748
"""
@@ -53,6 +54,7 @@ async def native_upload(
5354
"headers": {"X-Dataverse-key": api_token},
5455
"timeout": None,
5556
"limits": httpx.Limits(max_connections=n_parallel_uploads),
57+
"proxy": proxy,
5658
}
5759

5860
async with httpx.AsyncClient(**session_params) as session:

tests/integration/test_native_upload.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,55 @@ def test_forced_native_upload(
105105
assert len(files) == 3
106106
assert sorted([file["label"] for file in files]) == sorted(expected_files)
107107

108+
def test_native_upload_with_proxy(
109+
self,
110+
credentials,
111+
):
112+
BASE_URL, API_TOKEN = credentials
113+
proxy = "http://127.0.0.1:3128"
114+
115+
with tempfile.TemporaryDirectory() as directory:
116+
# Arrange
117+
create_mock_file(directory, "small_file.txt", size=1)
118+
create_mock_file(directory, "mid_file.txt", size=50)
119+
create_mock_file(directory, "large_file.txt", size=200)
120+
121+
# Add all files in the directory
122+
files = add_directory(directory=directory)
123+
124+
# Create Dataset
125+
pid = create_dataset(
126+
parent="Root",
127+
server_url=BASE_URL,
128+
api_token=API_TOKEN,
129+
)
130+
131+
# Act
132+
uploader = DVUploader(files=files)
133+
uploader.upload(
134+
persistent_id=pid,
135+
api_token=API_TOKEN,
136+
dataverse_url=BASE_URL,
137+
n_parallel_uploads=1,
138+
proxy=proxy,
139+
)
140+
141+
# Assert
142+
files = retrieve_dataset_files(
143+
dataverse_url=BASE_URL,
144+
persistent_id=pid,
145+
api_token=API_TOKEN,
146+
)
147+
148+
expected_files = [
149+
"small_file.txt",
150+
"mid_file.txt",
151+
"large_file.txt",
152+
]
153+
154+
assert len(files) == 3
155+
assert sorted([file["label"] for file in files]) == sorted(expected_files)
156+
108157
def test_native_upload_by_handler(
109158
self,
110159
credentials,

0 commit comments

Comments
 (0)