Skip to content

Commit 1b7b0b1

Browse files
authored
Make SSLContext async friendly (#1448)
* Make SSLContext async friendly * remove extra enter
1 parent e9e3648 commit 1b7b0b1

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

pyoverkiz/client.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ async def refresh_listener(invocation: Mapping[str, Any]) -> None:
9898
# pylint: disable=too-many-instance-attributes, too-many-branches
9999

100100

101+
def _create_local_ssl_context() -> ssl.SSLContext:
102+
"""Create SSL context.
103+
104+
This method is not async-friendly and should be called from a thread
105+
because it will load certificates from disk and do other blocking I/O.
106+
"""
107+
return ssl.create_default_context(
108+
cafile=os.path.dirname(os.path.realpath(__file__)) + "/overkiz-root-ca-2048.crt"
109+
)
110+
111+
112+
# The default SSLContext objects are created at import time
113+
# since they do blocking I/O to load certificates from disk,
114+
# and imports should always be done before the event loop starts or in a thread.
115+
SSL_CONTEXT_LOCAL_API = _create_local_ssl_context()
116+
117+
101118
class OverkizClient:
102119
"""Interface class for the Overkiz API"""
103120

@@ -153,11 +170,7 @@ def __init__(
153170
if verify_ssl:
154171
# To avoid security issues while authentication to local API, we add the following authority to
155172
# our HTTPS client trust store: https://ca.overkiz.com/overkiz-root-ca-2048.crt
156-
self._ssl = ssl.create_default_context(
157-
cafile=os.path.dirname(os.path.realpath(__file__))
158-
+ "/overkiz-root-ca-2048.crt"
159-
)
160-
173+
self._ssl = SSL_CONTEXT_LOCAL_API
161174
else:
162175
self.api_type = APIType.CLOUD
163176

0 commit comments

Comments
 (0)