Skip to content

Commit 1c59838

Browse files
committed
Refactor
1 parent c1a67d6 commit 1c59838

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,13 +36,13 @@ 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

@@ -62,7 +62,7 @@ def test_use_UnixAdapter_directly():
6262

6363
def test_unix_domain_adapter_ok():
6464
with UnixSocketServerThread() as usock_thread:
65-
session = requests_unixsocket.Session('http+unix://')
65+
session = Session('http+unix://')
6666
urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
6767
url = 'http+unix://%s/path/to/page' % urlencoded_usock
6868

@@ -78,7 +78,7 @@ def test_unix_domain_adapter_ok():
7878
assert r.headers['X-Transport'] == 'unix domain socket'
7979
assert r.headers['X-Requested-Path'] == '/path/to/page'
8080
assert r.headers['X-Socket-Path'] == usock_thread.usock
81-
assert isinstance(r.connection, requests_unixsocket.UnixAdapter)
81+
assert isinstance(r.connection, UnixAdapter)
8282
assert r.url.lower() == url.lower()
8383
if method == 'head':
8484
assert r.text == ''
@@ -88,7 +88,7 @@ def test_unix_domain_adapter_ok():
8888

8989
def test_unix_domain_adapter_alt_settings_1_ok():
9090
with UnixSocketServerThread() as usock_thread:
91-
session = requests_unixsocket.Session(
91+
session = Session(
9292
url_scheme='http+unix://',
9393
settings=alt_settings_1,
9494
)
@@ -106,7 +106,7 @@ def test_unix_domain_adapter_alt_settings_1_ok():
106106
assert r.headers['X-Transport'] == 'unix domain socket'
107107
assert r.headers['X-Requested-Path'] == '/path/to/page'
108108
assert r.headers['X-Socket-Path'] == usock_thread.usock
109-
assert isinstance(r.connection, requests_unixsocket.UnixAdapter)
109+
assert isinstance(r.connection, UnixAdapter)
110110
assert r.url.lower() == url.lower()
111111
if method == 'head':
112112
assert r.text == ''
@@ -116,7 +116,7 @@ def test_unix_domain_adapter_alt_settings_1_ok():
116116

117117
def test_unix_domain_adapter_url_with_query_params():
118118
with UnixSocketServerThread() as usock_thread:
119-
session = requests_unixsocket.Session('http+unix://')
119+
session = Session('http+unix://')
120120
urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
121121
url = ('http+unix://%s'
122122
'/containers/nginx/logs?timestamp=true' % urlencoded_usock)
@@ -134,7 +134,7 @@ def test_unix_domain_adapter_url_with_query_params():
134134
assert r.headers['X-Requested-Path'] == '/containers/nginx/logs'
135135
assert r.headers['X-Requested-Query-String'] == 'timestamp=true'
136136
assert r.headers['X-Socket-Path'] == usock_thread.usock
137-
assert isinstance(r.connection, requests_unixsocket.UnixAdapter)
137+
assert isinstance(r.connection, UnixAdapter)
138138
assert r.url.lower() == url.lower()
139139
if method == 'head':
140140
assert r.text == ''
@@ -144,7 +144,7 @@ def test_unix_domain_adapter_url_with_query_params():
144144

145145
def test_unix_domain_adapter_url_with_fragment():
146146
with UnixSocketServerThread() as usock_thread:
147-
session = requests_unixsocket.Session('http+unix://')
147+
session = Session('http+unix://')
148148
urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
149149
url = ('http+unix://%s'
150150
'/containers/nginx/logs#some-fragment' % urlencoded_usock)
@@ -161,7 +161,7 @@ def test_unix_domain_adapter_url_with_fragment():
161161
assert r.headers['X-Transport'] == 'unix domain socket'
162162
assert r.headers['X-Requested-Path'] == '/containers/nginx/logs'
163163
assert r.headers['X-Socket-Path'] == usock_thread.usock
164-
assert isinstance(r.connection, requests_unixsocket.UnixAdapter)
164+
assert isinstance(r.connection, UnixAdapter)
165165
assert r.url.lower() == url.lower()
166166
if method == 'head':
167167
assert r.text == ''
@@ -170,7 +170,7 @@ def test_unix_domain_adapter_url_with_fragment():
170170

171171

172172
def test_unix_domain_adapter_connection_error():
173-
session = requests_unixsocket.Session('http+unix://')
173+
session = Session('http+unix://')
174174

175175
for method in ['get', 'post', 'head', 'patch', 'put', 'delete', 'options']:
176176
with pytest.raises(requests.ConnectionError):
@@ -179,7 +179,7 @@ def test_unix_domain_adapter_connection_error():
179179

180180

181181
def test_unix_domain_adapter_connection_proxies_error():
182-
session = requests_unixsocket.Session('http+unix://')
182+
session = Session('http+unix://')
183183

184184
for method in ['get', 'post', 'head', 'patch', 'put', 'delete', 'options']:
185185
with pytest.raises(ValueError) as excinfo:
@@ -192,7 +192,7 @@ def test_unix_domain_adapter_connection_proxies_error():
192192

193193
def test_unix_domain_adapter_monkeypatch():
194194
with UnixSocketServerThread() as usock_thread:
195-
with requests_unixsocket.monkeypatch('http+unix://'):
195+
with monkeypatch('http+unix://'):
196196
urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
197197
url = 'http+unix://%s/path/to/page' % urlencoded_usock
198198

@@ -209,7 +209,7 @@ def test_unix_domain_adapter_monkeypatch():
209209
assert r.headers['X-Requested-Path'] == '/path/to/page'
210210
assert r.headers['X-Socket-Path'] == usock_thread.usock
211211
assert isinstance(r.connection,
212-
requests_unixsocket.UnixAdapter)
212+
UnixAdapter)
213213
assert r.url.lower() == url.lower()
214214
if method == 'head':
215215
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)