Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add local support for Somfy Developer Mode #441

Merged
merged 13 commits into from
Apr 11, 2022
Prev Previous commit
Next Next commit
Local fixesak
  • Loading branch information
iMicknl authored Apr 11, 2022
commit 21a255dd7235a69999efc075e539d724ad73acc2
31 changes: 12 additions & 19 deletions pyoverkiz/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import asyncio
import datetime
import os
import ssl
import urllib.parse
from json import JSONDecodeError
from types import TracebackType
Expand Down Expand Up @@ -94,8 +96,8 @@ class OverkizClient:
_refresh_token: str | None = None
_expires_in: datetime.datetime | None = None
_access_token: str | None = None
_local_access_token: str | None = None
_is_local: bool = False
_ssl_context: ssl.SSLContext | None = None

def __init__(
self,
Expand Down Expand Up @@ -127,13 +129,14 @@ def __init__(

if "/enduser-mobile-web/1/enduserAPI/" in server.endpoint:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to add a property named for instance. support_local. WDYT?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was in doubt about this one as well... In the end I would love to see something like this (#321), where we define the auth mechanism in the OverkizServer definition.

Eventually that would be even better, since we can than easily pick any authentication method, and we don't need an extra property just for local.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simplified it in cfa2abc, would that be ok for now? @tetienne

self._is_local = True
self._local_access_token = token
self._access_token = token

# TODO To avoid security issue, add the following authority to
# To avoid security issue, add the following authority to
# your HTTPS client trust store: https://ca.overkiz.com/overkiz-root-ca-2048.crt
# sslcontext = ssl.create_default_context(
# cafile="overkiz-root-ca-2048.crt"
# )
self._ssl_context = ssl.create_default_context(
cafile=os.path.dirname(os.path.realpath(__file__))
+ "/overkiz-root-ca-2048.crt"
)

async def __aenter__(self) -> OverkizClient:
return self
Expand Down Expand Up @@ -740,12 +743,10 @@ async def __get(self, path: str) -> Any:
if self._access_token:
headers["Authorization"] = f"Bearer {self._access_token}"

# TODO check what is the definitive header
if self._local_access_token:
headers["X-Auth-Token"] = f"Bearer {self._access_token}"

async with self.session.get(
f"{self.server.endpoint}{path}", headers=headers
f"{self.server.endpoint}{path}",
headers=headers,
ssl_context=self._ssl_context,
) as response:
await self.check_response(response)
return await response.json()
Expand All @@ -760,10 +761,6 @@ async def __post(
await self._refresh_token_if_expired()
headers["Authorization"] = f"Bearer {self._access_token}"

# TODO check what is the definitive header
if self._local_access_token:
headers["X-Auth-Token"] = f"Bearer {self._access_token}"

async with self.session.post(
f"{self.server.endpoint}{path}", data=data, json=payload, headers=headers
) as response:
Expand All @@ -779,10 +776,6 @@ async def __delete(self, path: str) -> None:
if self._access_token:
headers["Authorization"] = f"Bearer {self._access_token}"

# TODO check what is the definitive header
if self._local_access_token:
headers["X-Auth-Token"] = f"Bearer {self._access_token}"

async with self.session.delete(
f"{self.server.endpoint}{path}", headers=headers
) as response:
Expand Down