from typing import Annotated, Literal
from fastapi import Depends, FastAPI, HTTPException, Request, responses
from yemot_api.api_model import ApiModel
from yemot_api.api_response import id_list_message_text, join, routing_yemot
from yemot_api.input_types import FileAction
from yemot_api.yemot_api import Yemot
def verify_yemot(request: Request) -> Literal[True]:
user_agent = request.headers.get("user-agent")
if user_agent != "yemot-core-api/1.0":
raise HTTPException(status_code=403, detail=f"Access denied: {user_agent}")
return True
class ApiModelExample(ApiModel):
path: str
token: str
# app = FastAPI(dependencies=[Depends(verify_yemot)])
app = FastAPI()
@app.get("/foo")
def foo(q: Annotated[ApiModelExample, Depends()]) -> responses.PlainTextResponse:
yemot_ins = Yemot(q.token)
if not yemot_ins.check_if_file_exists(q.path):
return responses.PlainTextResponse("ERROR", 404)
content = yemot_ins.download_file(q.path)
with open("testtt.wav", "wb") as f:
f.write(content)
yemot_ins.file_action(FileAction.MOVE, q.path, "1")
res_1 = id_list_message_text("הנך מועבר למערכת אחרת")
res_2 = routing_yemot("0773137770")
res = join(res_1, res_2)
return responses.PlainTextResponse(res, 200)Uses the new API at https://rest.yemot.co.il/start with a permanent API key instead of a token.
All the Yemot / AsyncYemot methods work the same, the switch is selected with switch_id (the uuid from search_switches).
from yemot_api.yemot_rest_api import YemotRest
with YemotRest("yk_live_...") as yemot:
switches = yemot.search_switches()
yemot.switch_id = switches.items[0].uuid
print(yemot.get_ivr2_dir("ivr2:/"))from yemot_api.async_yemot_rest_api import AsyncYemotRest
async with AsyncYemotRest("yk_live_...", switch_id="<SWITCH_UUID>") as yemot:
print(await yemot.get_customer_data())By the system phone number (searches the switches of the account, LookupError when not found):
with YemotRest.from_number("yk_live_...", "0791234567") as yemot:
print(yemot.get_customer_data())
yemot = await AsyncYemotRest.from_number("yk_live_...", "+972791234567")Errors of the REST API (bad key, permissions, missing switch_id, rate limit) raise YemotAPIError with the error code in message_code and status_code, details, request_id and retry_after (on HTTP 429, wait before retrying).
Account methods: get_profile, get_security, get_api_key, revoke_api_key, search_switches, get_switch, find_switch.
Not available with an API key: login, logout, render_ymgr_file and the session and security methods (set_password, set_customer_details, double_auth, mfa_session, get_all_sessions, kill_session, kill_all_sessions).
upload_file sends the file in a single request, up to YemotRest.MAX_UPLOAD_SIZE (about 20MB, the gateway limit), the chunked upload for larger files does not work through the gateway.
check_if_file_exists and check_if_folder_exists are checked with GetFile.
get_session and get_customer_data do not return access_password and record_password.