-
Notifications
You must be signed in to change notification settings - Fork 298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow client options to be set in magics context #322
Merged
cguardia
merged 8 commits into
googleapis:master
from
cguardia:160-magics-client-options
Oct 14, 2020
+188
−8
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d70a513
feat: allow client options to be set in magics context
cguardia f8dfac5
Merge branch 'master' into 160-magics-client-options
cguardia 74802fc
add separate client options for storage client
cguardia 8f49562
Merge branch '160-magics-client-options' of github.com:cguardia/pytho…
cguardia 3a821dd
Merge branch 'master' into 160-magics-client-options
cguardia 97a32cf
use unambiguous name for bigquery storage client options
cguardia 16cb9aa
Merge branch '160-magics-client-options' of github.com:cguardia/pytho…
cguardia 0753276
Merge branch 'master' into 160-magics-client-options
cguardia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -139,6 +139,7 @@ | |||||
|
||||||
import re | ||||||
import ast | ||||||
import copy | ||||||
import functools | ||||||
import sys | ||||||
import time | ||||||
|
@@ -155,6 +156,7 @@ | |||||
import six | ||||||
|
||||||
from google.api_core import client_info | ||||||
from google.api_core import client_options | ||||||
from google.api_core.exceptions import NotFound | ||||||
import google.auth | ||||||
from google.cloud import bigquery | ||||||
|
@@ -178,11 +180,12 @@ def __init__(self): | |||||
self._project = None | ||||||
self._connection = None | ||||||
self._default_query_job_config = bigquery.QueryJobConfig() | ||||||
self._client_options = client_options.ClientOptions() | ||||||
|
||||||
@property | ||||||
def credentials(self): | ||||||
"""google.auth.credentials.Credentials: Credentials to use for queries | ||||||
performed through IPython magics | ||||||
performed through IPython magics. | ||||||
|
||||||
Note: | ||||||
These credentials do not need to be explicitly defined if you are | ||||||
|
@@ -217,7 +220,7 @@ def credentials(self, value): | |||||
@property | ||||||
def project(self): | ||||||
"""str: Default project to use for queries performed through IPython | ||||||
magics | ||||||
magics. | ||||||
|
||||||
Note: | ||||||
The project does not need to be explicitly defined if you have an | ||||||
|
@@ -239,6 +242,30 @@ def project(self): | |||||
def project(self, value): | ||||||
self._project = value | ||||||
|
||||||
@property | ||||||
def client_options(self): | ||||||
"""google.api_core.client_options.ClientOptions: client options to be | ||||||
used through IPython magics. | ||||||
|
||||||
Note:: | ||||||
The client options do not need to be explicitly defined if no | ||||||
special network connections are required. Normally you would be | ||||||
using the https://www.googleapis.com/ end point. | ||||||
|
||||||
Example: | ||||||
Manually setting the endpoint: | ||||||
|
||||||
>>> from google.cloud.bigquery import magics | ||||||
>>> client_options = {} | ||||||
>>> client_options['api_endpoint'] = "https://some.special.url" | ||||||
>>> magics.context.client_options = client_options | ||||||
""" | ||||||
return self._client_options | ||||||
|
||||||
@client_options.setter | ||||||
def client_options(self, value): | ||||||
self._client_options = value | ||||||
|
||||||
@property | ||||||
def default_query_job_config(self): | ||||||
"""google.cloud.bigquery.job.QueryJobConfig: Default job | ||||||
|
@@ -410,6 +437,15 @@ def _create_dataset_if_necessary(client, dataset_id): | |||||
"Standard SQL if this argument is not used." | ||||||
), | ||||||
) | ||||||
@magic_arguments.argument( | ||||||
"--api_endpoint", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might actually want two API endpoints, one for the BigQuery client and one for the BigQuery Storage client. |
||||||
type=str, | ||||||
default=None, | ||||||
help=( | ||||||
"The desired API endpoint, e.g., compute.googlepis.com. Defaults to this " | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make the example bigquery-specific
Suggested change
|
||||||
"option's value in the context client options." | ||||||
), | ||||||
) | ||||||
@magic_arguments.argument( | ||||||
"--use_bqstorage_api", | ||||||
action="store_true", | ||||||
|
@@ -511,11 +547,20 @@ def _cell_magic(line, query): | |||||
params = _helpers.to_query_parameters(ast.literal_eval(params_option_value)) | ||||||
|
||||||
project = args.project or context.project | ||||||
|
||||||
client_options = copy.deepcopy(context.client_options) | ||||||
if args.api_endpoint: | ||||||
if isinstance(client_options, dict): | ||||||
client_options["api_endpoint"] = args.api_endpoint | ||||||
else: | ||||||
client_options.api_endpoint = args.api_endpoint | ||||||
|
||||||
client = bigquery.Client( | ||||||
project=project, | ||||||
credentials=context.credentials, | ||||||
default_query_job_config=context.default_query_job_config, | ||||||
client_info=client_info.ClientInfo(user_agent=IPYTHON_USER_AGENT), | ||||||
client_options=client_options, | ||||||
) | ||||||
if context._connection: | ||||||
client._connection = context._connection | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might actually want two client options, one for the BigQuery client and one for the BigQuery Storage client.