Skip to content

Commit

Permalink
Cleaning up discovery API response (#396)
Browse files Browse the repository at this point in the history
* Cleaning up discovery API response
  • Loading branch information
dristysrivastava authored May 29, 2024
1 parent 24c1046 commit d44ac4f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
13 changes: 1 addition & 12 deletions pebblo/app/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,6 @@ class LoaderDocResponseModel(BaseModel):
message: Optional[str] = None


class DiscoverAIApps(BaseModel):
name: str
description: Optional[str]
owner: str
instanceDetails: Optional[InstanceDetails]
framework: Optional[FrameworkInfo]
lastUsed: datetime
pebbloServerVersion: Optional[str]
pebbloClientVersion: Optional[str]


class DiscoverAIAppsResponseModel(BaseModel):
ai_apps_data: Union[DiscoverAIApps, None] = None
pebblo_server_version: Union[str, None] = None
message: Optional[str] = None
27 changes: 10 additions & 17 deletions pebblo/app/service/discovery_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from pebblo.app.models.models import (
AiApp,
Chain,
DiscoverAIApps,
DiscoverAIAppsResponseModel,
InstanceDetails,
Metadata,
Expand Down Expand Up @@ -298,33 +297,27 @@ def process_request(self):
self._write_file_content_to_path(ai_apps, file_path)

# Prepare response
ai_apps_data = ai_apps
ai_apps_obj = DiscoverAIApps(
name=ai_apps_data["name"],
description=ai_apps_data.get("description"),
owner=ai_apps_data.get("owner"),
instanceDetails=ai_apps_data.get("instanceDetails"),
framework=ai_apps_data.get("framework"),
lastUsed=ai_apps_data.get("lastUsed"),
pebbloServerVersion=ai_apps_data.get("pebbloServerVersion"),
pebbloClientVersion=ai_apps_data.get("pebbloClientVersion"),
)
message = "App Discover Request Processed Successfully"
response = DiscoverAIAppsResponseModel(
ai_apps_data=ai_apps_obj, message=message
pebblo_server_version=ai_apps.get("pebbloServerVersion"),
message=message,
)
return PebbloJsonResponse.build(
body=response.dict(exclude_none=True), status_code=200
)
except ValidationError as ex:
response = DiscoverAIAppsResponseModel(ai_apps_data=None, message=str(ex))
logger.error(f"Error in Discovery API process_request. Error:{ex}")
response = DiscoverAIAppsResponseModel(
pebblo_server_version=None, message=str(ex)
)
logger.error(f"Error in Discovery API process_request. Error: {ex}")
return PebbloJsonResponse.build(
body=response.dict(exclude_none=True), status_code=400
)
except Exception as ex:
response = DiscoverAIAppsResponseModel(ai_apps_data=None, message=str(ex))
logger.error(f"Error in Discovery API process_request. Error:{ex}")
response = DiscoverAIAppsResponseModel(
pebblo_server_version=None, message=str(ex)
)
logger.error(f"Error in Discovery API process_request. Error: {ex}")
return PebbloJsonResponse.build(
body=response.dict(exclude_none=True), status_code=500
)
Expand Down
17 changes: 13 additions & 4 deletions tests/app/test_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ def mock_write_json_to_file():
yield mock_write_json_to_file


@pytest.fixture
def mock_pebblo_server_version():
# Mocking the get_pebblo_server_version function
with patch(
"pebblo.app.service.discovery_service.get_pebblo_server_version"
) as mock_get_pebblo_server_version:
# Set the return value of the mocked function
mock_get_pebblo_server_version.return_value = "x.x.x"
yield mock_get_pebblo_server_version


def test_topic_classifier(topic_classifier):
assert topic_classifier is not None

Expand All @@ -80,7 +91,7 @@ def test_root_endpoint():
assert response.json() == {"detail": "Not Found"}


def test_app_discover_success(mock_write_json_to_file):
def test_app_discover_success(mock_write_json_to_file, mock_pebblo_server_version):
"""
Test the app discover endpoint.
"""
Expand All @@ -95,9 +106,7 @@ def test_app_discover_success(mock_write_json_to_file):

# Assertions
assert response.status_code == 200
assert response.json()["ai_apps_data"]["description"] == "This is a test app."
assert response.json()["ai_apps_data"]["name"] == "Test App"
assert response.json()["ai_apps_data"]["owner"] == "Test owner"
assert response.json()["pebblo_server_version"] == "x.x.x"
assert response.json()["message"] == "App Discover Request Processed Successfully"


Expand Down

0 comments on commit d44ac4f

Please sign in to comment.