Skip to content

Commit

Permalink
fix: patches failing health test
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeLeePatterson committed Apr 2, 2023
1 parent 5eb8a3d commit ace7233
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
31 changes: 18 additions & 13 deletions src/pymoai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
handle_unknown_response(msg: Optional[str] = None) -> ApiError
"""
import logging
import time
from typing import Optional

import requests
Expand All @@ -19,13 +20,7 @@
from pymoai.api.datasets import Datasets
from pymoai.config import Configuration, app_config
from pymoai.exceptions import ApiResponseError, InvalidTokenError
from pymoai.schemas import (
ApiError,
ApiMessage,
Credentials,
HealthStatus,
TokenResponse,
)
from pymoai.schemas import ApiError, ApiMessage, Credentials, TokenResponse

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -139,7 +134,7 @@ def get_auth_headers(
self, with_json: bool = False, with_token: bool = True
) -> dict[str, str]:
"""Get auth headers."""
headers: dict[str, str] = {}
headers: dict[str, str] = {"X-Request-Id": str(round(time.time()))}
if with_token:
headers = {**headers, "Authorization": f"Bearer {self.token}"}

Expand All @@ -155,6 +150,16 @@ def add_org_header(self, headers: Optional[dict[str, str]]) -> dict[str, str]:
org_header = self.config.org_header
return {**(headers or {}), org_header: self.org_id}

def parse_response(self, res: requests.Response):
"""Simple helper function to parse json if header present, otherwise present text repr"""
if res is not None:
if "application/json" in res.headers.get("Content-Type", ""):
return res.json()
else:
return res.text
else:
return res

def validate_token(self) -> ApiMessage:
"""Validate token with remote server."""
headers = self.get_auth_headers(with_json=False)
Expand Down Expand Up @@ -190,18 +195,18 @@ def connect(self) -> ApiMessage:

# Api calls

def health(self) -> HealthStatus:
def health(self) -> str:
"""Query the current health of the server."""
url = f"{self.base_url}/health"
url = f"{self.base_url}/healthstatus"
headers = self.get_auth_headers()
headers = self.add_org_header(headers=headers)

logger.debug(f"Api request using headers={headers}")

req = requests.get(url, headers=headers)
res = req.json()
res = requests.get(url, headers=headers)
res = self.parse_response(res)

if "error" in res:
raise ApiResponseError(error=from_dict(data=res, data_class=ApiError))
else:
return from_dict(data=res, data_class=HealthStatus)
return res
4 changes: 1 addition & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ def test_health_with_token():

health = moai.health()

health = health.dict()

print("Health: ", health)

assert health is not None
assert health["status"] == "live"
assert health == "ok"

0 comments on commit ace7233

Please sign in to comment.