Skip to content

Commit 7714c7d

Browse files
committed
add from API test
1 parent ed5aec6 commit 7714c7d

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

.github/workflows/python.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
working-directory: py
1414
env:
1515
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
16+
API_KEY: ${{secrets.API_KEY}}
1617
bq:
1718
runs-on: ubuntu-latest
1819
environment: data

py/davidkhala/gcp/auth/options.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ def from_service_account(info: ServiceAccountInfo = None, *, client_email, priva
4343
c.credentials = service_account.Credentials.from_service_account_info(info)
4444
c.projectId = info['project_id']
4545
return c
46+
47+
@staticmethod
48+
def from_api_key(api_key: str, client_options=None) -> dict:
49+
if client_options is None:
50+
client_options = {}
51+
client_options["api_key"] = api_key
52+
return client_options

py/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ auth = ["google-auth"]
1515

1616
[tool.poetry.group.dev.dependencies]
1717
pytest = "*"
18+
google-cloud-compute = "*"
1819

1920
[build-system]
2021
requires = ["poetry-core"]

py/tests/auth_test.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ def test_from_service_account(self):
1313
private_key=os.environ.get('PRIVATE_KEY'),
1414
)
1515
self.assertEqual(str(type(o.credentials)), "<class 'google.oauth2.service_account.Credentials'>")
16-
def test_always_green(self):
17-
pass
16+
17+
def test_from_api_key(self):
18+
from google.cloud.compute import RegionsClient
19+
api_key = os.environ.get('API_KEY')
20+
client = RegionsClient(
21+
client_options=AuthOptions.from_api_key(api_key)
22+
)
23+
for region in client.list(project='gcp-data-davidkhala'):
24+
print(region.name)
25+
1826

1927
if __name__ == '__main__':
2028
unittest.main()

0 commit comments

Comments
 (0)