def refresh_client(self): if os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", False): import google.oauth2.service_account # google-auth creds = google.oauth2.service_account.Credentials.from_service_account_file( os.environ["GOOGLE_APPLICATION_CREDENTIALS"], scopes=["https://www.googleapis.com/auth/cloud-platform"], ) else: import google.auth.credentials # google-auth path = os.path.join(os.path.expanduser("~"), ".config/gcloud/credentials.db") if not os.path.exists(path): raise GCPCredentialsError() conn = sqlite3.connect(path) creds_rows = conn.execute("select * from credentials").fetchall() with tmpfile() as f: with open(f, "w") as f_: # take first row f_.write(creds_rows[0][1]) creds, _ = google.auth.load_credentials_from_file(filename=f) return googleapiclient.discovery.build( "compute", "v1", credentials=creds, requestBuilder=build_request(creds))
def test_build_request(): assert build_request()(None, lambda x: x, "https://example.com")