Skip to content

Commit 2e3640c

Browse files
authored
Merge pull request #388 from dvd-dev/httpx
Fix blocking call création de context SSL
2 parents 41bc675 + 3b3073f commit 2e3640c

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

pyhilo/graphql.py

Lines changed: 17 additions & 2 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

@@ -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:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ python-dateutil = ">=2.8.2"
7474
python = "^3.9.0"
7575
voluptuous = ">=0.13.1"
7676
pyyaml = "^6.0.2"
77-
httpx = {version = ">=0.20.0", extras = ["http2"]}
77+
httpx = {version = ">=0.27.0", extras = ["http2"]}
7878
httpx-sse = ">=0.4.0"
7979

8080
[poetry.group.dev.dependencies]

0 commit comments

Comments
 (0)