# Path: src/amcrest/config.py
# KEEPALIVE_COUNT = 5
# 
# KEEPALIVE_IDLE = 20
# 
# KEEPALIVE_INTERVAL = 10
# 
# MAX_RETRY_HTTP_CONNECTION = 3
# 
# TIMEOUT_HTTP_PROTOCOL = 6.05
# 
# Path: src/amcrest/exceptions.py
# class CommError(AmcrestError):
#     """A communication error occurred."""
# 
# class LoginError(AmcrestError):
#     """A login error occurred."""
# 
# Path: src/amcrest/utils.py
# def clean_url(url: str) -> str:
#     host = re.sub(r"^http[s]?://", "", url, flags=re.IGNORECASE)
#     host = re.sub(r"/$", "", host)
#     return host
# 
# def pretty(value: str, delimiter: str = "=") -> str:
#     """Format string key=value."""
#     return value.strip().rpartition(delimiter)[2]


import asyncio
import logging
import re
import socket
import threading
import httpx
import requests
from contextlib import asynccontextmanager
from typing import AsyncIterator, Optional, Tuple, Union
from requests.adapters import HTTPAdapter
from urllib3.connection import HTTPConnection
from .config import (
    KEEPALIVE_COUNT,
    KEEPALIVE_IDLE,
    KEEPALIVE_INTERVAL,
    MAX_RETRY_HTTP_CONNECTION,
    TIMEOUT_HTTP_PROTOCOL,
)
from .exceptions import CommError, LoginError
from .utils import clean_url, pretty
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# vim:sw=4:ts=4:et



_LOGGER = logging.getLogger(__name__)

_KEEPALIVE_OPTS = HTTPConnection.default_socket_options + [
    (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
]
# On some systems TCP_KEEP* are not defined in socket.
try:
    _KEEPALIVE_OPTS += [
        (socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, KEEPALIVE_IDLE),
        (socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, KEEPALIVE_INTERVAL),

# Path: src/amcrest/config.py
# KEEPALIVE_COUNT = 5
# 
# KEEPALIVE_IDLE = 20
# 
# KEEPALIVE_INTERVAL = 10
# 
# MAX_RETRY_HTTP_CONNECTION = 3
# 
# TIMEOUT_HTTP_PROTOCOL = 6.05
# 
# Path: src/amcrest/exceptions.py
# class CommError(AmcrestError):
#     """A communication error occurred."""
# 
# class LoginError(AmcrestError):
#     """A login error occurred."""
# 
# Path: src/amcrest/utils.py
# def clean_url(url: str) -> str:
#     host = re.sub(r"^http[s]?://", "", url, flags=re.IGNORECASE)
#     host = re.sub(r"/$", "", host)
#     return host
# 
# def pretty(value: str, delimiter: str = "=") -> str:
#     """Format string key=value."""
#     return value.strip().rpartition(delimiter)[2]


# Path: src/amcrest/http.py
import asyncio
import logging
import re
import socket
import threading
import httpx
import requests
from contextlib import asynccontextmanager
from typing import AsyncIterator, Optional, Tuple, Union
from requests.adapters import HTTPAdapter
from urllib3.connection import HTTPConnection
from .config import (
    KEEPALIVE_COUNT,
    KEEPALIVE_IDLE,
    KEEPALIVE_INTERVAL,
    MAX_RETRY_HTTP_CONNECTION,
    TIMEOUT_HTTP_PROTOCOL,
)
from .exceptions import CommError, LoginError
from .utils import clean_url, pretty

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# vim:sw=4:ts=4:et



_LOGGER = logging.getLogger(__name__)

_KEEPALIVE_OPTS = HTTPConnection.default_socket_options + [
    (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
]
# On some systems TCP_KEEP* are not defined in socket.
try:
    _KEEPALIVE_OPTS += [
        (socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, KEEPALIVE_IDLE),
        (socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, KEEPALIVE_INTERVAL),

        (socket.IPPROTO_TCP, socket.TCP_KEEPCNT, KEEPALIVE_COUNT),