11import asyncio
22import hashlib
33import json
4+ import ssl
45from typing import Any , Callable , Dict , List , Optional
56
7+ import certifi
68import httpx
79from httpx_sse import aconnect_sse
810
@@ -24,6 +26,17 @@ def __init__(self, api: API, devices: Devices):
2426
2527 async def async_init (self ) -> None :
2628 """Initialize the Hilo "GraphQlHelper" class."""
29+ loop = asyncio .get_running_loop ()
30+
31+ def _create_ssl_context () -> ssl .SSLContext :
32+ ctx = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
33+ ctx .check_hostname = True
34+ ctx .verify_mode = ssl .CERT_REQUIRED
35+ ctx .load_verify_locations (certifi .where ())
36+ return ctx
37+
38+ self ._ssl_context = await loop .run_in_executor (None , _create_ssl_context )
39+
2740 await self .call_get_location_query (self ._devices .location_hilo_id )
2841
2942 QUERY_GET_LOCATION : str = """query getLocation($locationHiloId: String!) {
@@ -548,7 +561,7 @@ async def call_get_location_query(self, location_hilo_id: str) -> None:
548561 "variables" : {"locationHiloId" : location_hilo_id },
549562 }
550563
551- async with httpx .AsyncClient (http2 = True ) as client :
564+ async with httpx .AsyncClient (http2 = True , verify = self . _ssl_context ) as client :
552565 try :
553566 response = await client .post (url , json = payload , headers = headers )
554567 response .raise_for_status ()
@@ -634,7 +647,9 @@ async def _listen_to_sse(
634647
635648 retry_with_full_query = False
636649
637- async with httpx .AsyncClient (http2 = True , timeout = None ) as client :
650+ async with httpx .AsyncClient (
651+ http2 = True , timeout = None , verify = self ._ssl_context
652+ ) as client :
638653 async with aconnect_sse (
639654 client , "POST" , url , json = payload , headers = headers
640655 ) as event_source :
0 commit comments