This repository has been archived on 2023-06-09. You can view files and clone it, but cannot push or open issues or pull requests.
jarvis-server/utils/client_utils.py
2021-07-26 22:46:47 +02:00

62 lines
1.6 KiB
Python

import json
import requests
from requests.structures import CaseInsensitiveDict
from utils import config_utils
client_url = config_utils.get_in_config("CLIENT_URL")
def ask_for_microphone_output(record_for_seconds, speech_before_input):
data = {
'record_for_seconds': record_for_seconds,
'speech_before_input': speech_before_input
}
call_client_api("POST", "/record", json.dumps(data))
def ask_for_input(listen_for_seconds, speech_before_input):
data = {
'listen_for_seconds': listen_for_seconds,
'speech_before_input': speech_before_input
}
call_client_api("POST", "/input", json.dumps(data))
def speak(speech):
data = {
'speech': speech
}
call_client_api("POST", "/speak", data)
def sound(sound_name):
data = {
'sound_name': sound_name
}
call_client_api("POST", "/sound", data)
def call_client_api(method, url, json_data=None):
if json_data is None:
json_data = {}
try:
url_service = client_url + url
headers = CaseInsensitiveDict()
headers["Authorization"] = config_utils.get_in_config("API_KEY")
headers["Content-Type"] = "application/json; charset=utf8"
if method == 'GET':
return json.loads(requests.get(url_service, headers=headers).content.decode("utf-8"))
elif method == 'POST':
json_data = json.dumps(json_data)
return json.loads(
requests.post(url_service, headers=headers, data=json_data.encode("utf8")).content.decode("utf-8"))
except:
print("Error when calling the client API")