# 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

    def _generate_token(self) -> None:
        """Create authentation to use with requests."""
        cmd = "magicBox.cgi?action=getMachineName"
        _LOGGER.debug("%s Trying Basic Authentication", self)
        self._token = requests.auth.HTTPBasicAuth(self._user, self._password)
        try:
            try:
                resp = self._command(cmd).content.decode()
            except LoginError:
                _LOGGER.debug("%s Trying Digest Authentication", self)
                self._token = requests.auth.HTTPDigestAuth(
                    self._user, self._password
                )
                resp = self._command(cmd).content.decode()
        except CommError:
            self._token = None
            raise

        # check if user passed
        result = resp.lower()
        if "invalid" in result or "error" in result:
            _LOGGER.debug(
                "%s Result from camera: %s",
                self,
                resp.strip().replace("\r\n", ": "),
            )
            self._token = None
            raise LoginError("Invalid credentials")


# 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


    def _generate_token(self) -> None:
        """Create authentation to use with requests."""
        cmd = "magicBox.cgi?action=getMachineName"
        _LOGGER.debug("%s Trying Basic Authentication", self)
        self._token = requests.auth.HTTPBasicAuth(self._user, self._password)
        try:
            try:
                resp = self._command(cmd).content.decode()
            except LoginError:
                _LOGGER.debug("%s Trying Digest Authentication", self)
                self._token = requests.auth.HTTPDigestAuth(
                    self._user, self._password
                )
                resp = self._command(cmd).content.decode()
        except CommError:
            self._token = None
            raise

        # check if user passed
        result = resp.lower()
        if "invalid" in result or "error" in result:
            _LOGGER.debug(
                "%s Result from camera: %s",
                self,
                resp.strip().replace("\r\n", ": "),
            )
            self._token = None
            raise LoginError("Invalid credentials")


        self._name = pretty(resp.strip())