6161 ReadPickleBuffer ,
6262 StorageOptions ,
6363)
64- import pydata_google_auth
6564
6665import bigframes ._config .bigquery_options as bigquery_options
6766import bigframes .constants as constants
7574import bigframes .formatting_helpers as formatting_helpers
7675from bigframes .remote_function import read_gbq_function as bigframes_rgf
7776from bigframes .remote_function import remote_function as bigframes_rf
77+ import bigframes .session .clients
7878import bigframes .version
7979
8080# Even though the ibis.backends.bigquery.registry import is unused, it's needed
8585import third_party .bigframes_vendored .pandas .io .parsers .readers as third_party_pandas_readers
8686import third_party .bigframes_vendored .pandas .io .pickle as third_party_pandas_pickle
8787
88- _ENV_DEFAULT_PROJECT = "GOOGLE_CLOUD_PROJECT"
89- _APPLICATION_NAME = f"bigframes/{ bigframes .version .__version__ } "
90- _SCOPES = ["https://www.googleapis.com/auth/cloud-platform" ]
91-
92- # BigQuery is a REST API, which requires the protocol as part of the URL.
93- _BIGQUERY_REGIONAL_ENDPOINT = "https://{location}-bigquery.googleapis.com"
94-
95- # BigQuery Connection and Storage are gRPC APIs, which don't support the
96- # https:// protocol in the API endpoint URL.
97- _BIGQUERYCONNECTION_REGIONAL_ENDPOINT = "{location}-bigqueryconnection.googleapis.com"
98- _BIGQUERYSTORAGE_REGIONAL_ENDPOINT = "{location}-bigquerystorage.googleapis.com"
99-
10088_BIGFRAMES_DEFAULT_CONNECTION_ID = "bigframes-default-connection"
10189
10290_MAX_CLUSTER_COLUMNS = 4
@@ -122,149 +110,6 @@ def _is_query(query_or_table: str) -> bool:
122110 return re .search (r"\s" , query_or_table .strip (), re .MULTILINE ) is not None
123111
124112
125- def _get_default_credentials_with_project ():
126- return pydata_google_auth .default (scopes = _SCOPES , use_local_webserver = False )
127-
128-
129- class ClientsProvider :
130- """Provides client instances necessary to perform cloud operations."""
131-
132- def __init__ (
133- self ,
134- project : Optional [str ],
135- location : Optional [str ],
136- use_regional_endpoints : Optional [bool ],
137- credentials : Optional [google .auth .credentials .Credentials ],
138- ):
139- credentials_project = None
140- if credentials is None :
141- credentials , credentials_project = _get_default_credentials_with_project ()
142-
143- # Prefer the project in this order:
144- # 1. Project explicitly specified by the user
145- # 2. Project set in the environment
146- # 3. Project associated with the default credentials
147- project = (
148- project
149- or os .getenv (_ENV_DEFAULT_PROJECT )
150- or typing .cast (Optional [str ], credentials_project )
151- )
152-
153- if not project :
154- raise ValueError (
155- "Project must be set to initialize BigQuery client. "
156- "Try setting `bigframes.options.bigquery.project` first."
157- )
158-
159- self ._project = project
160- self ._location = location
161- self ._use_regional_endpoints = use_regional_endpoints
162- self ._credentials = credentials
163-
164- # cloud clients initialized for lazy load
165- self ._bqclient = None
166- self ._bqconnectionclient = None
167- self ._bqstorageclient = None
168- self ._cloudfunctionsclient = None
169- self ._resourcemanagerclient = None
170-
171- @property
172- def bqclient (self ):
173- if not self ._bqclient :
174- bq_options = None
175- if self ._use_regional_endpoints :
176- bq_options = google .api_core .client_options .ClientOptions (
177- api_endpoint = _BIGQUERY_REGIONAL_ENDPOINT .format (
178- location = self ._location
179- ),
180- )
181- bq_info = google .api_core .client_info .ClientInfo (
182- user_agent = _APPLICATION_NAME
183- )
184- self ._bqclient = bigquery .Client (
185- client_info = bq_info ,
186- client_options = bq_options ,
187- credentials = self ._credentials ,
188- project = self ._project ,
189- location = self ._location ,
190- )
191-
192- return self ._bqclient
193-
194- @property
195- def bqconnectionclient (self ):
196- if not self ._bqconnectionclient :
197- bqconnection_options = None
198- if self ._use_regional_endpoints :
199- bqconnection_options = google .api_core .client_options .ClientOptions (
200- api_endpoint = _BIGQUERYCONNECTION_REGIONAL_ENDPOINT .format (
201- location = self ._location
202- )
203- )
204- bqconnection_info = google .api_core .gapic_v1 .client_info .ClientInfo (
205- user_agent = _APPLICATION_NAME
206- )
207- self ._bqconnectionclient = (
208- google .cloud .bigquery_connection_v1 .ConnectionServiceClient (
209- client_info = bqconnection_info ,
210- client_options = bqconnection_options ,
211- credentials = self ._credentials ,
212- )
213- )
214-
215- return self ._bqconnectionclient
216-
217- @property
218- def bqstorageclient (self ):
219- if not self ._bqstorageclient :
220- bqstorage_options = None
221- if self ._use_regional_endpoints :
222- bqstorage_options = google .api_core .client_options .ClientOptions (
223- api_endpoint = _BIGQUERYSTORAGE_REGIONAL_ENDPOINT .format (
224- location = self ._location
225- )
226- )
227- bqstorage_info = google .api_core .gapic_v1 .client_info .ClientInfo (
228- user_agent = _APPLICATION_NAME
229- )
230- self ._bqstorageclient = google .cloud .bigquery_storage_v1 .BigQueryReadClient (
231- client_info = bqstorage_info ,
232- client_options = bqstorage_options ,
233- credentials = self ._credentials ,
234- )
235-
236- return self ._bqstorageclient
237-
238- @property
239- def cloudfunctionsclient (self ):
240- if not self ._cloudfunctionsclient :
241- functions_info = google .api_core .gapic_v1 .client_info .ClientInfo (
242- user_agent = _APPLICATION_NAME
243- )
244- self ._cloudfunctionsclient = (
245- google .cloud .functions_v2 .FunctionServiceClient (
246- client_info = functions_info ,
247- credentials = self ._credentials ,
248- )
249- )
250-
251- return self ._cloudfunctionsclient
252-
253- @property
254- def resourcemanagerclient (self ):
255- if not self ._resourcemanagerclient :
256- resourcemanager_info = google .api_core .gapic_v1 .client_info .ClientInfo (
257- user_agent = _APPLICATION_NAME
258- )
259- self ._resourcemanagerclient = (
260- google .cloud .resourcemanager_v3 .ProjectsClient (
261- credentials = self ._credentials , client_info = resourcemanager_info
262- )
263- )
264-
265- return self ._resourcemanagerclient
266-
267-
268113class Session (
269114 third_party_pandas_gbq .GBQIOMixin ,
270115 third_party_pandas_parquet .ParquetIOMixin ,
@@ -279,14 +124,14 @@ class Session(
279124 Configuration adjusting how to connect to BigQuery and related
280125 APIs. Note that some options are ignored if ``clients_provider`` is
281126 set.
282- clients_provider (bigframes.session.ClientsProvider):
127+ clients_provider (bigframes.session.bigframes.session.clients. ClientsProvider):
283128 An object providing client library objects.
284129 """
285130
286131 def __init__ (
287132 self ,
288133 context : Optional [bigquery_options .BigQueryOptions ] = None ,
289- clients_provider : Optional [ClientsProvider ] = None ,
134+ clients_provider : Optional [bigframes . session . clients . ClientsProvider ] = None ,
290135 ):
291136 if context is None :
292137 context = bigquery_options .BigQueryOptions ()
@@ -306,11 +151,12 @@ def __init__(
306151 if clients_provider :
307152 self ._clients_provider = clients_provider
308153 else :
309- self ._clients_provider = ClientsProvider (
154+ self ._clients_provider = bigframes . session . clients . ClientsProvider (
310155 project = context .project ,
311156 location = self ._location ,
312157 use_regional_endpoints = context .use_regional_endpoints ,
313158 credentials = context .credentials ,
159+ application_name = context .application_name ,
314160 )
315161
316162 self ._create_and_bind_bq_session ()
@@ -319,7 +165,7 @@ def __init__(
319165 ibis .bigquery .connect (
320166 project_id = context .project ,
321167 client = self .bqclient ,
322- storage_client = self .bqstorageclient ,
168+ storage_client = self .bqstoragereadclient ,
323169 ),
324170 )
325171
@@ -338,8 +184,8 @@ def bqconnectionclient(self):
338184 return self ._clients_provider .bqconnectionclient
339185
340186 @property
341- def bqstorageclient (self ):
342- return self ._clients_provider .bqstorageclient
187+ def bqstoragereadclient (self ):
188+ return self ._clients_provider .bqstoragereadclient
343189
344190 @property
345191 def cloudfunctionsclient (self ):
0 commit comments