Skip to content

Commit

Permalink
Require project when initializing the client library, and un-document…
Browse files Browse the repository at this point in the history
… `gcloud-legacy`

PiperOrigin-RevId: 711290096
  • Loading branch information
Google Earth Engine Authors committed Jan 10, 2025
1 parent 1109074 commit 26abea8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 26 deletions.
9 changes: 3 additions & 6 deletions python/ee/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,13 @@ def Authenticate(
Args:
authorization_code: An optional authorization code.
quiet: If true, do not require interactive prompts and force --no-browser
mode for gcloud-legacy. If false, never supply --no-browser. Default is
None, which autodetects the --no-browser setting.
quiet: If true, do not require interactive prompts. If false, never supply
--no-browser. Default is None, which autodetects the --no-browser setting.
code_verifier: PKCE verifier to prevent auth code stealing.
auth_mode: The authentication mode. One of:
"colab" - use the Colab authentication flow;
"notebook" - send user to notebook authenticator page;
"gcloud" - use gcloud to obtain credentials;
"gcloud-legacy" - use legacy gcloud flow to obtain credentials;
"localhost" - runs auth flow in local browser only;
None - a default mode is chosen based on your environment.
scopes: List of scopes to use for authentication. Defaults to [
Expand Down Expand Up @@ -185,8 +183,7 @@ def Initialize(
credentials = data.get_persistent_credentials()
if not project and credentials and hasattr(credentials, 'quota_project_id'):
project = credentials.quota_project_id
# SDK credentials are not authorized for EE so a project must be given.
if not project and oauth.is_sdk_credentials(credentials):
if not project:
raise EEException(NO_PROJECT_EXCEPTION)

data.initialize(
Expand Down
2 changes: 1 addition & 1 deletion python/ee/apitestcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def InitializeApi(self):
ee.data.getTableDownloadId = self._MockTableDownload
# pylint: disable-next=protected-access
ee.deprecation._FetchDataCatalogStac = self._MockFetchDataCatalogStac
ee.Initialize(None, '')
ee.Initialize(None, '', project='my-project')

# We are mocking the url here so the unit tests are happy.
def _MockMapId(self, params: Dict[str, Any]) -> Dict[str, str]:
Expand Down
4 changes: 2 additions & 2 deletions python/ee/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,14 @@ def __init__(self, parser: argparse.ArgumentParser):
parser.add_argument(
'--quiet',
action='store_true',
help='Do not prompt for input, run gcloud-legacy in no-browser mode.')
help='Do not prompt for input.')
parser.add_argument(
'--code-verifier',
help='PKCE verifier to prevent auth code stealing.')
parser.add_argument(
'--auth_mode',
help='One of: notebook - use notebook authenticator; colab - use Colab'
' authenticator; gcloud / gcloud-legacy - use gcloud;'
' authenticator; gcloud - use gcloud;'
' localhost[:PORT|:0] - use local browser')
parser.add_argument(
'--scopes', help='Optional comma-separated list of scopes.')
Expand Down
4 changes: 2 additions & 2 deletions python/ee/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,8 @@ def authenticate(
"localhost" - sends credentials to the Python environment on the same
localhost as the browser. Does not work for remote shells. Default
port is 8085; use localhost:N set port or localhost:0 to auto-select.
"gcloud-legacy" - use less convenient gcloud mode, for users without
cloud projects.
"gcloud-legacy" - included for legacy compatibility but not materially
different from "gcloud".
"appdefault" - included for legacy compatibility but not necessary.
ee.Initialize() will always check for application default credentials.
None - a default mode is chosen based on your environment.
Expand Down
20 changes: 10 additions & 10 deletions python/ee/tests/ee_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def MockAlgorithms():
self.assertEqual(ee.ApiFunction._api, {})
self.assertTrue(ee.Image._initialized)

# Verify that ee.Initialize(None) does not override custom URLs.
ee.Initialize(None)
# Verify that ee.Initialize() without a URL does not override custom URLs.
ee.Initialize(None, project='my-project')
self.assertTrue(ee.data._initialized)
self.assertEqual(ee.data._api_base_url, 'foo/api')

Expand Down Expand Up @@ -85,9 +85,9 @@ def CheckDataInit(**kwargs):
ee.Initialize()

google_creds = google_creds.with_quota_project(None)
expected_project = None
ee.Initialize()
self.assertEqual(5, inits.call_count)
with self.assertRaisesRegex(ee.EEException, '.*no project found..*'):
ee.Initialize()
self.assertEqual(4, inits.call_count)

msg = 'Earth Engine API has not been used in project 764086051850 before'
with moc(ee.ApiFunction, 'initialize', side_effect=ee.EEException(msg)):
Expand All @@ -98,7 +98,7 @@ def CheckDataInit(**kwargs):
cred_args['refresh_token'] = 'rt'
with self.assertRaisesRegex(ee.EEException, '.*no project found..*'):
ee.Initialize()
self.assertEqual(6, inits.call_count)
self.assertEqual(4, inits.call_count)

def testCallAndApply(self):
"""Verifies library initialization."""
Expand All @@ -122,7 +122,7 @@ def MockAlgorithms():

ee.data.getAlgorithms = MockAlgorithms

ee.Initialize(None)
ee.Initialize(None, project='my-project')
image1 = ee.Image(1)
image2 = ee.Image(2)
expected = ee.Image(
Expand Down Expand Up @@ -227,7 +227,7 @@ def MockAlgorithms():

ee.data.getAlgorithms = MockAlgorithms

ee.Initialize(None)
ee.Initialize(None, project='my-project')

# Verify that the expected classes got generated.
self.assertTrue(hasattr(ee, 'Array'))
Expand Down Expand Up @@ -312,7 +312,7 @@ def MockAlgorithms():
}

ee.data.getAlgorithms = MockAlgorithms
ee.Initialize(None)
ee.Initialize(None, project='my-project')

# Try to cast something that's already of the right class.
x = ee.Foo('argument')
Expand Down Expand Up @@ -488,7 +488,7 @@ def MockAlgorithms():

ee.data.getAlgorithms = MockAlgorithms

ee.Initialize(None)
ee.Initialize(None, project='my-project')

# The initialisation shouldn't blow up.
self.assertTrue(callable(ee.Algorithms.Foo))
Expand Down
10 changes: 5 additions & 5 deletions python/ee/tests/image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def test_thumb_with_dimensions_region_crs(self):
)
),
)
self.assertEqual(kwargs['parent'], 'projects/earthengine-legacy')
self.assertEqual('projects/my-project', kwargs['parent'])

def test_thumb_with_dimensions_region_json(self):
# Try it with the region as a GeoJSON string.
Expand All @@ -330,7 +330,7 @@ def test_thumb_with_dimensions_region_json(self):
)
),
)
self.assertEqual(kwargs['parent'], 'projects/earthengine-legacy')
self.assertEqual('projects/my-project', kwargs['parent'])

