
    +j                        S SK r S SK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r\R                  " \
5      rS rS\S\4S jrSS.S	\S
\S\4S jjrS\S\S\S\4S jrS\S\4S jr       S7S\S\S\S\S-  S\S-  S\S\S-  S\S-  S\S\S-  4S jjr\
S:X  af  \R+                  \R,                  5        \" \S5      (       d   " S S5      r\" 5       r\" SS S!S"S#0S$S%0S&S'S(0S)9  \" S*S+S,S-S.S/0S0.S1S29  \" S3S S4S&S5S69  gg)8    N)Anyc                     [         R                  " 5       n [        R                  R	                  U S5      n [        R
                  " USS9  U$ ! [         a,  n[        R                  SU[        U5      5        U s SnA$ SnAff = f)zZEnsures the API log directory exists within ComfyUI's temp directory and returns its path.api_logsTexist_okz'Error creating API log directory %s: %sN)
folder_pathsget_temp_directoryospathjoinmakedirs	Exceptionloggererrorstr)base_temp_dirlog_dires      C/home/wildlama/comfy/ComfyUI/comfy_api_nodes/util/request_logger.pyget_log_directoryr      sk     335Mggll=*5G
Gd+
 N	  >QPs   A 
B!A?9B?Bnamereturnc                 z    U (       d  g[         R                  " SSU 5      nUR                  S5      nU(       d  SnU$ )Nlogz[^A-Za-z0-9._-]+_z ._)resubstrip)r   	sanitizeds     r   _sanitize_filename_componentr       s7    *C6I&I	    
   )lengthpartsr#   c                     [         R                  " SR                  U5      R                  S5      5      R	                  5       S U  $ )N|utf-8)hashlibsha1r   encode	hexdigest)r#   r$   s     r   _short_hashr,   %   s4    <<%009:DDFwOOr!   r   operation_idrequest_urlc                    [         R                   R                  5       R                  S5      n[        U5      n[	        U=(       d    SU=(       d    S5      nSnU S3nSU S3nU(       d  Sn[        SU[        U 5      -
  S-
  5      n	[        S	U	[        U5      -
  [        U5      -
  5      n
[        U5      U
:  a  US
U
 R                  S5      n[        R                  R                  X U U 35      $ )zgBuild log filepath. We keep it well under common path length limits aiming for <= 240 characters total.z%Y%m%d_%H%M%S_%f    r   z.logop<         Nz ._-)datetimenowstrftimer    r,   maxlenrstripr
   r   r   )r   r-   r.   	timestampslughmax_total_pathprefixsuffixmax_filename_lenmax_slug_lens              r   _build_log_filepathrD   )   s    !!%%'001CDI'5DL&B(9r:A N{!_F4[F2~G<q@Aq*S[83v;FGL
4y<M\"))&177<<8D6&!:;;r!   datac                 >   [        U [        5      (       a   U R                  S5      $ [        U [
        [        45      (       a   [        R                  " U SSS9$ [        U 5      $ ! [         a    S[	        U 5       S3s $ f = f! [         a    [        U 5      s $ f = f)z5Helper to format data (dict, str, bytes) for logging.r'   z[Binary data of length z bytes]   F)indentensure_ascii)
isinstancebytesdecodeUnicodeDecodeErrorr:   dictlistjsondumps	TypeErrorr   )rE   s    r   _format_data_for_loggingrS   =   s    $	@;;w'' 
D4,	'	'	::d15AA t9 " 	@,SYKw??	@
  	t9	s#   A% B %B BBBrequest_methodrequest_headersrequest_paramsrequest_dataresponse_status_coderesponse_headersresponse_contenterror_messagec
                     [        5       n
[        XU5      n/ nUR                  S[        R                  R	                  5       R                  5        35        UR                  SU  35        UR                  S5        UR                  SU 35        UR                  SU 35        U(       a  UR                  S[        U5       35        U(       a  UR                  S[        U5       35        Ub  UR                  S	[        U5       35        UR                  S
5        Ub  UR                  SU 35        U(       a  UR                  S[        U5       35        Ub  UR                  S[        U5       35        U	(       a  UR                  SU	 35         [        USSS9 nUR                  SR                  U5      5        SSS5        [        R                  SU5        g! , (       d  f       N%= f! [         a*  n[        R                  SU[        U5      5         SnAgSnAff = f! [         a!  n[        R                  " SU5         SnAgSnAff = f)z
Logs API request and response details to a file in the temp/api_logs directory.
Filenames are sanitized and length-limited for cross-platform safety.
If we still fail to write, we fall back to appending into api.log.
zTimestamp: zOperation ID: zE------------------------------ REQUEST ------------------------------zMethod: zURL: z	Headers:
zParams:
NzData/Body:
zG
------------------------------ RESPONSE ------------------------------zStatus Code: z	Content:
zError:
wr'   )encoding
zAPI log saved to: %szError writing API log to %s: %sz'[DEBUG] log_request_response failed: %s)r   rD   appendr6   r7   	isoformatrS   openwriter   r   debugr   r   r   logging)r-   rT   r.   rU   rV   rW   rX   rY   rZ   r[   r   filepathlog_contentfr   _log_es                   r   log_request_responserj   L   s   ""I#%&wkJ!#[):):)>)>)@)J)J)L(MNO^L>:;<=Xn%567U;-01,D_,U+VWX+CN+S*TUV#.F|.T-UVWDE+/C.DEF,DEU,V+WXY',DEU,V+WXY-9:	Nhg6!		+./ 7LL/: 76  	NLL:Hc!fMM	N I?HHIsT   FH( G1  !G G1  
G.*G1 1
H%; H H(  H%%H( (
I2II__main__r	   c                       \ rS rSrS rSrg)MockFolderPaths   c                     [         R                  R                  [         R                  R                  [        5      S5      n[         R
                  " USS9  U$ )Ntemp_test_logsTr   )r
   r   r   dirname__file__r   )selfps     r   r	   "MockFolderPaths.get_temp_directory   s7    GGLL!:<LMA-r!    N)__name__
__module____qualname____firstlineno__r	   __static_attributes__rv   r!   r   rm   rm      s    r!   rm   test_operation_getGETzhttps://api.example.com/testAuthorizationzBearer testtokenparam1value1   messagezSuccess!)r-   rT   r.   rU   rV   rX   rZ   test_operation_post_errorPOSTzhttps://api.example.com/submitvaluenum{   )keynestedzConnection timed out)r-   rT   r.   rW   r[   test_binary_responsez!https://api.example.com/image.pngs   PNG

   IHDR...)r-   rT   r.   rX   rZ   )NNNNNNN)r6   r(   rP   re   r
   r   typingr   r   	getLoggerrw   r   r   r   r    intr,   rD   rS   rN   rj   setLevelDEBUGhasattrrm   rv   r!   r   <module>r      s       	 	  			8	$
s s  ,. P PS P# P< <C <c <c <(3 3 & $("&'+$(  $3I3I3I 3I D[	3I
 4K3I 3I *3I Tk3I 3I :3Il z
OOGMM"<!566	 	 '()2(*<= (+ #Z0 04$=, +7 B; r!   