Skip to content

Commit 3ae1ee3

Browse files
committed
Add Node creation with a URL string
1 parent e22e31d commit 3ae1ee3

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

typesense/configuration.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
from .exceptions import ConfigError
22
from .logger import logger
3-
3+
from urllib.parse import urlparse
44

55
class Node(object):
6+
def __init__(self, url):
7+
parsed = urlparse(url);
8+
if not parsed.hostname:
9+
raise ConfigError('Node URL does not contain the host name.')
10+
if not parsed.port:
11+
raise ConfigError('Node URL does not contain the port.')
12+
if not parsed.scheme:
13+
raise ConfigError('Node URL does not contain the protocol.')
14+
self.__init__(parsed.hostname, parsed.port, parsed.path, parsed.scheme)
15+
616
def __init__(self, host, port, path, protocol):
717
self.host = host
818
self.port = port
@@ -21,19 +31,21 @@ def __init__(self, config_dict):
2131
Configuration.show_deprecation_warnings(config_dict)
2232
Configuration.validate_config_dict(config_dict)
2333

24-
node_dicts = config_dict.get('nodes', [])
25-
2634
self.nodes = []
27-
for node_dict in node_dicts:
28-
self.nodes.append(
29-
Node(node_dict['host'], node_dict['port'], node_dict.get('path', ''), node_dict['protocol'])
30-
)
35+
for node_config in config_dict.get('nodes', []):
36+
if isinstance(node_config, str):
37+
node = Node(node_config)
38+
else:
39+
node = Node(node_config['host'], node_config['port'], node_config.get('path', ''), node_config['protocol'])
40+
self.nodes.append(node)
3141

3242
nearest_node = config_dict.get('nearest_node', None)
33-
if nearest_node:
34-
self.nearest_node = Node(nearest_node['host'], nearest_node['port'], nearest_node.get('path', ''), nearest_node['protocol'])
35-
else:
43+
if not nearest_node:
3644
self.nearest_node = None
45+
else if isinstance(nearest_node, str):
46+
self.nearest_node = Node(nearest_node)
47+
else:
48+
self.nearest_node = Node(nearest_node['host'], nearest_node['port'], nearest_node.get('path', ''), nearest_node['protocol'])
3749

3850
self.api_key = config_dict.get('api_key', '')
3951
self.connection_timeout_seconds = config_dict.get('connection_timeout_seconds', 3.0)
@@ -53,16 +65,18 @@ def validate_config_dict(config_dict):
5365

5466
for node in nodes:
5567
if not Configuration.validate_node_fields(node):
56-
raise ConfigError('`node` entry must be a dictionary with the following required keys: '
68+
raise ConfigError('`node` entry must be a URL string or a dictionary with the following required keys: '
5769
'host, port, protocol')
5870

5971
nearest_node = config_dict.get('nearest_node', None)
6072
if nearest_node and not Configuration.validate_node_fields(nearest_node):
61-
raise ConfigError('`nearest_node` entry must be a dictionary with the following required keys: '
73+
raise ConfigError('`nearest_node` entry must be a URL string or a dictionary with the following required keys: '
6274
'host, port, protocol')
6375

6476
@staticmethod
6577
def validate_node_fields(node):
78+
if isinstance(node, str):
79+
return true
6680
expected_fields = {'host', 'port', 'protocol'}
6781
return expected_fields.issubset(node)
6882

@@ -76,4 +90,3 @@ def show_deprecation_warnings(config_dict):
7690

7791
if config_dict.get('read_replica_nodes'):
7892
logger.warn('Deprecation warning: read_replica_nodes is now consolidated to nodes, starting with Typesense Server v0.12')
79-

0 commit comments

Comments
 (0)