def _get_api_key_credentials(quota_project_id=None):
    """Gets API key credentials and project ID."""
    from google.auth import api_key

    api_key_value = os.environ.get(environment_vars.API_KEY)
    if api_key_value:
        return api_key.Credentials(api_key_value), quota_project_id
    else:
        return None, None
def test_expired_and_valid():
    credentials = api_key.Credentials("api-key")

    assert credentials.valid
    assert credentials.token == "api-key"
    assert not credentials.expired

    credentials.refresh(None)
    assert credentials.valid
    assert credentials.token == "api-key"
    assert not credentials.expired
def test_before_request():
    credentials = api_key.Credentials("api-key")
    headers = {}

    credentials.before_request(None, "http://example.com", "GET", headers)
    assert headers["x-goog-api-key"] == "api-key"
def test_credentials_constructor():
    with pytest.raises(ValueError) as excinfo:
        api_key.Credentials("")

    assert excinfo.match(r"Token must be a non-empty API key string")
def get_api_key_credentials(api_key_value):
    """Gets API key credentials using the given api key value."""
    from google.auth import api_key

    return api_key.Credentials(api_key_value)