def test_thumb_with_dimensions_list_coords(self):
# Try it with the region as a list of coordinates.
Expand All @@ -356,7 +356,7 @@ def test_thumb_with_dimensions_list_coords(self):
)
),
)
self.assertEqual(kwargs['parent'], 'projects/earthengine-legacy')
self.assertEqual('projects/my-project', kwargs['parent'])

def test_thumb_with_dimensions_list_min_max(self):
# Try it with the region as a list of coordinates.
Expand All @@ -382,7 +382,7 @@ def test_thumb_with_dimensions_list_min_max(self):
)
),
)
self.assertEqual(kwargs['parent'], 'projects/earthengine-legacy')
self.assertEqual('projects/my-project', kwargs['parent'])

def test_thumb_with_visualization_params(self):
cloud_api_resource = mock.MagicMock()
Expand Down Expand Up @@ -603,7 +603,7 @@ def test_download_url(self):
kwargs['body']['expression'],
)
self.assertEqual('ZIPPED_GEO_TIFF_PER_BAND', kwargs['body']['fileFormat'])
self.assertEqual('projects/earthengine-legacy', kwargs['parent'])
self.assertEqual('projects/my-project', kwargs['parent'])
self.assertEqual(
'/%s/thumbName:getPixels' % _cloud_api_utils.VERSION, url
)
Expand Down

0 comments on commit 26abea8

Please sign in to comment.