Skip to content

Commit

Permalink
Merge pull request #620 from vespa-engine/jobergum/more-cases-where-n…
Browse files Browse the repository at this point in the history
…ot-found-should-not-raise

Jobergum/more cases where not found should not raise
  • Loading branch information
kkraune committed Nov 8, 2023
2 parents 5681635 + 5a82d71 commit 70a3fab
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
16 changes: 8 additions & 8 deletions vespa/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import sys
import ssl
from xmlrpc.client import Boolean
import aiohttp
import asyncio
import requests
Expand Down Expand Up @@ -36,7 +35,7 @@

VESPA_CLOUD_SECRET_TOKEN: str = "VESPA_CLOUD_SECRET_TOKEN"

def raise_for_status(response: Response, ignore_not_found:bool=False) -> None:
def raise_for_status(response: Response, raise_on_not_found: Optional[bool]=False) -> None:
"""
Raises an appropriate error if necessary.
Expand All @@ -52,7 +51,7 @@ def raise_for_status(response: Response, ignore_not_found:bool=False) -> None:
except HTTPError as http_error:
try:
response_json = response.json()
if response.status_code == 404 and ignore_not_found:
if response.status_code == 404 and not raise_on_not_found:
return
except JSONDecodeError:
raise http_error
Expand Down Expand Up @@ -479,14 +478,15 @@ def delete_all_docs(
)

def get_data(
self, data_id: str, schema: Optional[str]=None, namespace: str = None, **kwargs
self, data_id: str, schema: Optional[str]=None, namespace: str = None, raise_on_not_found:Optional[bool]=False, **kwargs
) -> VespaResponse:
"""
Get a data point from a Vespa app.
:param data_id: Unique id associated with this data point.
:param schema: The schema that we are getting data from. Will attempt to infer schema name if not provided.
:param namespace: The namespace that we are getting data from. If no namespace is provided the schema is used.
:param raise_on_not_found: Raise an exception if the data_id is not found. Default is False.
:param kwargs: Additional arguments to be passed to the HTTP GET request https://docs.vespa.ai/en/reference/document-v1-api-reference.html#request-parameters
:return: Response of the HTTP GET request.
"""
Expand All @@ -499,7 +499,7 @@ def get_data(

with VespaSync(self,pool_connections=1,pool_maxsize=1) as sync_app:
return sync_app.get_data(
schema=schema, data_id=data_id, namespace=namespace, **kwargs
schema=schema, data_id=data_id, namespace=namespace, raise_on_not_found=raise_on_not_found, **kwargs
)

def update_data(
Expand Down Expand Up @@ -569,7 +569,6 @@ def predict(self, x, model_id, function_name="output_0"):
)
)


class VespaSync(object):
def __init__(self, app: Vespa, pool_maxsize: int = 100, pool_connections=100) -> None:
self.app = app
Expand Down Expand Up @@ -767,14 +766,15 @@ def delete_all_docs(
return last_response

def get_data(
self, schema: str, data_id: str, namespace: str = None, **kwargs
self, schema: str, data_id: str, namespace: str = None, raise_on_not_found: Optional[bool]=False, **kwargs
) -> VespaResponse:
"""
Get a data point from a Vespa app.
:param schema: The schema that we are getting data from.
:param data_id: Unique id associated with this data point.
:param namespace: The namespace that we are getting data from.
:param raise_on_not_found: Raise an exception if the document is not found.
:param kwargs: Additional HTTP request parameters (https://docs.vespa.ai/en/reference/document-v1-api-reference.html#request-parameters)
:return: Response of the HTTP GET request.
:raises HTTPError: if one occurred
Expand All @@ -786,7 +786,7 @@ def get_data(
self.app.end_point, namespace, schema, str(data_id)
)
response = self.http_session.get(end_point, params=kwargs)
raise_for_status(response,ignore_not_found=True)
raise_for_status(response, raise_on_not_found=raise_on_not_found)
return VespaResponse(
json=response.json(),
status_code=response.status_code,
Expand Down
12 changes: 7 additions & 5 deletions vespa/test_integration_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from requests import HTTPError
from typing import List
from vespa.io import VespaResponse

from vespa.package import (
HNSW,
Expand Down Expand Up @@ -277,8 +278,10 @@ def execute_data_operations(
#
# Get data that does not exist
#
with pytest.raises(HTTPError):
app.get_data(schema=schema_name, data_id=fields_to_send["id"])

response:VespaResponse = app.get_data(schema=schema_name, data_id=fields_to_send["id"])
self.assertEqual(response.status_code, 404)
self.assertFalse(response.is_successfull())

#
# Feed a data point
Expand Down Expand Up @@ -351,9 +354,8 @@ def execute_data_operations(
)
#
# Deleted data should be gone
#
with pytest.raises(HTTPError):
app.get_data(schema=schema_name, data_id=fields_to_send["id"])
response: VespaResponse = app.get_data(schema=schema_name, data_id=fields_to_send["id"])
self.assertFalse(response.is_successfull())

#
# Update a non-existent data point
Expand Down
2 changes: 0 additions & 2 deletions vespa/test_integration_vespa_cloud_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import asyncio
import shutil
import unittest
import pytest
from requests import HTTPError
from vespa.application import Vespa
from vespa.package import AuthClient, Parameter
from vespa.deployment import VespaCloud
Expand Down

0 comments on commit 70a3fab

Please sign in to comment.