
    d?i1                         S r SSKJrJ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)zD
Main API client 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                   R    \ rS rSrSrSS jrSS jrS\SS4S jrS r	  SS	 jr
S
rg)Handler"   ze
Allows client to request data for specified IP address.
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;   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timeoutcachecache_optionsmaxsizettlheadersN )access_tokengetr   r   r   r   r   r   r   r    r   r   r   r$   )selfr&   kwargsr!   s       R/var/www/html/livechat/backend/venv/lib/python3.13/site-packages/ipinfo/handler.py__init__Handler.__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[        5      (       d  [        U[        5      (       a  UR                  nU(       a&  [	        U5      (       a  0 nXS'   SUS'   [        U5      $  U R                  [        U5         n[        U5      $ ! [         a     Of = f0 U R                  EnUb  X%S'   [        nU(       a  USU-   -  n[        R                  " U R                  U R                  5      n[        R                   " U4SU0UD6nUR"                  S:X  a
  [%        5       eUR"                  S:  aW  UR"                  n	UR                  R!                  S	5      n
U
S
:X  a  UR'                  5       nOSUR(                  0n[+        X5      eUR'                  5       n[        R,                  " UU R.                  U R0                  U R2                  U R4                  U R6                  5        X0R                  [        U5      '   [        U5      $ )z
Get details for specified IP address as a Details object.

If `timeout` is not `None`, it will override the client-level timeout
just for this operation.
ipTbogonr   /r$     i  zContent-Typeapplication/jsonerror)
isinstancer   r   explodedr   r   r    r   KeyErrorr   r   r   get_headersr&   r$   requestsr'   status_coder	   jsontextr   format_detailsr   r   r   r   r   )r(   
ip_addressr   detailscached_ipaddrreq_optsurlr$   response
error_codecontent_typeerror_responses               r*   
getDetailsHandler.getDetailsS   s    j+..*3
 3
 $,,J (:..G&DM#GG7##	 JJy'<=M=)) 		 ,d**+")Y 3##C++D,=,=t||L<<AWAA3&+--3&!--J#++//?L11!)")8==!9:66--/ 	$$NN  %%OO	
 -4

9Z()ws   %"B 
BBTc           
      "   US:X  a  [         n0 n/ nU H  n[        U[        5      (       d  [        U[        5      (       a  UR                  nU(       a*  [        U5      (       a  0 n	XS'   SU	S'   [        U	5      Xh'   Mj   U R                  [        U5         n
XU'   M     [        U5      S:X  a  U$ Ub  [        R                  " 5       n0 U R                  ESU0En[        S-   n[        R                   " U R"                  U R$                  5      nSUS	'   ['        S[        U5      U5       GH]  nUb>  [        R                  " 5       W-
  U:  a"  [        R(                  " U[+        5       U5      s  $ XX-    n [,        R.                  " U4UUS
.UD6n UR2                  S:X  a
  [5        5       eUR7                  5         UR9                  5       nUR;                  5        H  u  pXR                  [        U5      '   M     UR=                  U5        UR?                  5        Hg  n[        U[@        5      (       d  M  [        RB                  " UU RD                  U RF                  U RH                  U RJ                  U RL                  5        Mi     GM`     U$ ! [         a    UR                  U5         GM  f = f! [0         a$  n[        R(                  " UUU5      s SnAs  $ SnAff = f! [0         a$  n[        R(                  " UUU5      s SnAs  $ SnAff = f)aw  
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.
Nr/   Tr0   r   r   /batchr3   content-typer;   r$   r2   )'r   r5   r   r   r6   r   r   r    r   r7   appendlentimer   r   r   r8   r&   r$   rangereturn_or_failr
   r9   post	Exceptionr:   r	   raise_for_statusr;   itemsupdatevaluesdictr=   r   r   r   r   r   )r(   ip_addresses
batch_sizetimeout_per_batchtimeout_totalraise_on_failresultlookup_addressesr>   r?   r@   
start_timerA   rB   r$   ichunkrC   ejson_responsedetails                        r*   getBatchDetailsHandler.getBatchDetails   s   J 'J 'J *k22jK7 7 (00
hz22 *#' %,W%5"8$(JJy/D$EM)6:&# ',  A%M $J Jd**II7HI  ++D,=,=t||L"4q#./<A )IIK*,}<$33!#7#96  %8EN#==#W8@N''3.355))+
 %MMOM'4':':'<#
4;

9Z01 (= MM-( !--/fd++!00)),,11 *K =` E   8$++J778F  N$33M1fMMN  N$33M1fMMNsN   >J4J/*K J,+J,/
K9KKK 
L*L	L	Lc                    / nU HJ  n[        U[        5      (       d  [        U[        5      (       a  UR                  nUR	                  U5        ML     0 U R
                  En[         S3n[        R                  " SU R                  5      nSUS'   [        R                  " U4X&S.UD6nUR                  5         UR                  5       S   $ )zQ
Gets a URL to a map on https://ipinfo.io/map given a list of IPs (max
500,000).
z
/map?cli=1Nr3   rK   rL   	reportUrl)r5   r   r   r6   rM   r   r   r   r8   r$   r9   rR   rT   r;   )r(   ipsip_strsr/   rA   rB   r$   rC   s           r*   getMapHandler.getMap  s    
 B "k**j[.I.I[[NN2  ,d**+	$++D$,,?"4==

2:
 	!!#}}{++r-   c           	   #     #    Uc  [         n0 n/ nU H  n[        U[        5      (       d  [        U[        5      (       a  UR                  nU(       a*  [        U5      (       a  0 nXgS'   SUS'   [        U5      v   Mj   U R                  [        U5         nXU'   M     [        U5      S:X  a  [        UR                  5       5      e[        S-   n	[        R                   " U R"                  U R$                  5      n
SU
S'   ['        S[        U5      U5       H  nX[X-    n [(        R*                  " XU
S9n UR.                  S	:X  a
  [1        5       eUR3                  5         UR7                  5       n[        R8                  " UU R:                  U R<                  U R>                  U R@                  U RB                  5        U H0  nURE                  U5      nUU R                  [        U5      '   Uv   M2     M     g ! [         a    UR                  U5         GM  f = f! [,         a  nUeS nAff = f! [,         a"  n[        R4                  " X>5      s S nAs  $ S nAff = f7f)
Nr/   Tr0   r   rJ   r3   rK   rL   r2   )#r   r5   r   r   r6   r   r   r    r   r7   rM   rN   StopIterationrU   r   r   r8   r&   r$   rP   r9   rR   rS   r:   r	   rT   rQ   r;   r=   r   r   r   r   r   r'   )r(   rY   rZ   r]   r^   r_   r>   r?   r@   rB   r$   ra   batchrC   rc   r/   re   s                    r*   getBatchDetailsIterHandler.getBatchDetailsIter0  s(     'J&J*k22jK7 7 (00
hz22 *#' g&&8$(JJy/D$EM)6:& '&  A%// ++D,=,=t||L"4q#./<A$8E#=='JF''3.355))+ mmoG ((!!$$))  R,2

9R=) 5 =   8$++J778    F$33MEEFsz   A:I=G6BIH4*H-BI6HIHI
H*#H%%H**I-
I7IIIII)	r&   r    r   r   r   r   r   r$   r   )N)NN)NT)__name__
__module____qualname____firstlineno____doc__r+   rG   r   rf   rl   rq   __static_attributes__r%   r-   r*   r   r   "   s;    
)3V? H 3AF,8 	Ar-   r   )!rw   	ipaddressr   r   rO   r9   r4   r   cache.defaultr   r?   r   
exceptionsr	   r
   r   r   r   r   r   r   r   r    r0   r   datar   r   r   r   r   r   r%   r-   r*   <module>r~      sN    /    '  G     O Or-   