Skip to content

Commit

Permalink
Add Unknown Object exception
Browse files Browse the repository at this point in the history
  • Loading branch information
iMicknl authored Apr 11, 2022
1 parent 88f9769 commit 9031763
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
31 changes: 26 additions & 5 deletions pyoverkiz/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
TooManyExecutionsException,
TooManyRequestsException,
UnknownUserException,
UnknownObjectException,
)
from pyoverkiz.models import (
Command,
Expand Down Expand Up @@ -137,6 +138,7 @@ def __init__(
cafile=os.path.dirname(os.path.realpath(__file__))
+ "/overkiz-root-ca-2048.crt"
)
self._ssl_context = None

async def __aenter__(self) -> OverkizClient:
return self
Expand Down Expand Up @@ -165,10 +167,13 @@ async def login(
Caller must provide one of [userId+userPassword, userId+ssoToken, accessToken, jwt]
"""
# Local authentication
# TODO check which endpoint can be used to validate the token!
if self._is_local:
if register_event_listener:
await self.register_event_listener()
else:
# Call a simple endpoint to verify if our token is correct
await self.get_api_version()

return True

# Somfy TaHoma authentication using access_token
Expand Down Expand Up @@ -594,6 +599,15 @@ async def get_current_executions(self) -> list[Execution]:

return executions

@backoff.on_exception(
backoff.expo, NotAuthenticatedException, max_tries=2, on_backoff=relogin
)
async def get_api_version(self) -> str:
"""Get the API version (local only)"""
response = await self.__get("apiVersion")

return cast(str, response["protocolVersion"])

@backoff.on_exception(backoff.expo, TooManyExecutionsException, max_tries=10)
@backoff.on_exception(
backoff.expo, NotAuthenticatedException, max_tries=2, on_backoff=relogin
Expand Down Expand Up @@ -670,7 +684,7 @@ async def generate_local_token(self, gateway_id: str) -> str:
Generates a new token
Access scope : Full enduser API access (enduser/*)
"""
response = await self.__get(f"/config/{gateway_id}/local/tokens/generate")
response = await self.__get(f"config/{gateway_id}/local/tokens/generate")

return cast(str, response["token"])

Expand All @@ -688,7 +702,7 @@ async def activate_local_token(
Access scope : Full enduser API access (enduser/*)
"""
response = await self.__post(
f"/config/{gateway_id}/local/tokens",
f"config/{gateway_id}/local/tokens",
{"label": label, "token": token, "scope": scope},
)

Expand All @@ -707,7 +721,7 @@ async def get_local_tokens(
Get all gateway tokens with the given scope
Access scope : Full enduser API access (enduser/*)
"""
response = await self.__get(f"/config/{gateway_id}/local/tokens/{scope}")
response = await self.__get(f"config/{gateway_id}/local/tokens/{scope}")
local_tokens = [LocalToken(**lt) for lt in humps.decamelize(response)]

return local_tokens
Expand All @@ -723,7 +737,7 @@ async def delete_local_token(self, gateway_id: str, uuid: str) -> bool:
Delete a token
Access scope : Full enduser API access (enduser/*)
"""
await self.__delete(f"/config/{gateway_id}/local/tokens/{uuid}")
await self.__delete(f"config/{gateway_id}/local/tokens/{uuid}")

return True

Expand Down Expand Up @@ -853,8 +867,15 @@ async def check_response(response: ClientResponse) -> None:
if "Not such token with UUID: " in message:
raise NotSuchTokenException(message)


<< << << < HEAD
if "Unknown user :" in message:
raise UnknownUserException(message)
== == == =
# {"error":"Unknown object.","errorCode":"UNSPECIFIED_ERROR"}
if message == "Unknown object.":
raise UnknownObjectException(message)
>>>>>> > 562d327(Add Unknown Object exception)

raise Exception(message if message else result)

Expand Down
4 changes: 4 additions & 0 deletions pyoverkiz/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class UnknownUserException(BaseOverkizException):
pass


class UnknownObjectException(BaseOverkizException):
pass


# Nexity
class NexityBadCredentialsException(BadCredentialsException):
pass
Expand Down

0 comments on commit 9031763

Please sign in to comment.