
    !\i                     p    d Z ddlZddlZddlmZ ddlmZ ddlZ ej                  e	      Z
 G d d      Zy)zFlashscore HTTP client    N)Optional)settingsc                   R    e Zd ZdZd Zd Zd
dededee	j                     fdZd Zy	)FlashscoreClientz-HTTP client for Flashscore with rate limitingc                     t         j                  | _        t         j                  | _        t         j
                  | _        d| _        t        j                  dd| j                  id      | _
        y )Ng        g      >@z
User-AgentT)timeoutheadersfollow_redirects)r   flashscore_base_urlbase_urlflashscore_rate_limit_secondsrate_limit_secondsflashscore_user_agent
user_agentlast_request_timehttpxClientclientselfs    */app/app/data_sources/flashscore/client.py__init__zFlashscoreClient.__init__   sT     44"*"H"H"88!$ll!4??3!
    c                     t        j                          | j                  z
  }|| j                  k  r$| j                  |z
  }t        j                  |       t        j                          | _        y)zApply rate limitingN)timer   r   sleep)r   elapsed
sleep_times      r   _rate_limitzFlashscoreClient._rate_limit   sP    ))+ 6 66T,,,007:JJJz"!%r   urlretriesreturnc           
         |j                  d      r|n| j                   | }t        |      D ]A  }	 | j                          | j                  j                  |      }|j                          |c S  y	# t        j                  $ rm}t        j                  d|dz    d| d|        ||dz
  k(  r"t        j                  d| d| d       Y d	}~ y	t        j                  d
|z         Y d	}~d	}~ww xY w)z*GET request with retries and rate limitinghttpzRequest failed (attempt    /z): zFailed to fetch z after z	 attemptsN   )
startswithr   ranger   r   getraise_for_statusr   	HTTPErrorloggerwarningerrorr   r   )r   r    r!   full_urlattemptresponsees          r   r*   zFlashscoreClient.get!   s    ..03u6MW~G
)  ";;??84))+ &  ?? )!9'A+ayPSTUSVWXgk)LL#3H:WWIY!WX

1<(()s   <A44C4AC/C//C4c                 8    | j                   j                          y)zClose the clientN)r   closer   s    r   r5   zFlashscoreClient.close4   s    r   N)   )__name__
__module____qualname____doc__r   r   strintr   r   Responser*   r5    r   r   r   r      s7    7	
-s S %..1I &r   r   )r:   r   r   typingr   app.core.configr   logging	getLoggerr7   r-   r   r>   r   r   <module>rC      s3        $ 			8	$+ +r   