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

Improve server version handling #77

Merged
merged 5 commits into from
Aug 31, 2023
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
28 changes: 27 additions & 1 deletion tests/client_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# mypy: ignore-errors

import pytest
from packaging.version import Version
from pytest_httpx import HTTPXMock

from tests.conftest import API_BASE_URL, mock_healthcheck, undo_mock_healthcheck
from zep_python import APIError
from zep_python.zep_client import ZepClient, concat_url
from zep_python.zep_client import ZepClient, concat_url, parse_version_string

_ = mock_healthcheck, undo_mock_healthcheck

Expand Down Expand Up @@ -54,3 +55,28 @@ def test_concat_url():
concat_url("https://server.com/zep/", "v1/api")
== "https://server.com/zep/v1/api"
)


def test_parse_version_string_with_dash():
assert parse_version_string("1.2.3-456") == Version("1.2.3")


def test_parse_version_string_with_dash_and_empty_prefix():
assert parse_version_string("-456") == Version("0.0.0")


def test_parse_version_string_with_dash_and_empty_prefix():
assert parse_version_string("abc") == Version("0.0.0")


def test_parse_version_string_without_dash():
assert parse_version_string("1.2.3") == Version("0.0.0")


def test_parse_version_string_empty():
assert parse_version_string("") == Version("0.0.0")


def test_parse_version_string_none():
with pytest.raises(TypeError):
parse_version_string(None)
30 changes: 28 additions & 2 deletions zep_python/zep_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from urllib.parse import urljoin

import httpx
from packaging.version import Version
from packaging.version import InvalidVersion, Version

from zep_python.document.client import DocumentClient
from zep_python.exceptions import APIError
Expand Down Expand Up @@ -117,7 +117,8 @@ def _healthcheck(self, base_url: str) -> None:
if zep_server_version_str:
if "dev" in zep_server_version_str:
return
zep_server_version = Version(zep_server_version_str.split("-")[0])

zep_server_version = parse_version_string(zep_server_version_str)
else:
zep_server_version = Version("0.0.0")

Expand Down Expand Up @@ -268,3 +269,28 @@ def deprecated_warning(func: Callable[..., Any]) -> Callable[..., Any]:
stacklevel=3,
)
return func


def parse_version_string(version_string: str) -> Version:
"""
Parse a string into a Version object.

Parameters
----------
version_string : str
The version string to parse.

Returns
-------
Version
The parsed version.
"""

try:
if "-" in version_string:
version_str = version_string.split("-")[0]
return Version(version_str if version_str else "0.0.0")
except InvalidVersion:
return Version("0.0.0")

return Version("0.0.0")