Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_bad_requests_should_raise_looker_api_errors(
mock_request, method_name, looker_client, client_kwargs, mock_404_response
):
"""Tests each method of LookerClient for how it handles a 404 response"""
mock_request.return_value = mock_404_response
client_method = getattr(looker_client, method_name)
with pytest.raises(LookerApiError):
client_method(**client_kwargs[method_name])
def wrapper(*args, **kwargs):
try:
return function(*args, **kwargs)
except GenericValidationError as error:
sys.exit(error.exit_code)
except LookerApiError as error:
logger.error(
f"\n{error}\n\n"
+ printer.dim(
"Run in verbose mode (-v) or check your log file to see the full "
"response from the Looker API. "
"For support, please create an issue at "
"https://github.com/spectacles-ci/spectacles/issues"
)
+ "\n"
)
looker_api_response = json.dumps(error.looker_api_response, indent=2)
logger.debug(
f"Spectacles received a {error.status} response code from "
f"the Looker API with the following details: {looker_api_response}\n"
)
sys.exit(error.exit_code)
def get_all_branches(self, project: str) -> List[str]:
"""Returns a list of git branches in the project repository.
Args:
project: Name of the Looker project to use.
"""
logger.debug(f"Getting all Git branches in project '{project}'")
url = utils.compose_url(
self.api_url, path=["projects", project, "git_branches"]
)
response = self.get(url=url, timeout=TIMEOUT_SEC)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
raise LookerApiError(
name="unable-to-get-branches",
title="Couldn't get all Git branches.",
status=response.status_code,
detail=(
f"Unable to get all Git branches in project '{project}'. "
"Please try again."
),
response=response,
)
return [branch["name"] for branch in response.json()]
def all_folders(self, project: str) -> List[JsonDict]:
logger.debug("Getting information about all folders")
url = utils.compose_url(self.api_url, path=["folders"])
response = self.get(url=url, timeout=TIMEOUT_SEC)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
raise LookerApiError(
name="unable-to-get-folders",
title="Couldn't obtain project folders.",
status=response.status_code,
detail=(f"Failed to get all folders for project '{project}'."),
response=response,
)
result = response.json()
return result
"""Gets the active branch for the user in the given project.
Args:
project: Name of the Looker project to use.
Returns:
str: Name of the active branch
"""
logger.debug(f"Getting active branch for project '{project}'")
url = utils.compose_url(self.api_url, path=["projects", project, "git_branch"])
response = self.get(url=url, timeout=TIMEOUT_SEC)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
raise LookerApiError(
name="unable-to-get-active-branch",
title="Couldn't determine active Git branch.",
status=response.status_code,
detail=(
f"Unable to get active branch for project '{project}'. "
"Please check that the project exists and try again."
),
response=response,
)
branch_name = response.json()["name"]
logger.debug(f"The active branch is '{branch_name}'")
return response.json()
"""
# Using old-style string formatting so that strings are formatted lazily
logger.debug(
"Attempting to get results for %d query tasks", len(query_task_ids)
)
url = utils.compose_url(self.api_url, path=["query_tasks", "multi_results"])
response = self.get(
url=url,
params={"query_task_ids": ",".join(query_task_ids)},
timeout=TIMEOUT_SEC,
)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
raise LookerApiError(
name="unable-to-get-query-results",
title="Couldn't get results for the specified query tasks.",
status=response.status_code,
detail=(
"Failed to get the results for "
f"{len(query_task_ids)} query tasks. "
"Please try again."
),
response=response,
)
result = response.json()
return result
def reset_to_remote(self, project: str) -> None:
"""Reset a project development branch to the revision of the project that is on the remote.
Args:
project: Name of the Looker project to use.
"""
logger.debug("Resetting branch to remote.")
url = utils.compose_url(
self.api_url, path=["projects", project, "reset_to_remote"]
)
response = self.post(url=url, timeout=TIMEOUT_SEC)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
raise LookerApiError(
name="unable-to-reset-remote",
title="Couldn't checkout Git branch.",
status=response.status_code,
detail=(
"Unable to reset local Git branch"
"to match remote. Please try again."
),
response=response,
)
project: Name of the Looker project to use
Returns:
List[JsonDict]: JSON response containing all LookML/data tests
"""
logger.debug(f"Getting LookML tests for project {project}")
url = utils.compose_url(
self.api_url, path=["projects", project, "lookml_tests"]
)
response = self.get(url=url, timeout=TIMEOUT_SEC)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
raise LookerApiError(
name="unable-to-get-data-tests",
title="Couldn't retrieve all data tests.",
status=response.status_code,
detail=(
f"Unable to retrieve all data tests for "
f"project '{project}'. Please try again."
),
response=response,
)
return response.json()
explore: Name of LookML explore to query.
Returns:
List[str]: Names of all the dimensions in the specified explore. Dimension
names are returned in the format 'explore_name.dimension_name'.
"""
logger.debug(f"Getting all dimensions from explore {explore}")
url = utils.compose_url(
self.api_url, path=["lookml_models", model, "explores", explore]
)
response = self.get(url=url, timeout=TIMEOUT_SEC)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
raise LookerApiError(
name="unable-to-get-dimension-lookml",
title="Couldn't retrieve dimensions.",
status=response.status_code,
detail=(
"Unable to retrieve dimension LookML details "
f"for explore '{model}/{explore}'. Please try again."
),
response=response,
)
return response.json()["fields"]["dimensions"]