Skip to content

NukiWebAPI

Fetch all smartlocks and create Smartlock objects mapped by ID.

Source code in nukiwebapi/nuki_web_api.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def _fetch_smartlocks(self):
    """Fetch all smartlocks and create Smartlock objects mapped by ID."""
    response = self._request("GET", "/smartlock")
    smartlocks = {}
    if response.json():
        for item in response.json():
            smartlock_id = item.get("smartlockId")
            if not smartlock_id:
                continue  # skip invalid entries
            smartlock = SmartlockInstance(
                client=self,
                smartlock_id=smartlock_id,
                data=item
            )

            smartlocks[smartlock_id] = smartlock

    return smartlocks
Source code in nukiwebapi/nuki_web_api.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def _request(self, method: str, endpoint: str, **kwargs):
    url = f"{self.base_url}{endpoint}"
    headers = kwargs.pop("headers", {})
    headers["Authorization"] = f"Bearer {self.access_token}"
    headers["Accept"] = "application/json"

    response = requests.request(method, url, headers=headers, **kwargs)

    try:
        response.raise_for_status()
    except requests.HTTPError as e:
        # Try to parse detailMessage if present
        try:
            error_json = response.json()
            detail = error_json.get("detailMessage", response.text)
        except ValueError:
            detail = response.text

        # Raise a new error with detail included
        raise requests.HTTPError(
            f"{e} | Detail: {detail}",
            response=response
        ) from None

    return response