Skip to content

Commit e7b5e55

Browse files
c0llab0rat0rntninja
authored andcommitted
Rename ClientSync to reflect it being a factory function
1 parent 8da3c12 commit e7b5e55

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

ipfshttpclient/client/base.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,16 @@ def __init__( # type: ignore[no-any-unimported]
393393
assert username and password
394394
auth = (username, password)
395395

396-
self._client = http.ClientSync(
397-
addr, base, offline=offline,
398-
auth=auth, cookies=cookies, headers=headers, timeout=timeout,
396+
self._client = http.build_client_sync(
397+
addr=addr,
398+
base=base,
399+
offline=offline,
400+
auth=auth,
401+
cookies=cookies,
402+
headers=headers,
403+
timeout=timeout
399404
)
405+
400406
if session:
401407
self._client.open_session()
402408

ipfshttpclient/http.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"addr_t", "auth_t", "cookies_t", "headers_t", "params_t", "reqdata_sync_t",
1616
"timeout_t", "workarounds_t",
1717

18-
"ClientSync",
18+
"build_client_sync",
1919
"StreamDecodeIteratorSync",
2020
)
2121

@@ -34,6 +34,22 @@
3434
from . import http_httpx as _backend
3535

3636

37-
# noinspection PyPep8Naming
38-
def ClientSync(*args, **kwargs) -> ClientSyncBase[ty.Any]: # type: ignore[no-untyped-def]
39-
return _backend.ClientSync(*args, **kwargs)
37+
def build_client_sync( # type: ignore[no-any-unimported]
38+
addr: addr_t,
39+
base: str,
40+
offline: bool = False,
41+
auth: auth_t = None,
42+
cookies: cookies_t = None,
43+
headers: headers_t = None,
44+
timeout: timeout_t = 120
45+
) -> ClientSyncBase[ty.Any]:
46+
47+
return _backend.ClientSync(
48+
addr=addr,
49+
base=base,
50+
offline=offline,
51+
auth=auth,
52+
cookies=cookies,
53+
headers=headers or ty.cast(ty.Dict[str, str], {}),
54+
timeout=timeout
55+
)

test/unit/test_http.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def http_server_uds(request):
7676
pytest.skip("Platform does not support Unix domain sockets")
7777

7878
uds_path = make_temp_maxlen_socket_path()
79+
7980
def remove_uds_path():
8081
try:
8182
os.remove(uds_path)
@@ -91,15 +92,15 @@ def remove_uds_path():
9192

9293
@pytest.fixture
9394
def http_client(http_server):
94-
return ipfshttpclient.http.ClientSync(
95+
return ipfshttpclient.http.build_client_sync(
9596
"/ip4/{0}/tcp/{1}/http".format(*http_server.server_address),
9697
ipfshttpclient.DEFAULT_BASE,
9798
)
9899

99100

100101
@pytest.fixture
101102
def http_client_uds(http_server_uds):
102-
return ipfshttpclient.http.ClientSync(
103+
return ipfshttpclient.http.build_client_sync(
103104
"/unix/{0}".format(http_server_uds.server_address.lstrip("/")),
104105
ipfshttpclient.DEFAULT_BASE,
105106
)
@@ -284,7 +285,7 @@ def test_failed_download(http_client, http_server):
284285

285286
def test_download_connect_error():
286287
"""Tests that a download from a non-existing server raises a ConnectionError."""
287-
http_client = ipfshttpclient.http.ClientSync(
288+
http_client = ipfshttpclient.http.build_client_sync(
288289
"/ip4/127.4.5.6/tcp/12393/http",
289290
ipfshttpclient.DEFAULT_BASE
290291
)
@@ -295,7 +296,7 @@ def test_download_connect_error():
295296

296297
def test_download_protocol_error(broken_http_server):
297298
"""Tests that a download from a server violating the HTTP protocol raises a ProtocolError."""
298-
http_client = ipfshttpclient.http.ClientSync(
299+
http_client = ipfshttpclient.http.build_client_sync(
299300
"/ip4/{0}/tcp/{1}/http".format(*broken_http_server.server_address),
300301
ipfshttpclient.DEFAULT_BASE
301302
)
@@ -306,7 +307,7 @@ def test_download_protocol_error(broken_http_server):
306307

307308
def test_download_timeout(slow_http_server):
308309
"""Tests that a timed-out download raises a TimeoutError."""
309-
http_client = ipfshttpclient.http.ClientSync(
310+
http_client = ipfshttpclient.http.build_client_sync(
310311
"/ip4/{0}/tcp/{1}/http".format(*slow_http_server.server_address),
311312
ipfshttpclient.DEFAULT_BASE
312313
)
@@ -317,7 +318,7 @@ def test_download_timeout(slow_http_server):
317318

318319
def test_download_timeout_session(slow_http_server):
319320
"""Tests that a timed-out download raises a TimeoutError."""
320-
http_client = ipfshttpclient.http.ClientSync(
321+
http_client = ipfshttpclient.http.build_client_sync(
321322
"/ip4/{0}/tcp/{1}/http".format(*slow_http_server.server_address),
322323
ipfshttpclient.DEFAULT_BASE,
323324
timeout=0.1
@@ -329,34 +330,37 @@ def test_download_timeout_session(slow_http_server):
329330

330331
def test_request_connect_error():
331332
"""Tests that a request to a non-existing server raises a ConnectionError."""
332-
http_client = ipfshttpclient.http.ClientSync(
333+
http_client = ipfshttpclient.http.build_client_sync(
333334
"/ip4/127.99.99.99/tcp/12393/http",
334335
ipfshttpclient.DEFAULT_BASE
335336
)
336337

337338
with pytest.raises(ipfshttpclient.exceptions.ConnectionError):
338339
http_client.download('/any')
339340

341+
340342
def test_request_protocol_error(broken_http_server):
341343
"""Tests that a download from a server violating the HTTP protocol raises a ProtocolError."""
342-
http_client = ipfshttpclient.http.ClientSync(
344+
http_client = ipfshttpclient.http.build_client_sync(
343345
"/ip4/{0}/tcp/{1}/http".format(*broken_http_server.server_address),
344346
ipfshttpclient.DEFAULT_BASE
345347
)
346348

347349
with pytest.raises(ipfshttpclient.exceptions.ProtocolError):
348350
http_client.request('/any')
349351

352+
350353
def test_request_timeout(slow_http_server):
351354
"""Tests that a timed-out request raises a TimeoutError."""
352-
http_client = ipfshttpclient.http.ClientSync(
355+
http_client = ipfshttpclient.http.build_client_sync(
353356
"/ip4/{0}/tcp/{1}/http".format(*slow_http_server.server_address),
354357
ipfshttpclient.DEFAULT_BASE
355358
)
356359

357360
with pytest.raises(ipfshttpclient.exceptions.TimeoutError):
358361
http_client.request('/timeout', timeout=0.1)
359362

363+
360364
def test_session(http_client, http_server):
361365
"""Tests that a session is established and then closed."""
362366
http_server.serve_content("okay", 200)

0 commit comments

Comments
 (0)