Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(Unofficial) async Python client to interact with Sagemcom F@st routers via internal API's. This client offers helper functions to retrieve common used functions, but also offers functionality to do custom requests via XPATH notation.

Python 3.9+ required.
Python 3.11+ required.

## Features

Expand Down
64 changes: 27 additions & 37 deletions sagemcom_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,34 +381,14 @@ async def get_encryption_method(self):

return None

@backoff.on_exception(
backoff.expo,
(
AuthenticationException,
LoginRetryErrorException,
LoginTimeoutException,
InvalidSessionException,
),
max_tries=1,
on_backoff=retry_login,
)
Comment thread
iMicknl marked this conversation as resolved.
async def get_value_by_xpath(self, xpath: str, options: dict | None = None) -> dict:
"""Retrieve raw value from router using XPath.

:param xpath: path expression
:param options: optional options
"""
actions = {
"id": 0,
"method": "getValue",
"xpath": urllib.parse.quote(xpath, "/=[]'"),
"options": options if options else {},
}

response = await self.__api_request_async([actions], False)
data = self.__get_response_value(response)

return data
result = await self.get_values_by_xpaths({"value": xpath}, options)
return result["value"]
Comment thread
WASDi marked this conversation as resolved.

@backoff.on_exception(
backoff.expo,
Expand All @@ -421,7 +401,7 @@ async def get_value_by_xpath(self, xpath: str, options: dict | None = None) -> d
max_tries=1,
on_backoff=retry_login,
)
async def get_values_by_xpaths(self, xpaths, options: dict | None = None) -> dict:
async def get_values_by_xpaths(self, xpaths: dict[str, str], options: dict | None = None) -> dict:
"""Retrieve raw values from router using XPath.

:param xpaths: Dict of key to xpath expression
Expand All @@ -431,7 +411,7 @@ async def get_values_by_xpaths(self, xpaths, options: dict | None = None) -> dic
{
"id": i,
"method": "getValue",
"xpath": urllib.parse.quote(xpath, "/=[]'"),
"xpath": urllib.parse.quote(xpath, "/=[]'@\""),
"options": options if options else {},
Comment thread
WASDi marked this conversation as resolved.
}
for i, xpath in enumerate(xpaths.values())
Expand All @@ -443,6 +423,15 @@ async def get_values_by_xpaths(self, xpaths, options: dict | None = None) -> dic

return data

async def set_value_by_xpath(self, xpath: str, value: str, options: dict | None = None) -> dict:
"""Set value using XPath.

:param xpath: path expression
:param value: value
:param options: optional options
"""
return await self.set_values_by_xpaths({xpath: value}, options)

@backoff.on_exception(
backoff.expo,
(
Expand All @@ -454,23 +443,24 @@ async def get_values_by_xpaths(self, xpaths, options: dict | None = None) -> dic
max_tries=1,
on_backoff=retry_login,
)
async def set_value_by_xpath(self, xpath: str, value: str, options: dict | None = None) -> dict:
"""Retrieve raw value from router using XPath.
Comment thread
WASDi marked this conversation as resolved.
async def set_values_by_xpaths(self, xpaths: dict[str, str], options: dict | None = None) -> dict:
"""Set multiple values using XPath.

:param xpath: path expression
:param value: value
:param xpaths: Dict of xpath expression to value
:param options: optional options
Comment thread
WASDi marked this conversation as resolved.
"""
actions = {
"id": 0,
"method": "setValue",
"xpath": urllib.parse.quote(xpath, "/=[]'"),
"parameters": {"value": str(value)},
"options": options if options else {},
}

response = await self.__api_request_async([actions], False)
actions = [
{
"id": i,
"method": "setValue",
"xpath": urllib.parse.quote(xpath, "/=[]'@\""),
Comment thread
WASDi marked this conversation as resolved.
"parameters": {"value": str(value)},
"options": options if options else {},
}
for i, (xpath, value) in enumerate(xpaths.items())
]

response = await self.__api_request_async(actions, False)
return response

@backoff.on_exception(
Expand Down
Loading