import hashlib
import logging

import httpx
from pydantic import BaseModel

from ..exceptions import PwnedConnectionErrorException
from ..http_base_client import HttpBaseClient
from ..redis import RedisClient

PWNED_URL = 'https://api.pwnedpasswords.com/range'

logger = logging.getLogger('iperon')


class Pwned(BaseModel):
    @classmethod
    async def check(cls, password: str) -> int:
        """Checks the success of password in the pwned service

        :param password: Password
        :type password: str

        :return: Returns how many times it appears in the data set.
        :rtype: int
        """

        hash_obj = hashlib.sha1(password.encode())  # skipcq: PTC-W1003
        hash_password = hash_obj.hexdigest().upper()

        async with RedisClient() as redis_client:
            if result := await redis_client.get(f'pwned-{hash_password}'):
                return int(result)

        async with HttpBaseClient(base_url=PWNED_URL) as client:
            response: httpx.Response = await client.get(url=f'/{hash_password[:5]}')

            result = 0
            if response.status_code == 404:  # pragma: no cover
                result = 0
            elif response.status_code != 200:  # pragma: no cover
                raise PwnedConnectionErrorException()
            else:
                for line in response.text.splitlines():
                    line_hash, _, times = line.partition(':')
                    if hash_password[5:] == line_hash:

                        async with RedisClient() as redis_client:
                            await redis_client.set(f'pwned-{hash_password}', times, 60*60*24*90)

                        result = int(times)
                        break

            return result  # pragma: no cover


check = Pwned.check

import hashlib
import logging

import httpx
from pydantic import BaseModel

from ..exceptions import PwnedConnectionErrorException
from ..http_base_client import HttpBaseClient
from ..redis import RedisClient

PWNED_URL = 'https://api.pwnedpasswords.com/range'

logger = logging.getLogger('iperon')


class Pwned(BaseModel):
    @classmethod
    async def check(cls, password: str) -> int:
        """Checks the success of password in the pwned service

        :param password: Password
        :type password: str

        :return: Returns how many times it appears in the data set.
        :rtype: int
        """

        hash_obj = hashlib.sha1(password.encode())  # skipcq: PTC-W1003
        hash_password = hash_obj.hexdigest().upper()

        async with RedisClient() as redis_client:
            if result := await redis_client.get(f'pwned-{hash_password}'):
                return int(result)

        async with HttpBaseClient(base_url=PWNED_URL) as client:
            response: httpx.Response = await client.get(url=f'/{hash_password[:5]}')

            result = 0
            if response.status_code == 404:  # pragma: no cover
                result = 0
            elif response.status_code != 200:  # pragma: no cover
                raise PwnedConnectionErrorException()
            else:
                for line in response.text.splitlines():
                    line_hash, _, times = line.partition(':')
                    if hash_password[5:] == line_hash:

                        async with RedisClient() as redis_client:
                            await redis_client.set(f'pwned-{hash_password}', times, 60*60*24*90)

                        result = int(times)
                        break

            return result  # pragma: no cover


check = Pwned.check

Checks the success of password in the pwned service          :param password: Password         :type password: str          :return: Returns how many times it appears in the data set.         :rtype: int # skipcq: PTC-W1003 # pragma: no cover # pragma: no cover # pragma: no cover