diff --git a/LidlConnect/api/credit.py b/LidlConnect/api/credit.py new file mode 100644 index 0000000..3c37755 --- /dev/null +++ b/LidlConnect/api/credit.py @@ -0,0 +1,58 @@ +"""Credit-related API functionality for Lidl Connect.""" + +class CreditMixin: + """Credit methods for Lidl Connect API.""" + + CREDIT_URL = "https://selfcare.lidl-connect.at/credit/code" + + def credit_topup(self, code: str) -> bool: + """ + Top up credit using a voucher code. + + Args: + code: The voucher code to redeem + + Returns: + True if the top up was successful, False otherwise. + """ + if not self.logged_in or not self.csrf_token: + raise ValueError("Not logged in or missing CSRF token") + + headers = { + "Accept": "application/json", + "Content-Type": "application/json;charset=UTF-8", + "X-Requested-With": "XMLHttpRequest", + "Origin": "https://selfcare.lidl-connect.at", + "Referer": self.DASHBOARD_URL, + "X-CSRF-TOKEN": self.csrf_token, + "X-SELF-CARE": "1", + } + + phone_number = self.phone_number + if not phone_number: + raise ValueError("Phone number not available") + + payload = { + "phoneNumber": phone_number, + "code": { + "label": "credit_top_up.bon", + "code_fields": 1, + "code_field_length": 16, + "code_validation": "^([a-zA-Z0-9]+)$", + "active": True, + "api_type": "VOUCHER_WITH_ACTIVATION_NUMBER", + "code": code + }, + "userId": self.user_id, + "endpointId": self.endpoint_id + } + + r = self.session.post(self.CREDIT_URL, headers=headers, json=payload) + + if r.status_code != 200: + return False + + if hasattr(self._get_user_data, 'cache'): + self._get_user_data.cache.clear() + + return True \ No newline at end of file diff --git a/LidlConnect/client.py b/LidlConnect/client.py index cea6d03..6bc566a 100644 --- a/LidlConnect/client.py +++ b/LidlConnect/client.py @@ -11,8 +11,9 @@ from .api.utils import ApiMixin from .api.user import UserDataMixin from .api.tariffs import TariffsMixin from .api.invoices import InvoicesMixin +from .api.credit import CreditMixin -class LidlConnect(AuthMixin, ExtractorMixin, UsageMixin, ApiMixin, UserDataMixin, TariffsMixin, InvoicesMixin): +class LidlConnect(AuthMixin, ExtractorMixin, UsageMixin, ApiMixin, UserDataMixin, TariffsMixin, InvoicesMixin, CreditMixin): """Client for interacting with Lidl Connect Self-Care portal.""" DASHBOARD_URL = "https://selfcare.lidl-connect.at/customer/dashboard/" diff --git a/LidlConnect/helpers.py b/LidlConnect/helpers.py index e0aa745..3cfe34a 100644 --- a/LidlConnect/helpers.py +++ b/LidlConnect/helpers.py @@ -30,5 +30,6 @@ def ttl_cache(ttl_seconds=30): cache[key] = (result, now) return result + wrapper.cache = cache return wrapper return decorator \ No newline at end of file