Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
msabramo committed Jan 3, 2022
1 parent 984be21 commit 58c4061
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 38 deletions.
6 changes: 3 additions & 3 deletions examples/docker-info-alt2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import json
from requests.compat import urlparse

from requests_unixsocket import Session, UnixAdapter
from requests_unixsocket import Session, Settings


def custom_urlparse(url):
parsed_url = urlparse(url)
return UnixAdapter.Settings.ParseResult(
return Settings.ParseResult(
sockpath=parsed_url.path,
reqpath=parsed_url.fragment,
)


session = Session(settings=UnixAdapter.Settings(urlparse=custom_urlparse))
session = Session(settings=Settings(urlparse=custom_urlparse))

r = session.get('http+unix://sock.localhost/var/run/docker.sock#/info')
registry_config = r.json()['RegistryConfig']
Expand Down
26 changes: 14 additions & 12 deletions requests_unixsocket/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import sys

import requests
from requests.compat import urlparse, unquote

from .adapters import UnixAdapter
from .settings import default_scheme, default_settings, Settings


def default_urlparse(url):
parsed_url = urlparse(url)
return UnixAdapter.Settings.ParseResult(
sockpath=unquote(parsed_url.netloc),
reqpath=parsed_url.path + '?' + parsed_url.query,
)


default_scheme = 'http+unix://'
default_settings = UnixAdapter.Settings(urlparse=default_urlparse)
# for backwards compatibility
# https://github.com/httpie/httpie-unixsocket uses this for example
DEFAULT_SCHEME = default_scheme


class Session(requests.Session):
Expand Down Expand Up @@ -89,3 +81,13 @@ def delete(url, **kwargs):
def options(url, **kwargs):
kwargs.setdefault('allow_redirects', True)
return request('options', url, **kwargs)


__all__ = [
default_scheme, DEFAULT_SCHEME,
default_settings,
monkeypatch,
Session,
Settings,
request, get, head, post, patch, put, delete, options,
]
8 changes: 0 additions & 8 deletions requests_unixsocket/adapters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import socket
from collections import namedtuple

from requests.adapters import HTTPAdapter
from requests.compat import urlparse
Expand Down Expand Up @@ -57,13 +56,6 @@ def _new_conn(self):


class UnixAdapter(HTTPAdapter):
class Settings(object):
class ParseResult(namedtuple('ParseResult', 'sockpath reqpath')):
pass

def __init__(self, urlparse=None):
self.urlparse = urlparse

def __init__(self, timeout=60, pool_connections=25, settings=None,
*args, **kwargs):
super(UnixAdapter, self).__init__(*args, **kwargs)
Expand Down
30 changes: 15 additions & 15 deletions requests_unixsocket/tests/test_requests_unixsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import requests
from requests.compat import urlparse

import requests_unixsocket
from requests_unixsocket import monkeypatch, Session, Settings, UnixAdapter
from requests_unixsocket.testutils import UnixSocketServerThread


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

return requests_unixsocket.UnixAdapter.Settings.ParseResult(
return Settings.ParseResult(
sockpath=sockpath,
reqpath='/' + os.path.join(*reversed(reqpath_parts)),
)


alt_settings_1 = requests_unixsocket.UnixAdapter.Settings(
alt_settings_1 = Settings(
urlparse=lambda url: get_sock_prefix(urlparse(url).path),
)


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

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

def test_unix_domain_adapter_alt_settings_1_ok():
with UnixSocketServerThread() as usock_thread:
session = requests_unixsocket.Session(
session = Session(
url_scheme='http+unix://',
settings=alt_settings_1,
)
Expand All @@ -93,7 +93,7 @@ def test_unix_domain_adapter_alt_settings_1_ok():
assert r.headers['X-Transport'] == 'unix domain socket'
assert r.headers['X-Requested-Path'] == '/path/to/page'
assert r.headers['X-Socket-Path'] == usock_thread.usock
assert isinstance(r.connection, requests_unixsocket.UnixAdapter)
assert isinstance(r.connection, UnixAdapter)
assert r.url.lower() == url.lower()
if method == 'head':
assert r.text == ''
Expand All @@ -103,7 +103,7 @@ def test_unix_domain_adapter_alt_settings_1_ok():

def test_unix_domain_adapter_url_with_query_params():
with UnixSocketServerThread() as usock_thread:
session = requests_unixsocket.Session('http+unix://')
session = Session('http+unix://')
urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
url = ('http+unix://%s'
'/containers/nginx/logs?timestamp=true' % urlencoded_usock)
Expand All @@ -121,7 +121,7 @@ def test_unix_domain_adapter_url_with_query_params():
assert r.headers['X-Requested-Path'] == '/containers/nginx/logs'
assert r.headers['X-Requested-Query-String'] == 'timestamp=true'
assert r.headers['X-Socket-Path'] == usock_thread.usock
assert isinstance(r.connection, requests_unixsocket.UnixAdapter)
assert isinstance(r.connection, UnixAdapter)
assert r.url.lower() == url.lower()
if method == 'head':
assert r.text == ''
Expand All @@ -131,7 +131,7 @@ def test_unix_domain_adapter_url_with_query_params():

def test_unix_domain_adapter_url_with_fragment():
with UnixSocketServerThread() as usock_thread:
session = requests_unixsocket.Session('http+unix://')
session = Session('http+unix://')
urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
url = ('http+unix://%s'
'/containers/nginx/logs#some-fragment' % urlencoded_usock)
Expand All @@ -148,7 +148,7 @@ def test_unix_domain_adapter_url_with_fragment():
assert r.headers['X-Transport'] == 'unix domain socket'
assert r.headers['X-Requested-Path'] == '/containers/nginx/logs'
assert r.headers['X-Socket-Path'] == usock_thread.usock
assert isinstance(r.connection, requests_unixsocket.UnixAdapter)
assert isinstance(r.connection, UnixAdapter)
assert r.url.lower() == url.lower()
if method == 'head':
assert r.text == ''
Expand All @@ -157,7 +157,7 @@ def test_unix_domain_adapter_url_with_fragment():


def test_unix_domain_adapter_connection_error():
session = requests_unixsocket.Session('http+unix://')
session = Session('http+unix://')

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


def test_unix_domain_adapter_connection_proxies_error():
session = requests_unixsocket.Session('http+unix://')
session = Session('http+unix://')

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

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

Expand All @@ -196,7 +196,7 @@ def test_unix_domain_adapter_monkeypatch():
assert r.headers['X-Requested-Path'] == '/path/to/page'
assert r.headers['X-Socket-Path'] == usock_thread.usock
assert isinstance(r.connection,
requests_unixsocket.UnixAdapter)
UnixAdapter)
assert r.url.lower() == url.lower()
if method == 'head':
assert r.text == ''
Expand Down
5 changes: 5 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ deps =
coverage
{[testenv]deps}

[testenv:dev]
deps =
python-semantic-release
{[testenv]deps}

[testenv:doctest]
# note this only works under python 3 because of unicode literals
commands =
Expand Down

0 comments on commit 58c4061

Please sign in to comment.