Skip to content

Commit

Permalink
Try fix SSLV3 error
Browse files Browse the repository at this point in the history
  • Loading branch information
GuyKh authored Sep 26, 2023
1 parent 27c1ac2 commit b8f8754
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions ims_envista/ims_envista.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from requests.adapters import HTTPAdapter
from requests.exceptions import HTTPError
from loguru import logger
from urllib3 import Retry

from .const import (
GET_LATEST_STATION_DATA_URL,
Expand Down Expand Up @@ -39,13 +38,10 @@
# and wait for timeout before trying ipv4, so we have to disable ipv6
requests.packages.urllib3.util.connection.HAS_IPV6 = False

def create_session():
session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
# https://github.com/home-assistant/core/issues/92500#issuecomment-1636743499
ssl_ctx = requests.packages.urllib3.util.ssl_.create_urllib3_context()
ssl_ctx.load_default_certs()
ssl_ctx.options |= 0x4


class IMSEnvista:
Expand All @@ -56,7 +52,6 @@ def __init__(self, token: str):
raise ValueError

self.token = token
self.session = create_session()

@staticmethod
def _get_channel_id_url_part(channel_id: int) -> str:
Expand All @@ -76,14 +71,11 @@ def _get_ims_url(self, url: str) -> Optional[dict]:
"""
logger.debug(f"Fetching data from: {url}")
try:
response = self.session.get(
url,
headers={
with requests.packages.urllib3.PoolManager(ssl_context=ssl_ctx) as http:
response = http.request("GET", url, headers={
"Accept": "application/vnd.github.v3.text-match+json",
"Authorization": f"ApiToken {self.token}",
},
timeout=10,
)
"Authorization": f"ApiToken {self.token}"
}, retries=requests.packages.urllib3.Retry(3), timeout=10)

# If the response was successful, no Exception will be raised
response.raise_for_status()
Expand All @@ -95,10 +87,6 @@ def _get_ims_url(self, url: str) -> Optional[dict]:
logger.error(f"Other error occurred: {err}") # Python 3.6
return None

def close(self):
""" Close Requests Session """
self.session.close()

def get_latest_station_data(
self, station_id: int, channel_id: int = None
) -> StationMeteorologicalReadings:
Expand Down

0 comments on commit b8f8754

Please sign in to comment.