Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7dabe32

Browse files
committed
Implement DB URL connection support
1 parent 9aa68fe commit 7dabe32

File tree

2 files changed

+238
-15
lines changed

2 files changed

+238
-15
lines changed

rethinkdb/net.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
import struct
2626
import time
2727

28+
try:
29+
from urllib.parse import urlparse, parse_qs
30+
except ImportError:
31+
from urlparse import urlparse, parse_qs
32+
2833
from rethinkdb import ql2_pb2
2934
from rethinkdb.ast import DB, ReQLDecoder, ReQLEncoder, Repl, expr
3035
from rethinkdb.errors import (
@@ -703,9 +708,6 @@ def __init__(self, *args, **kwargs):
703708
Connection.__init__(self, ConnectionInstance, *args, **kwargs)
704709

705710

706-
707-
708-
709711
def make_connection(
710712
connection_type,
711713
host=None,
@@ -716,20 +718,41 @@ def make_connection(
716718
password=None,
717719
timeout=20,
718720
ssl=None,
721+
url=None,
719722
_handshake_version=10,
720723
**kwargs):
721-
if host is None:
722-
host = 'localhost'
723-
if port is None:
724-
port = DEFAULT_PORT
725-
if user is None:
726-
user = 'admin'
727-
if timeout is None:
728-
timeout = 20
729-
if ssl is None:
730-
ssl = dict()
731-
if _handshake_version is None:
732-
_handshake_version = 10
724+
if url:
725+
connection_string = urlparse(url)
726+
query_string = parse_qs(connection_string.query)
727+
728+
# Reverse the tuple, this way we can ensure that the host:port/user:pass
729+
# will be always at the same position
730+
host_port, _, user_pass = connection_string.netloc.partition("@")[::-1]
731+
user, password = user_pass.partition(":")[0], user_pass.partition(":")[2]
732+
host, port = host_port.partition(":")[0], host_port.partition(":")[2]
733+
734+
db = connection_string.path.replace("/", "") or None
735+
auth_key = query_string.get("auth_key")
736+
timeout = query_string.get("timeout")
737+
738+
if auth_key:
739+
auth_key = auth_key[0]
740+
741+
if timeout:
742+
timeout = int(timeout[0])
743+
744+
745+
host = host or 'localhost'
746+
port = port or DEFAULT_PORT
747+
user = user or 'admin'
748+
timeout = timeout or 20
749+
ssl = ssl or dict()
750+
_handshake_version = _handshake_version or 10
751+
752+
# The internal APIs will wait for none to deal with auth_key and password
753+
# TODO: refactor when we drop python2
754+
if not password and not password is None:
755+
password = None
733756

734757
conn = connection_type(host, port, db, auth_key, user, password, timeout, ssl, _handshake_version, **kwargs)
735758
return conn.reconnect(timeout=timeout)

tests/test_net.py

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
import pytest
2+
from mock import Mock, ANY
3+
from rethinkdb.net import make_connection, DefaultConnection, DEFAULT_PORT
4+
5+
6+
@pytest.mark.unit
7+
class TestMakeConnection(object):
8+
def setup_method(self):
9+
self.reconnect = Mock()
10+
self.conn_type = Mock()
11+
self.conn_type.return_value.reconnect.return_value = self.reconnect
12+
13+
self.host = "myhost"
14+
self.port = "1234"
15+
self.db = "mydb"
16+
self.auth_key = None
17+
self.user = "gabor"
18+
self.password = "strongpass"
19+
self.timeout = 20
20+
21+
22+
def test_make_connection(self):
23+
ssl = dict()
24+
_handshake_version = 10
25+
26+
conn = make_connection(
27+
self.conn_type,
28+
host=self.host,
29+
port=self.port,
30+
db=self.db,
31+
auth_key=self.auth_key,
32+
user=self.user,
33+
password=self.password,
34+
timeout=self.timeout,
35+
)
36+
37+
assert conn == self.reconnect
38+
self.conn_type.assert_called_once_with(
39+
self.host,
40+
self.port,
41+
self.db,
42+
self.auth_key,
43+
self.user,
44+
self.password,
45+
self.timeout,
46+
ssl,
47+
_handshake_version
48+
)
49+
50+
51+
def test_make_connection_db_url(self):
52+
url = "rethinkdb://gabor:strongpass@myhost:1234/mydb?auth_key=mykey&timeout=30"
53+
ssl = dict()
54+
_handshake_version = 10
55+
56+
conn = make_connection(self.conn_type, url=url)
57+
58+
assert conn == self.reconnect
59+
self.conn_type.assert_called_once_with(
60+
self.host,
61+
self.port,
62+
self.db,
63+
"mykey",
64+
self.user,
65+
self.password,
66+
30,
67+
ssl,
68+
_handshake_version
69+
)
70+
71+
72+
def test_make_connection_no_host(self):
73+
conn = make_connection(
74+
self.conn_type,
75+
port=self.port,
76+
db=self.db,
77+
auth_key=self.auth_key,
78+
user=self.user,
79+
password=self.password,
80+
timeout=self.timeout,
81+
)
82+
83+
assert conn == self.reconnect
84+
self.conn_type.assert_called_once_with(
85+
"localhost",
86+
self.port,
87+
self.db,
88+
self.auth_key,
89+
self.user,
90+
self.password,
91+
self.timeout,
92+
ANY,
93+
ANY
94+
)
95+
96+
97+
def test_make_connection_no_port(self):
98+
conn = make_connection(
99+
self.conn_type,
100+
host=self.host,
101+
db=self.db,
102+
auth_key=self.auth_key,
103+
user=self.user,
104+
password=self.password,
105+
timeout=self.timeout,
106+
)
107+
108+
assert conn == self.reconnect
109+
self.conn_type.assert_called_once_with(
110+
self.host,
111+
DEFAULT_PORT,
112+
self.db,
113+
self.auth_key,
114+
self.user,
115+
self.password,
116+
self.timeout,
117+
ANY,
118+
ANY
119+
)
120+
121+
122+
def test_make_connection_no_user(self):
123+
conn = make_connection(
124+
self.conn_type,
125+
host=self.host,
126+
port=self.port,
127+
db=self.db,
128+
auth_key=self.auth_key,
129+
password=self.password,
130+
timeout=self.timeout,
131+
)
132+
133+
assert conn == self.reconnect
134+
self.conn_type.assert_called_once_with(
135+
self.host,
136+
self.port,
137+
self.db,
138+
self.auth_key,
139+
"admin",
140+
self.password,
141+
self.timeout,
142+
ANY,
143+
ANY
144+
)
145+
146+
147+
def test_make_connection_with_ssl(self):
148+
ssl = dict()
149+
150+
conn = make_connection(
151+
self.conn_type,
152+
host=self.host,
153+
port=self.port,
154+
db=self.db,
155+
auth_key=self.auth_key,
156+
user=self.user,
157+
password=self.password,
158+
timeout=self.timeout,
159+
ssl=ssl,
160+
)
161+
162+
assert conn == self.reconnect
163+
self.conn_type.assert_called_once_with(
164+
self.host,
165+
self.port,
166+
self.db,
167+
self.auth_key,
168+
self.user,
169+
self.password,
170+
self.timeout,
171+
ssl,
172+
ANY
173+
)
174+
175+
176+
def test_make_connection_different_handshake_version(self):
177+
conn = make_connection(
178+
self.conn_type,
179+
host=self.host,
180+
port=self.port,
181+
db=self.db,
182+
auth_key=self.auth_key,
183+
user=self.user,
184+
password=self.password,
185+
timeout=self.timeout,
186+
_handshake_version=20,
187+
)
188+
189+
assert conn == self.reconnect
190+
self.conn_type.assert_called_once_with(
191+
self.host,
192+
self.port,
193+
self.db,
194+
self.auth_key,
195+
self.user,
196+
self.password,
197+
self.timeout,
198+
ANY,
199+
20
200+
)

0 commit comments

Comments
 (0)