
    d?i.4                         S r SSKJrJr  SSKrSSKrSSKrSSKrSSKJ	r	  SSK
Jr  SSKJr  SSKJrJr  SS	KJrJrJrJrJrJrJr  SS
KJr  SSKJr  SSKJrJrJrJ r J!r!   " S S5      r"g)zQ
Main API client asynchronous handler for fetching data from the IPinfo service.
    )IPv4AddressIPv6AddressN   )APIError)DefaultCache)Details)RequestQuotaExceededErrorTimeoutExceededError)API_URLBATCH_MAX_SIZECACHE_MAXSIZE	CACHE_TTLREQUEST_TIMEOUT_DEFAULTBATCH_REQ_TIMEOUT_DEFAULT	cache_key)handler_utils)is_bogon)
continents	countriescountries_currencieseu_countriescountries_flagsc                   d    \ rS rSrSrSS jrS rS rSS jrS\	SS4S	 jr
S
 rS r  SS jrSrg)AsyncHandler$   zt
Allows client to request data for specified IP address asynchronously.
Instantiates and maintains access to cache.
Nc                    Xl         UR                  S5      =(       d    [        U l        UR                  S5      =(       d    [        U l        UR                  S5      =(       d    [        U l        UR                  S5      =(       d    [
        U l        UR                  S5      =(       d    [        U l        UR                  S0 5      U l        SU R                  ;  a  [        U R                  S'   SU l	        S	U;   a  US	   U l
        O@UR                  S
0 5      nSU;  a	  [        US'   SU;  a	  [        US'   [        S0 UD6U l
        UR                  SS5      U l        g)zQ
Initialize the Handler object with country name list and the
cache initialized.
r   r   r   r   	continentrequest_optionstimeoutNcachecache_optionsmaxsizettlheaders )access_tokengetr   r   r   r   r   r   r   httpsessr    r   r   r   r$   )selfr&   kwargsr!   s       X/var/www/html/livechat/backend/venv/lib/python3.13/site-packages/ipinfo/handler_async.py__init__AsyncHandler.__init__*   s/   
 )  K0=I #JJ~6F,  &zz*;<O JJ-.F2F 	!
 !**[1?Z  &zz*;R@D000.ED  +  fDJ"JJ;M-+8i(M)'0e$%66DJ zz)T2    c                 @   #    U R                  5       I Sh  vN   g N7f)z
Initializes internal aiohttp connection pool.

This isn't _required_, as the pool is initialized lazily when needed.
But in case you require non-lazy initialization, you may await this.

This is idempotent.
N)_ensure_aiohttp_readyr)   s    r+   initAsyncHandler.initX   s      ((***s   c                    #    U R                   (       a*  U R                   R                  5       I Sh  vN   SU l         gg N7f)z
Deinitialize the async handler.

This is required in case you need to let go of the memory/state
associated with the async handler in a long-running process.

This is idempotent.
N)r(   closer1   s    r+   deinitAsyncHandler.deinitc   s3      ==--%%''' DM 's   /A?Ac                   #    U R                  5         [        U[        5      (       d  [        U[        5      (       a  UR                  nU(       a   [        U5      (       a  USS.n[        U5      $  U R                  [        U5         n[        U5      $ ! [         a     Of = f[        nU(       a  USU-   -  n[        R                  " U R                  U R                  5      n0 nUb  X'S'   U R                  R                   " U4SU0UD6 ISh  vN  nUR"                  S:X  a
  [%        5       eUR"                  S:  ad  UR"                  n	UR                  R!                  S	5      n
U
S
:X  a  UR'                  5       I Sh  vN  nOSUR)                  5       0n[+        X5      eUR'                  5       I Sh  vN  nSSS5      ISh  vN    O! , ISh  vN  (       d  f       O= f[        R,                  " WU R.                  U R0                  U R2                  U R4                  U R6                  5        X0R                  [        U5      '   [        U5      $ 7f)z9Get details for specified IP address as a Details object.Tipbogon/Nr   r$     i  zContent-Typeapplication/jsonerror)r0   
isinstancer   r   explodedr   r   r    r   KeyErrorr   r   get_headersr&   r$   r(   r'   statusr	   jsontextr   format_detailsr   r   r   r   r   )r)   
ip_addressr   detailscached_ipaddrurlr$   req_optsresp
error_codecontent_typeerror_responses               r+   
getDetailsAsyncHandler.getDetailsp   s    ""$
 j+..*3
 3
 $,,J (:..'$7G7##	 JJy'<=M=)) 		 3##C++D,=,=t||L")Y==$$SF'FXFF${{c!/11{{c!![[
#||//?#55+/99;%6%6N&-tyy{%;Nz:: IIK''G GFFFFF 	$$NN  %%OO	
 -4

9Z()ws   A.I1"B I
B!I B!!A/IDIA+G F6G7F:8G=IG	IG&GG&"A6ITc                   #    U R                  5         Uc  [        n0 n/ nU HV  n[        U[        5      (       d  [        U[        5      (       a  UR
                  n U R                  [        U5         n	XU'   MX     U(       d  U$ Ub  [        R                  " 5       n
[        S-   n[        R                  " U R                  U R                  5      nSUS'   [!        S[#        U5      U5       Vs/ s H  nU R%                  X}X-    UUUUU5      PM     nn [&        R(                  " 1 UkU[&        R*                  S9I Sh  vN u  nnU(       d  U$ U H  n UR-                  5         UI Sh  vN   M      [        R0                  " U[3        5       U5      $ ! [         a    UR                  U5         GM  f = fs  snf  N| NR! [&        R.                   a     M  f = f! [4         a"  n[        R0                  " UUU5      s SnA$ SnAff = f7f)a  
Get details for a batch of IP addresses at once.

There is no specified limit to the number of IPs this function can
accept; it can handle as much as the user can fit in RAM (along with
all of the response data, which is at least a magnitude larger than the
input list).

The input list is broken up into batches to abide by API requirements.
The batch size can be adjusted with `batch_size` but is clipped to
`BATCH_MAX_SIZE`.
Defaults to `BATCH_MAX_SIZE`.

For each batch, `timeout_per_batch` indicates the maximum seconds to
spend waiting for the HTTP request to complete. If any batch fails with
this timeout, the whole operation fails.
Defaults to `BATCH_REQ_TIMEOUT_DEFAULT` seconds.

`timeout_total` is a seconds-denominated hard-timeout for the time
spent in HTTP operations; regardless of whether all batches have
succeeded so far, if `timeout_total` is reached, the whole operation
will fail by raising `TimeoutExceededError`.
Defaults to being turned off.

`raise_on_fail`, if turned off, will return any result retrieved so far
rather than raise an exception when errors occur, including timeout and
quota errors.
Defaults to on.

The concurrency level is currently unadjustable; coroutines will be
created and consumed for all batches at once.
N/batchr>   content-typer   )r   return_when)r0   r   r@   r   r   rA   r    r   rB   appendtimer   r   rC   r&   r$   rangelen_do_batch_reqasynciowaitFIRST_EXCEPTIONcancelCancelledErrorreturn_or_failr
   	Exception)r)   ip_addresses
batch_sizetimeout_per_batchtimeout_totalraise_on_failresultlookup_addressesrH   rJ   
start_timerK   r$   ireqs_pendingcoes                      r+   getBatchDetailsAsyncHandler.getBatchDetails   s    P 	""$'J &J *k22jK7 7 (00
4 $

9Z+@ A%2z" '   M $J  ++D,=,=t||L"4 1c"23Z@

 A  Q^4! A 	 

	J&||$%#33  JAw  IIKHH  !//35v c  4 ''
334"

 --   	J //q&II	Js   AH	F8A6H	.$F6H	)G >F;?G H	G F?*F=+F?/#G H	F3.H	2F33H	;G =F??GG GG 
H$H;H<H	HH	c           	        #     U R                   R                  U[        R                  " U5      UUS9I Sh  vN n UR                  S:X  a
  [        5       eUR                  5         UR                  5       I Sh  vN n	U	R                  5        H  u  p[        U[        5      (       d  M  [
        R                  " UU R                  U R                  U R                   U R"                  U R$                  5        XR&                  [)        U
5      '   M     UR+                  U	5        g N! [         a!  n[
        R                  " XXS5      s SnA$ SnAff = f! [         a!  n[
        R                  " XXS5      s SnA$ SnAff = f GN	7f)zF
Coroutine which will do the actual POST request for getBatchDetails.
)datar$   r   Nr=   )r(   postrE   dumpsrb   r   ra   rD   r	   raise_for_statusitemsr@   dictrG   r   r   r   r   r   r    r   update)r)   chunkrK   r$   re   rg   rh   rM   rp   	json_resprH   rI   s               r+   r[   AsyncHandler._do_batch_req!  sK    	H++ZZ&)	 ,  D	H{{c!/11!!# ))+%	 $-??#4J'4((,,NN%%((--OO 5<

9Z01 $5 	i C  	H //$GG	H  	H //$GG	H &s   F3D' D%D' *E 'F:F;,F+A:F%D' '
E1EEFEF
F E;5F 6F;F  Fc                     U R                   (       a  g[        R                  " U R                  S   S9n[        R                  " US9U l         g)z.Ensures aiohttp internal state is initialized.Nr   )total)r   )r(   aiohttpClientTimeoutr   ClientSession)r)   r   s     r+   r0   "AsyncHandler._ensure_aiohttp_readyK  s9    ==''d.B.B9.MN--g>r.   c           
       ^ ^^^#    Uc  [         n0 m/ nU H  n[        U[        5      (       d  [        U[        5      (       a  UR                  nU(       a%  [        U5      (       a  USS.n[        U5      7v   Me   T R                  [        U5         nUTU'   M     U(       d  TR                  5       7v   [        S-   m[        R                  " T R                  T R                   5      mSTS'   UUU U4S jn[#        S[%        U5      U5       H  n	XIX-    n
U" U
5      I S h  vN   TR                  5        Hm  u  pV[        U[&        5      (       aM  [        R(                  " UT R*                  T R,                  T R.                  T R0                  T R2                  5        XV47v   Mo     M     g ! [         a    UR                  U5         GM  f = f N7f)NTr9   rT   r>   rU   c                   >#    [         R                  " TS9 IS h  vN nUR                  T	U S9I S h  vN nUR                  5         UR	                  5       I S h  vN nUR                  5        H"  u  pEUTR                  [        U5      '   UTU'   M$     S S S 5      IS h  vN   g  N Nu NO N! , IS h  vN  (       d  f       g = f7f)N)r$   )rE   )r   r   ru   rw   rE   rx   r    r   )
batchsessionresponsejson_responserH   rI   r$   resultsr)   rK   s
         r+   process_batch7AsyncHandler.getBatchDetailsIter.<locals>.process_batchu  s     ,,W==!(c!>>))+&.mmo 5+8+>+>+@'J8?DJJy45*1GJ' ,A	 >==> 5 >===sf   C
B(C
B0B*'B0B,:B0C
"B.#C
*B0,B0.C
0C6B97CC
r   )r   r@   r   r   rA   r   r   r    r   rB   rW   rx   r   r   rC   r&   r$   rY   rZ   ry   rG   r   r   r   r   r   )r)   rc   rd   rg   ri   rH   rI   rJ   r   rk   r   r$   r   rK   s   `          @@@r+   getBatchDetailsIter AsyncHandler.getBatchDetailsIterS  s     'J&J*k22jK7 7 (00
hz22!+d;g&&8$(JJy/D$EM*7GJ' '   --/! ++D,=,=t||L"4	2 	2 q#./<A$8E&&&'.}}#
gt,,!00)),,11 !)) (7	 ='   8$++J778* 's8   A5G<F.B
G#G$B
G.G	GGG)
r&   r    r   r   r   r   r   r$   r(   r   )N)NN)NT)__name__
__module____qualname____firstlineno____doc__r,   r2   r6   rQ   r   rq   r[   r0   r   __static_attributes__r%   r.   r+   r   r   $   sJ    
,3\	+!8 z 3un(!T? 	9*r.   r   )#r   	ipaddressr   r   r\   rE   rX   r   r?   r   cache.defaultr   rI   r   
exceptionsr	   r
   r   r   r   r   r   r   r   r    r;   r   rt   r   r   r   r   r   r   r%   r.   r+   <module>r      sT    /      '  G     h* h*r.   