1515## Imports
1616##########################################################################
1717
18- import os
1918from btrdb .conn import Connection , BTrDB
2019from btrdb .endpoint import Endpoint
21- from btrdb .exceptions import ConnectionError , CredentialsFileNotFound , \
22- ProfileNotFound
20+ from btrdb .exceptions import ConnectionError
2321from btrdb .version import get_version
24- from btrdb .utils .credentials import load_profile
22+ from btrdb .utils .credentials import credentials_by_profile , credentials
2523
2624##########################################################################
2725## Module Variables
3735## Functions
3836##########################################################################
3937
40- def _connect (conn_str = None , apikey = None ):
41- return BTrDB (Endpoint (Connection (conn_str , apikey = apikey ).channel ))
38+ def _connect (endpoints = None , apikey = None ):
39+ return BTrDB (Endpoint (Connection (endpoints , apikey = apikey ).channel ))
4240
4341def connect (conn_str = None , apikey = None , profile = None ):
4442 """
@@ -64,39 +62,18 @@ def connect(conn_str=None, apikey=None, profile=None):
6462 An instance of the BTrDB context to directly interact with the database.
6563
6664 """
67- # Check function arguments
68-
65+ # do not allow user to provide both address and profile
6966 if conn_str and profile :
7067 raise ValueError ("Received both conn_str and profile arguments." )
7168
69+ # use specific profile if requested
7270 if profile :
73- credentials = load_profile (profile )
74- return _connect (credentials ["endpoints" ], credentials ["api_key" ])
75-
76- if conn_str :
77- return _connect (conn_str , apikey )
78-
79- # Check ENV variables
80-
81- keys = os .environ .keys ()
82- if BTRDB_PROFILE in keys and BTRDB_ENDPOINTS in keys :
83- raise ValueError ("Found both BTRDB_PROFILE and BTRDB_ENDPOINTS in ENV. "
84- "Only one is allowed." )
85-
86- if BTRDB_PROFILE in keys :
87- credentials = load_profile (os .environ [BTRDB_PROFILE ])
88- return _connect (credentials ["endpoints" ], credentials ["api_key" ])
71+ return _connect (** credentials_by_profile (profile ))
8972
90- if BTRDB_ENDPOINTS in keys :
91- return _connect (os .environ [BTRDB_ENDPOINTS ], os .environ .get (BTRDB_API_KEY , None ))
92-
93-
94- # Attempt default profile (no arguments or ENV found)
95-
96- try :
97- credentials = load_profile ("default" )
98- return _connect (credentials ["endpoints" ], credentials ["api_key" ])
99- except (CredentialsFileNotFound , ProfileNotFound ):
100- pass
73+ # resolve credentials using combination of arguments, env
74+ creds = credentials (conn_str , apikey )
75+ if "endpoints" in creds :
76+ return _connect (** creds )
10177
10278 raise ConnectionError ("Could not determine credentials to use." )
79+
0 commit comments