@@ -800,81 +800,81 @@ def get_client(self, db_url=None):
800
800
"""Creates a client based on the db_url. Clients may be cached."""
801
801
if db_url is None :
802
802
db_url = self ._db_url
803
- base_url , params , use_fake_creds = \
804
- _DatabaseService ._parse_db_url (db_url , self ._emulator_host )
803
+
804
+ base_url , namespace = _DatabaseService ._parse_db_url (db_url , self ._emulator_host )
805
+ if base_url == 'https://{0}.firebaseio.com' .format (namespace ):
806
+ # Production base_url. No need to specify namespace in query params.
807
+ params = {}
808
+ credential = self ._credential .get_credential ()
809
+ else :
810
+ # Emulator base_url. Use fake credentials and specify ?ns=foo in query params.
811
+ credential = _EmulatorAdminCredentials ()
812
+ params = {'ns' : namespace }
805
813
if self ._auth_override :
806
814
params ['auth_variable_override' ] = self ._auth_override
807
815
808
816
client_cache_key = (base_url , json .dumps (params , sort_keys = True ))
809
817
if client_cache_key not in self ._clients :
810
- if use_fake_creds :
811
- credential = _EmulatorAdminCredentials ()
812
- else :
813
- credential = self ._credential .get_creden
10000
tial ()
814
818
client = _Client (credential , base_url , self ._timeout , params )
815
819
self ._clients [client_cache_key ] = client
816
820
return self ._clients [client_cache_key ]
817
821
818
822
@classmethod
819
823
def _parse_db_url (cls , url , emulator_host = None ):
820
- """Parses a database URL into (base_url, query_params, use_fake_creds) .
824
+ """Parses (base_url, namespace) from a database URL .
821
825
822
826
The input can be either a production URL (https://foo-bar.firebaseio.com/)
823
- or an Emulator URL (http://localhost:8080/?ns=foo-bar). The resulting
824
- base_url never includes query params. Any required query parameters will
825
- be returned separately as a map (e.g. `{"ns": "foo-bar"}`) .
827
+ or an Emulator URL (http://localhost:8080/?ns=foo-bar). In case of Emulator
828
+ URL, the namespace is extracted from the query param ns. The resulting
829
+ base_url never includes query params .
826
830
827
831
If url is a production URL and emulator_host is specified, the result
828
- base URL will use emulator_host, with a ns query parameter indicating
829
- the namespace, parsed from the production URL. emulator_host is ignored
830
- if url is already an emulator URL. In either case, use_fake_creds will
831
- be set to True.
832
+ base URL will use emulator_host instead. emulator_host is ignored
833
+ if url is already an emulator URL.
832
834
"""
833
835
if not url or not isinstance (url , six .string_types ):
834
836
raise ValueError (
835
837
'Invalid database URL: "{0}". Database URL must be a non-empty '
836
838
'URL string.' .format (url ))
837
839
parsed_url = urllib .parse .urlparse (url )
838
- use_fake_creds = False
839
840
if parsed_url .netloc .endswith ('.firebaseio.com' ):
840
- if parsed_url .scheme != 'https' :
841
- raise ValueError (
842
- 'Invalid database URL: "{0}". Database URL must be an HTTPS URL.' .format (url ))
843
- # pylint: disable=invalid-name
844
- base_url , namespace = cls ._parse_production_url (parsed_url )
845
- if emulator_host :
846
- base_url = 'http://{0}' .format (emulator_host )
847
- use_fake_creds = True
841
+ base_url , namespace = cls ._parse_production_url (parsed_url , emulator_host )
848
842
else :
849
- use_fake_creds = True
850
843
base_url , namespace = cls ._parse_emulator_url (parsed_url )
851
844
852
- if not base_url or not namespace :
845
+ return base_url , namespace
846
+
847
+ @classmethod
848
+ def _parse_production_url (cls , parsed_url , emulator_host ):
849
+ # Handle production URL like https://foo-bar.firebaseio.com/
850
+ if parsed_url .scheme != 'https' :
851
+ raise ValueError (
852
+ 'Invalid database URL scheme: "{0}". Database URL must be an HTTPS URL.' .format (
853
+ parsed_url .scheme ))
854
+ namespace = parsed_url .netloc .split ('.' )[0 ]
855
+ if not namespace :
853
856
raise ValueError (
854
857
'Invalid database URL: "{0}". Database URL must be a valid URL to a '
855
- 'Firebase Realtime Database instance.' .format (url ))
856
- if base_url == 'https://{0}.firebaseio.com' .format (namespace ):
857
- # namespace can be inferred from the base_url. No need for query params.
858
- return base_url , {}, use_fake_creds
859
- else :
860
- # ?ns=foo is needed.
861
- return base_url , {'ns' : namespace }, use_fake_creds
858
+ 'Firebase Realtime Database instance.' .format (parsed_url .geturl ()))
862
859
863
- @classmethod
864
- def _parse_production_url (cls , parsed_url ):
865
- base_url = 'https://{0}' .format (parsed_url .netloc )
866
- return base_url , parsed_url .netloc .split ('.' )[0 ]
860
+ if emulator_host :
861
+ base_url = 'http://{0}' .format (emulator_host )
862
+ else :
863
+ base_url = 'https://{0}' .format (parsed_url .netloc )
864
+ return base_url , namespace
867
865
868
866
@classmethod
869
867
def _parse_emulator_url (cls , parsed_url ):
870
868
# Handle emulator URL like http://localhost:8080/?ns=foo-bar
871
869
query_ns = urllib .parse .parse_qs (parsed_url .query ).get ('ns' )
872
- if parsed_url .scheme == 'http' :
873
- if query_ns and len ( query_ns ) == 1 and query_ns [ 0 ]:
874
- base_url = ' {0}://{1}' . format ( parsed_url . scheme , parsed_url . netloc )
875
- return base_url , query_ns [ 0 ]
870
+ if parsed_url .scheme != 'http' or ( not query_ns or len ( query_ns ) != 1 or not query_ns [ 0 ]) :
871
+ raise ValueError (
872
+ 'Invalid database URL: " {0}". Database URL must be a valid URL to a '
873
+ 'Firebase Realtime Database instance.' . format ( parsed_url . geturl ()))
876
874
877
- return None , None
875
+ namespace = query_ns [0 ]
876
+ base_url = '{0}://{1}' .format (parsed_url .scheme , parsed_url .netloc )
877
+ return base_url , namespace
878
878
879
879
@classmethod
880
880
def _get_auth_override (cls , app ):
0 commit comments