Skip to content

Commit 365b053

Browse files
committed
Refactor
1 parent 984be21 commit 365b053

File tree

6 files changed

+60
-38
lines changed

6 files changed

+60
-38
lines changed

examples/docker-info-alt2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
import json
44
from requests.compat import urlparse
55

6-
from requests_unixsocket import Session, UnixAdapter
6+
from requests_unixsocket import Session, Settings
77

88

99
def custom_urlparse(url):
1010
parsed_url = urlparse(url)
11-
return UnixAdapter.Settings.ParseResult(
11+
return Settings.ParseResult(
1212
sockpath=parsed_url.path,
1313
reqpath=parsed_url.fragment,
1414
)
1515

1616

17-
session = Session(settings=UnixAdapter.Settings(urlparse=custom_urlparse))
17+
session = Session(settings=Settings(urlparse=custom_urlparse))
1818

1919
r = session.get('http+unix://sock.localhost/var/run/docker.sock#/info')
2020
registry_config = r.json()['RegistryConfig']

requests_unixsocket/__init__.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
import sys
22

33
import requests
4-
from requests.compat import urlparse, unquote
54

65
from .adapters import UnixAdapter
6+
from .settings import default_scheme, default_settings, Settings
77

8-
9-
def default_urlparse(url):
10-
parsed_url = urlparse(url)
11-
return UnixAdapter.Settings.ParseResult(
12-
sockpath=unquote(parsed_url.netloc),
13-
reqpath=parsed_url.path + '?' + parsed_url.query,
14-
)
15-
16-
17-
default_scheme = 'http+unix://'
18-
default_settings = UnixAdapter.Settings(urlparse=default_urlparse)
8+
# for backwards compatibility
9+
# https://github.com/httpie/httpie-unixsocket uses this for example
10+
DEFAULT_SCHEME = default_scheme
1911

2012

2113
class Session(requests.Session):
@@ -89,3 +81,13 @@ def delete(url, **kwargs):
8981
def options(url, **kwargs):
9082
kwargs.setdefault('allow_redirects', True)
9183
return request('options', url, **kwargs)
84+
85+
86+
__all__ = [
87+
default_scheme, DEFAULT_SCHEME,
88+
default_settings,
89+
monkeypatch,
90+
Session,
91+
Settings,
92+
request, get, head, post, patch, put, delete, options,
93+
]

requests_unixsocket/adapters.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import socket
2-
from collections import namedtuple
32

43
from requests.adapters import HTTPAdapter
54
from requests.compat import urlparse
@@ -57,13 +56,6 @@ def _new_conn(self):
5756

5857

5958
class UnixAdapter(HTTPAdapter):
60-
class Settings(object):
61-
class ParseResult(namedtuple('ParseResult', 'sockpath reqpath')):
62-
pass
63-
64-
def __init__(self, urlparse=None):
65-
self.urlparse = urlparse
66-
6759
def __init__(self, timeout=60, pool_connections=25, settings=None,
6860
*args, **kwargs):
6961
super(UnixAdapter, self).__init__(*args, **kwargs)

requests_unixsocket/settings.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from collections import namedtuple
2+
3+
from requests.compat import urlparse, unquote
4+
5+
6+
class Settings(object):
7+
class ParseResult(namedtuple('ParseResult', 'sockpath reqpath')):
8+
pass
9+
10+
def __init__(self, urlparse=None):
11+
self.urlparse = urlparse
12+
13+
14+
def default_urlparse(url):
15+
parsed_url = urlparse(url)
16+
return Settings.ParseResult(
17+
sockpath=unquote(parsed_url.netloc),
18+
reqpath=parsed_url.path + '?' + parsed_url.query,
19+
)
20+
21+
22+
default_scheme = 'http+unix://'
23+
default_settings = Settings(urlparse=default_urlparse)

requests_unixsocket/tests/test_requests_unixsocket.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import requests
1212
from requests.compat import urlparse
1313

14-
import requests_unixsocket
14+
from requests_unixsocket import monkeypatch, Session, Settings, UnixAdapter
1515
from requests_unixsocket.testutils import UnixSocketServerThread
1616

1717

@@ -36,20 +36,20 @@ def get_sock_prefix(path):
3636
sockpath, tail = os.path.split(sockpath)
3737
reqpath_parts.append(tail)
3838

39-
return requests_unixsocket.UnixAdapter.Settings.ParseResult(
39+
return Settings.ParseResult(
4040
sockpath=sockpath,
4141
reqpath='/' + os.path.join(*reversed(reqpath_parts)),
4242
)
4343

4444

45-
alt_settings_1 = requests_unixsocket.UnixAdapter.Settings(
45+
alt_settings_1 = Settings(
4646
urlparse=lambda url: get_sock_prefix(urlparse(url).path),
4747
)
4848

4949

5050
def test_unix_domain_adapter_ok():
5151
with UnixSocketServerThread() as usock_thread:
52-
session = requests_unixsocket.Session('http+unix://')
52+
session = Session('http+unix://')
5353
urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
5454
url = 'http+unix://%s/path/to/page' % urlencoded_usock
5555

@@ -65,7 +65,7 @@ def test_unix_domain_adapter_ok():
6565
assert r.headers['X-Transport'] == 'unix domain socket'
6666
assert r.headers['X-Requested-Path'] == '/path/to/page'
6767
assert r.headers['X-Socket-Path'] == usock_thread.usock
68-
assert isinstance(r.connection, requests_unixsocket.UnixAdapter)
68+
assert isinstance(r.connection, UnixAdapter)
6969
assert r.url.lower() == url.lower()
7070
if method == 'head':
7171
assert r.text == ''
@@ -75,7 +75,7 @@ def test_unix_domain_adapter_ok():
7575

7676
def test_unix_domain_adapter_alt_settings_1_ok():
7777
with UnixSocketServerThread() as usock_thread:
78-
session = requests_unixsocket.Session(
78+
session = Session(
7979
url_scheme='http+unix://',
8080
settings=alt_settings_1,
8181
)
@@ -93,7 +93,7 @@ def test_unix_domain_adapter_alt_settings_1_ok():
9393
assert r.headers['X-Transport'] == 'unix domain socket'
9494
assert r.headers['X-Requested-Path'] == '/path/to/page'
9595
assert r.headers['X-Socket-Path'] == usock_thread.usock
96-
assert isinstance(r.connection, requests_unixsocket.UnixAdapter)
96+
assert isinstance(r.connection, UnixAdapter)
9797
assert r.url.lower() == url.lower()
9898
if method == 'head':
9999
assert r.text == ''
@@ -103,7 +103,7 @@ def test_unix_domain_adapter_alt_settings_1_ok():
103103

104104
def test_unix_domain_adapter_url_with_query_params():
105105
with UnixSocketServerThread() as usock_thread:
106-
session = requests_unixsocket.Session('http+unix://')
106+
session = Session('http+unix://')
107107
urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
108108
url = ('http+unix://%s'
109109
'/containers/nginx/logs?timestamp=true' % urlencoded_usock)
@@ -121,7 +121,7 @@ def test_unix_domain_adapter_url_with_query_params():
121121
assert r.headers['X-Requested-Path'] == '/containers/nginx/logs'
122122
assert r.headers['X-Requested-Query-String'] == 'timestamp=true'
123123
assert r.headers['X-Socket-Path'] == usock_thread.usock
124-
assert isinstance(r.connection, requests_unixsocket.UnixAdapter)
124+
assert isinstance(r.connection, UnixAdapter)
125125
assert r.url.lower() == url.lower()
126126
if method == 'head':
127127
assert r.text == ''
@@ -131,7 +131,7 @@ def test_unix_domain_adapter_url_with_query_params():
131131

132132
def test_unix_domain_adapter_url_with_fragment():
133133
with UnixSocketServerThread() as usock_thread:
134-
session = requests_unixsocket.Session('http+unix://')
134+
session = Session('http+unix://')
135135
urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
136136
url = ('http+unix://%s'
137137
'/containers/nginx/logs#some-fragment' % urlencoded_usock)
@@ -148,7 +148,7 @@ def test_unix_domain_adapter_url_with_fragment():
148148
assert r.headers['X-Transport'] == 'unix domain socket'
149149
assert r.headers['X-Requested-Path'] == '/containers/nginx/logs'
150150
assert r.headers['X-Socket-Path'] == usock_thread.usock
151-
assert isinstance(r.connection, requests_unixsocket.UnixAdapter)
151+
assert isinstance(r.connection, UnixAdapter)
152152
assert r.url.lower() == url.lower()
153153
if method == 'head':
154154
assert r.text == ''
@@ -157,7 +157,7 @@ def test_unix_domain_adapter_url_with_fragment():
157157

158158

159159
def test_unix_domain_adapter_connection_error():
160-
session = requests_unixsocket.Session('http+unix://')
160+
session = Session('http+unix://')
161161

162162
for method in ['get', 'post', 'head', 'patch', 'put', 'delete', 'options']:
163163
with pytest.raises(requests.ConnectionError):
@@ -166,7 +166,7 @@ def test_unix_domain_adapter_connection_error():
166166

167167

168168
def test_unix_domain_adapter_connection_proxies_error():
169-
session = requests_unixsocket.Session('http+unix://')
169+
session = Session('http+unix://')
170170

171171
for method in ['get', 'post', 'head', 'patch', 'put', 'delete', 'options']:
172172
with pytest.raises(ValueError) as excinfo:
@@ -179,7 +179,7 @@ def test_unix_domain_adapter_connection_proxies_error():
179179

180180
def test_unix_domain_adapter_monkeypatch():
181181
with UnixSocketServerThread() as usock_thread:
182-
with requests_unixsocket.monkeypatch('http+unix://'):
182+
with monkeypatch('http+unix://'):
183183
urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
184184
url = 'http+unix://%s/path/to/page' % urlencoded_usock
185185

@@ -196,7 +196,7 @@ def test_unix_domain_adapter_monkeypatch():
196196
assert r.headers['X-Requested-Path'] == '/path/to/page'
197197
assert r.headers['X-Socket-Path'] == usock_thread.usock
198198
assert isinstance(r.connection,
199-
requests_unixsocket.UnixAdapter)
199+
UnixAdapter)
200200
assert r.url.lower() == url.lower()
201201
if method == 'head':
202202
assert r.text == ''

tox.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ deps =
2626
coverage
2727
{[testenv]deps}
2828

29+
[testenv:dev]
30+
deps =
31+
python-semantic-release
32+
{[testenv]deps}
33+
2934
[testenv:doctest]
3035
# note this only works under python 3 because of unicode literals
3136
commands =

0 commit comments

Comments
 (0)