o
    sg[	                     @   sP   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 dd Z
dS )    N)HTTPException)JSONResponse)get_google_fonts_without_cachec            
   
      s  zt jtddttddddd} | d}|r"t|W S td	}|s/td
ddd| }t	
 4 I dH h}||4 I dH G}|jdkrSt|jdd| I dH }dd |dg D }|t|d}| ddt| |W  d  I dH  W  d  I dH  W S 1 I dH sw   Y  W d  I dH  W dS 1 I dH sw   Y  W dS  t	jy }	 ztd
dt|	 dd}	~	w t jy }	 ztdt|	 t I dH W  Y d}	~	S d}	~	w ty }	 ztd
dt|	 dd}	~	ww )z
    Retrieves a list of all available Google Font families.
    Caches the response for 7 days to minimize API calls.

    Returns:
        dict: A dictionary containing font families and total count
    
REDIS_HOST	localhost
REDIS_PORTi  r   T)hostportdbdecode_responsesgoogle_fonts_cacheGOOGLE_FONTS_API_KEYi  z#Google Fonts API key not configured)status_codedetailz4https://www.googleapis.com/webfonts/v1/webfonts?key=N   zFailed to fetch Google Fontsc                 S   s   g | ]}|d  qS )family ).0fontr   r   6/var/www/html/XCapMarket/utils/google_fonts_service.py
<listcomp>2   s    z)get_google_fonts_list.<locals>.<listcomp>items)fontstotali:	 z&Error connecting to Google Fonts API: zRedis error: %szUnexpected error: )redisRedisosgetenvintgetjsonloadsr   aiohttpClientSessionstatuslensetexdumpsClientErrorstr
RedisErrorprintr   	Exception)
redis_clientcached_fontsr   urlsessionresponse
fonts_datafont_familiesresponse_dataer   r   r   get_google_fonts_list   sd   




2r6   )r    r   r"   r   fastapir   fastapi.responsesr   utils.google_fontsr   r6   r   r   r   r   <module>   s    