Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix json 401 error #545

Merged
merged 9 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion azure-kusto-data/azure/kusto/data/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from azure.core.tracing import SpanKind

from azure.kusto.data._telemetry import Span, MonitoredActivity
from build.lib.azure.kusto.data.exceptions import KustoServiceError

from .client_base import ExecuteRequestParams, _KustoClientBase
from .client_request_properties import ClientRequestProperties
Expand Down Expand Up @@ -352,7 +353,10 @@ def _execute(
try:
if 300 <= response.status_code < 400:
raise Exception("Unexpected redirection, got status code: " + str(response.status))
response_json = response.json()
if response.text:
response_json = response.json()
else:
raise KustoServiceError("The content of the response contains no data.", response)
response.raise_for_status()
except Exception as e:
raise self._handle_http_error(e, endpoint, request.payload, response, response.status_code, response_json, response.text)
Expand Down
3 changes: 3 additions & 0 deletions azure-kusto-data/azure/kusto/data/client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def _handle_http_error(
if status == 429:
raise KustoThrottlingError("The request was throttled by the server.", response) from exception

if status == 401:
raise KustoServiceError(f"401. Missing adequate access rights.", response) from exception

if payload:
message = f"An error occurred while trying to ingest: Status: {status}, Reason: {response.reason}, Text: {response_text}."
if response_json:
Expand Down
2 changes: 2 additions & 0 deletions azure-kusto-data/tests/kusto_client_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def raise_for_status(self):
file_name = "pandas_bool.json"
elif "print dynamic" in kwargs["json"]["csl"]:
file_name = "dynamic.json"
elif "execute_401" in kwargs["json"]["csl"]:
return MockResponse(None, 401, url)
elif "take 0" in kwargs["json"]["csl"]:
file_name = "zero_results.json"
elif "PrimaryResultName" in kwargs["json"]["csl"]:
Expand Down
11 changes: 10 additions & 1 deletion azure-kusto-data/tests/test_kusto_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from azure.kusto.data import ClientRequestProperties, KustoClient, KustoConnectionStringBuilder
from azure.kusto.data._cloud_settings import CloudSettings
from azure.kusto.data.exceptions import KustoClosedError, KustoMultiApiError, KustoNetworkError
from azure.kusto.data.exceptions import KustoClosedError, KustoMultiApiError, KustoNetworkError, KustoServiceError
from azure.kusto.data.helpers import dataframe_from_result_table
from azure.kusto.data.response import KustoStreamingResponseDataSet
from tests.kusto_client_common import KustoClientTestsMixin, mocked_requests_post, get_response_first_primary_result, get_table_first_row, proxy_kcsb
Expand Down Expand Up @@ -126,6 +126,15 @@ def test_dynamic(self, mock_post, method):
row = get_table_first_row(get_response_first_primary_result(method.__call__(client, "PythonTest", query)))
self._assert_dynamic_response(row)

@patch("requests.Session.post", side_effect=mocked_requests_post)
def test_json_401(self, mock_post, method):
"""Tests 401 permission errors."""
with KustoClient(self.HOST) as client:
with pytest.raises(KustoServiceError, match=f"401. Missing adequate access rights."):
query = "execute_401"
response = method.__call__(client, "PythonTest", query)
get_response_first_primary_result(response)

@patch("requests.Session.post", side_effect=mocked_requests_post)
def test_empty_result(self, mock_post, method):
"""Tests dynamic responses."""
Expand Down
1 change: 1 addition & 0 deletions azure-kusto-ingest/tests/test_e2e_ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class TestE2E:
zipped_json_file_path: ClassVar[str]
cred: ClassVar[Callable[[], DefaultAzureCredential]]
async_cred: ClassVar[Callable[[], DefaultAzureCredential]]
dm_kcsb: KustoConnectionStringBuilder

CHUNK_SIZE = 1024

Expand Down
Loading