Skip to content

Commit dc4b889

Browse files
authored
Merge pull request #1980 from saleweaver/codex/fix-bug-with-reports.get_report_document-vofa5t
2 parents 3fc6b3c + 1e6a441 commit dc4b889

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

sp_api/api/data_kiosk/data_kiosk.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import httpx
21
import urllib.parse
32
from io import BytesIO, StringIO
43
from typing import Union, BinaryIO, TextIO
54

5+
import httpx
6+
67
from sp_api.base import Client, sp_endpoint, fill_query_params, ApiResponse
8+
from sp_api.base._transport_httpx import _httpx_client_kwargs
79

810

911
class DataKiosk(Client):
@@ -197,8 +199,10 @@ def get_document(
197199
)
198200
if download or file or ("decrypt" in kwargs and kwargs["decrypt"]):
199201
with httpx.Client(
200-
proxies=self.proxies,
201-
verify=self.verify,
202+
**_httpx_client_kwargs(
203+
proxies=self.proxies,
204+
verify=self.verify,
205+
)
202206
) as client:
203207
document_response = client.get(res.payload.get("documentUrl"))
204208
document = document_response.content

sp_api/api/reports/reports.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
ApiResponse,
1111
Marketplaces,
1212
)
13+
from sp_api.base._transport_httpx import _httpx_client_kwargs
1314
from sp_api.util import (
1415
normalize_csv_param,
1516
normalize_datetime_kwargs,
@@ -367,9 +368,11 @@ def get_report_document(
367368
if download or file or ("decrypt" in kwargs and kwargs["decrypt"]):
368369
compression_algorithm = res.payload.get("compressionAlgorithm")
369370
with httpx.Client(
370-
proxies=self.proxies,
371-
verify=self.verify,
372-
timeout=timeout,
371+
**_httpx_client_kwargs(
372+
proxies=self.proxies,
373+
verify=self.verify,
374+
timeout=timeout,
375+
)
373376
) as client:
374377
if stream and file:
375378
with client.stream("GET", res.payload.get("url")) as document_response:

sp_api/base/_transport_httpx.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1+
import random
2+
13
import httpx
24

35

6+
def _httpx_client_kwargs(*, proxies=None, verify=True, timeout=None):
7+
client_kwargs = {"verify": verify}
8+
if timeout is not None:
9+
client_kwargs["timeout"] = timeout
10+
if proxies is not None:
11+
if isinstance(proxies, (list, tuple)):
12+
proxy_value = random.choice(proxies)
13+
else:
14+
proxy_value = proxies
15+
client_kwargs["proxy"] = proxy_value
16+
return client_kwargs
17+
18+
419
class HttpxTransport:
520
def __init__(
621
self, *, timeout=None, proxies=None, proxy=None, verify=True, client=None
722
):
823
proxy_config = proxy if proxy is not None else proxies
924
self._client = client or httpx.Client(
10-
timeout=timeout,
11-
proxy=proxy_config,
12-
verify=verify,
25+
**_httpx_client_kwargs(
26+
timeout=timeout,
27+
proxies=proxy_config,
28+
verify=verify,
29+
)
1330
)
1431

1532
def request(self, method, url, *, params=None, data=None, content=None, headers=None):

tests/api/test_httpx_transport.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import random
2+
3+
from sp_api.base._transport_httpx import _httpx_client_kwargs
4+
5+
6+
def test_httpx_client_kwargs_uses_single_proxy_string():
7+
proxies = "https://proxy.example"
8+
9+
kwargs = _httpx_client_kwargs(proxies=proxies, verify=True, timeout=5)
10+
11+
assert kwargs["proxy"] == "https://proxy.example"
12+
assert kwargs["verify"] is True
13+
assert kwargs["timeout"] == 5
14+
15+
16+
def test_httpx_client_kwargs_selects_proxy_from_list(monkeypatch):
17+
proxies = ["https://proxy.example", "https://other.example"]
18+
19+
monkeypatch.setattr(random, "choice", lambda values: values[0])
20+
21+
kwargs = _httpx_client_kwargs(proxies=proxies, verify=False)
22+
23+
assert kwargs["proxy"] == "https://proxy.example"
24+
assert kwargs["verify"] is False

0 commit comments

Comments
 (0)