Skip to content

Commit

Permalink
Mark SDK disconnected (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaskivskyi authored Dec 26, 2023
1 parent 90b5f10 commit 79ca616
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,4 @@ cython_debug/
# Other

test*
local*
15 changes: 15 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
},
"black-formatter.args": [],
"isort.args": [
"--profile",
"black"
],
"python.analysis.typeCheckingMode": "basic",
}
6 changes: 6 additions & 0 deletions aiochroma/aiochroma.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,9 @@ def identity(self) -> dict[str, str]:
"""Chroma versions"""

return self._connection.identity

@property
def connected(self) -> bool:
"""Connection state"""

return self._connection.connected
55 changes: 45 additions & 10 deletions aiochroma/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@

import aiohttp

from .const import CREDENTIALS, DEFAULT_PORT, DEFAULT_SLEEP, HEADERS, URL, URL_MAIN
from .error import ChromaResultError
from aiochroma.const import (
CREDENTIALS,
DEFAULT_PORT,
DEFAULT_SLEEP,
HEADERS,
URL,
URL_MAIN,
)
from aiochroma.error import ChromaError, ChromaResultError

_LOGGER = logging.getLogger(__name__)

Expand All @@ -35,6 +42,19 @@ def __init__(
self._connected: bool = False
self._sid: int | None = None

### ------------------------
### Service methods -->
### ------------------------

def _mark_disconnected(self) -> None:
"""Mark connection as disconnected."""

self._connected = False

### ------------------------
### <-- Service methods
### ------------------------

### SEND REQUESTS ->

async def async_request(
Expand All @@ -46,7 +66,10 @@ async def async_request(
) -> dict[str, Any]:
"""Send a request"""

json_body = {}
# Check that we are connected
if not self._connected and endpoint != URL_MAIN:
if not await self.async_connect():
raise ChromaError("Cannot connect to Chroma SDK")

url = URL.format(
self._host,
Expand All @@ -55,17 +78,29 @@ async def async_request(
)

try:
async with method(url=url, headers=HEADERS, data=payload, ssl=True) as r:
json_body = await r.json()
async with method(
url=url, headers=HEADERS, data=payload, ssl=True
) as response:
responce_status = response.status
if responce_status == 404:
raise ChromaError("Chroma SDK is not available")

json_body = await response.json()

if "result" in json_body and json_body["result"] != 0:
raise ChromaResultError(json_body["result"])

await self.async_sleep(interval)

if "result" in json_body and json_body["result"] != 0:
raise ChromaResultError(json_body["result"])
return json_body

await self.async_sleep(interval)
except aiohttp.ClientConnectorError as ex:
self._mark_disconnected()
raise ChromaError("Cannot connect to the Chroma SDK") from ex

return json_body
except Exception as ex:
raise ex
self._mark_disconnected()
raise ChromaError("Error communicating with the Chroma SDK") from ex

async def async_delete(
self,
Expand Down

0 comments on commit 79ca616

Please sign in to comment.