From 4fe3f87d46daee9395627b97af156e9735c0de5e Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Wed, 15 Jun 2022 17:22:43 +0200 Subject: [PATCH] feat(api): assist autocomplete --- api/chalicelib/core/assist.py | 28 ++++++++++++++++++++++++++++ api/routers/core.py | 6 ++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/api/chalicelib/core/assist.py b/api/chalicelib/core/assist.py index f647e95f1..1804da669 100644 --- a/api/chalicelib/core/assist.py +++ b/api/chalicelib/core/assist.py @@ -102,6 +102,34 @@ def is_live(project_id, session_id, project_key=None): return str(session_id) in connected_peers +def autocomplete(project_id, q: str, key: str = None): + project_key = projects.get_project_key(project_id) + params = {"q": q} + if key: + params["key"] = key + try: + results = requests.get(config("assistList") % config("S3_KEY") + f"/{project_key}/autocomplete", + params=params, timeout=config("assistTimeout", cast=int, default=5)) + if results.status_code != 200: + print("!! issue with the peer-server") + print(results.text) + return {"errors": [f"Something went wrong wile calling assist:{results.text}"]} + results = results.json().get("data", []) + except requests.exceptions.Timeout: + print("Timeout getting Assist response") + return {"errors": ["Assist request timeout"]} + except Exception as e: + print("issue getting Assist response") + print(str(e)) + print("expected JSON, received:") + try: + print(results.text) + except: + print("couldn't get response") + return {"errors": ["Something went wrong wile calling assist"]} + return results + + def get_ice_servers(): return config("iceServers") if config("iceServers", default=None) is not None \ and len(config("iceServers")) > 0 else None diff --git a/api/routers/core.py b/api/routers/core.py index 7ad57334e..2ac949057 100644 --- a/api/routers/core.py +++ b/api/routers/core.py @@ -136,10 +136,12 @@ def events_search(projectId: int, q: str, type: Union[schemas.FilterType, schemas.EventType, schemas.PerformanceEventType, schemas.FetchFilterType, schemas.GraphqlFilterType] = None, - key: str = None, - source: str = None, context: schemas.CurrentContext = Depends(OR_context)): + key: str = None, source: str = None, live: bool = False, + context: schemas.CurrentContext = Depends(OR_context)): if len(q) == 0: return {"data": []} + if live: + return assist.autocomplete(project_id=projectId, q=q, key=key) if type in [schemas.FetchFilterType._url]: type = schemas.EventType.request elif type in [schemas.GraphqlFilterType._name]: