Skip to content

Commit e16f3d6

Browse files
committed
Update graphql.py
Add ssl context to prevent blocking call
1 parent 41bc675 commit e16f3d6

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

pyhilo/graphql.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import asyncio
22
import hashlib
33
import json
4+
import ssl
45
from typing import Any, Callable, Dict, List, Optional
56

7+
import certifi
68
import httpx
79
from httpx_sse import aconnect_sse
810

@@ -19,12 +21,16 @@ def __init__(self, api: API, devices: Devices):
1921
self._api = api
2022
self._devices = devices
2123
self.mapper: GraphqlValueMapper = GraphqlValueMapper()
22-
24+
self.ssl_context = None
2325
self.subscriptions: List[Optional[asyncio.Task]] = [None]
2426

2527
async def async_init(self) -> None:
2628
"""Initialize the Hilo "GraphQlHelper" class."""
2729
await self.call_get_location_query(self._devices.location_hilo_id)
30+
loop = asyncio.get_running_loop()
31+
self._ssl_context = await loop.run_in_executor(
32+
None, lambda: ssl.create_default_context(cafile=certifi.where())
33+
)
2834

2935
QUERY_GET_LOCATION: str = """query getLocation($locationHiloId: String!) {
3036
getLocation(id:$locationHiloId) {
@@ -548,7 +554,7 @@ async def call_get_location_query(self, location_hilo_id: str) -> None:
548554
"variables": {"locationHiloId": location_hilo_id},
549555
}
550556

551-
async with httpx.AsyncClient(http2=True) as client:
557+
async with httpx.AsyncClient(http2=True, verify=self.ssl_context) as client:
552558
try:
553559
response = await client.post(url, json=payload, headers=headers)
554560
response.raise_for_status()
@@ -634,7 +640,9 @@ async def _listen_to_sse(
634640

635641
retry_with_full_query = False
636642

637-
async with httpx.AsyncClient(http2=True, timeout=None) as client:
643+
async with httpx.AsyncClient(
644+
http2=True, timeout=None, verify=self.ssl_context
645+
) as client:
638646
async with aconnect_sse(
639647
client, "POST", url, json=payload, headers=headers
640648
) as event_source:

0 commit comments

Comments
 (0)