Skip to content

API

API

Source code in Client/Responder/api.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class API:
    def __init__(
        self,
        domain: str = API_DOMAIN,
        token: str = "",
        home_id: int = None,
    ):
        """
        init the api
        Args:
            domain (str): the domain of the api
            token (str): the token of the api
            home_id (int): the home id
        """
        self.domain = domain
        self.token = token
        self.mac_address = get_mac_address()
        self.home_id = home_id

    def register_device(
        self,
        device_name: Optional[str] = None,
        device_type: Optional[str] = None,
        description: Optional[str] = None,
    ):
        """
        register the device
        Args:
            device_name (Optional[str]): the device name
            device_type (Optional[str]): the device type
            description (Optional[str]): the description of the device

        Returns:

        """
        url = f"{self.domain}/hardware/register/"

        r = requests.post(
            url,
            data={
                "home": self.home_id,
                "mac_address": self.mac_address,
                "device_name": device_name,
                "device_type": device_type,
                "description": description,
            },
            headers={"Authorization": f"Token {self.token}"},
        )
        logger.info(url)
        logger.info(f"POST {url} {r.status_code}")

    def get_spoken_speech(self):
        """
        Call the API to get the speech to play
        Returns:

        """
        url = f"{self.domain}/hardware/speech/?home_id={self.home_id}"
        logger.info(url)
        r = requests.get(
            url, headers={"Authorization": f"Token {self.token}"}, timeout=30
        )

        logger.info(f"get {url} {r.status_code}")
        # logger.info(r.text)
        if r.status_code != 200:
            return []
        return r.json()

__init__(domain=API_DOMAIN, token='', home_id=None)

init the api Args: domain (str): the domain of the api token (str): the token of the api home_id (int): the home id

Source code in Client/Responder/api.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def __init__(
    self,
    domain: str = API_DOMAIN,
    token: str = "",
    home_id: int = None,
):
    """
    init the api
    Args:
        domain (str): the domain of the api
        token (str): the token of the api
        home_id (int): the home id
    """
    self.domain = domain
    self.token = token
    self.mac_address = get_mac_address()
    self.home_id = home_id

get_spoken_speech()

Call the API to get the speech to play Returns:

Source code in Client/Responder/api.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def get_spoken_speech(self):
    """
    Call the API to get the speech to play
    Returns:

    """
    url = f"{self.domain}/hardware/speech/?home_id={self.home_id}"
    logger.info(url)
    r = requests.get(
        url, headers={"Authorization": f"Token {self.token}"}, timeout=30
    )

    logger.info(f"get {url} {r.status_code}")
    # logger.info(r.text)
    if r.status_code != 200:
        return []
    return r.json()

register_device(device_name=None, device_type=None, description=None)

register the device Args: device_name (Optional[str]): the device name device_type (Optional[str]): the device type description (Optional[str]): the description of the device

Returns:

Source code in Client/Responder/api.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def register_device(
    self,
    device_name: Optional[str] = None,
    device_type: Optional[str] = None,
    description: Optional[str] = None,
):
    """
    register the device
    Args:
        device_name (Optional[str]): the device name
        device_type (Optional[str]): the device type
        description (Optional[str]): the description of the device

    Returns:

    """
    url = f"{self.domain}/hardware/register/"

    r = requests.post(
        url,
        data={
            "home": self.home_id,
            "mac_address": self.mac_address,
            "device_name": device_name,
            "device_type": device_type,
            "description": description,
        },
        headers={"Authorization": f"Token {self.token}"},
    )
    logger.info(url)
    logger.info(f"POST {url} {r.status_code}")