def _login(self):
        creds = Credentials()

        if not creds.are_filled_in():
            KodiWrapper.dialog_ok(KodiWrapper.get_localized_string(32014),
                                  KodiWrapper.get_localized_string(32015))
            KodiWrapper.open_settings()
            creds.reload()

        resp = self.session.post(
            "https://login.prd.telenet.be/openid/login.do",
            data=login_payload(creds.username, creds.password))

        last_response = resp.history[-1]

        try:
            self.auth_tries += 1

            if "Location" in last_response.headers:
                token = self.extract_auth_token(
                    last_response.headers.get('Location'))
                if not token:
                    raise NotAuthorizedException()
                PluginCache.set_data({"auth_token": token})
                return True
        except NotAuthorizedException:
            KodiWrapper.dialog_ok(KodiWrapper.get_localized_string(32006),
                                  KodiWrapper.get_localized_string(32007))

            if self.auth_tries < 2:
                KodiWrapper.open_settings()
                self._execute_required_steps()

        return False
 def _register_device(self):
     resp = self.session.post(
         BASE_URL + "/device/register",
         headers={"Content-Type": "application/json;charset=utf-8"},
         data=device_payload(),
         allow_redirects=False)
     try:
         json_data = resp.json()
         device_id = json_data.get('deviceRegistration').get('id')
         PluginCache.set_data({"device_id": device_id})
     except (ValueError, KeyError) as exc:
         _LOGGER.error(exc)
     return resp
    def _request_oauth_tokens(self):
        auth_token = PluginCache.get_by_key("auth_token")
        device_id = PluginCache.get_by_key("device_id")

        resp = self.session.post(BASE_URL + "/oauth/token",
                                 headers={
                                     "Content-Type":
                                     "application/json;charset=utf-8",
                                     "X-Yelo-DeviceId": device_id
                                 },
                                 data=oauth_payload(auth_token, CALLBACK_URL),
                                 allow_redirects=False)

        try:
            json_data = resp.json()
            oauth_data = json_data.get('OAuthTokens')

            if oauth_data and oauth_data.get('status').upper() == 'SUCCESS':
                PluginCache.set_data({"OAuthTokens": oauth_data})
        except (ValueError, KeyError) as exc:
            _LOGGER.error(exc)
    def _get_entitlements(self):
        if not PluginCache.get_by_key("entitlements"):
            try:
                customer_features = self._customer_features()

                entitlements = [
                    int(item["id"]) for item in customer_features["linked"]
                    ["customerFeatures"]["entitlements"]
                ]
                customer_id = customer_features["loginSession"]["user"][
                    "links"]["customerFeatures"]

                PluginCache.set_data({
                    "entitlements": {
                        "entitlementId": entitlements,
                        "customer_id": customer_id
                    }
                })
            except (ValueError, KeyError) as exc:
                _LOGGER.error(exc)

        return PluginCache.get_by_key("entitlements")