o
    sgA                     @   sB   d dl Z d dlZd dlZd dlmZ d dlmZ defddZdS )    N)HTTPException)
parsed_urlurlc           	   
      s  t d}|stdddt| ddj} dd| d	}d
|  }z[t 4 I dH E}|j||dd4 I dH !}|  |	 I dH W  d  I dH  W  d  I dH  W S 1 I dH s_w   Y  W d  I dH  W dS 1 I dH svw   Y  W dS  tj
y   tddd tjy } z
t|jd| dd}~w tjy } z	tdd| dd}~w t	jy } ztddd|d}~ww )z
    Fetch brand details for a given URL using the Brandfetch API.

    Args:
        url (str): The URL of the company to fetch brand details for.

    Returns:
        dict: The brand details retrieved from the Brandfetch API.
    BRAND_FETCH_API_KEYi  z"Brand Fetch API key not configured)status_codedetailF)check_redirectzapplication/jsonzBearer )acceptAuthorizationz$https://api.brandfetch.io/v2/brands/N   )headerstimeouti  z$Request to Brandfetch API timed out.zBrandfetch API error: z-An error occurred while fetching brand data: z3Failed to decode JSON response from Brandfetch API.)osgetenvr   r   domainaiohttpClientSessiongetraise_for_statusjsonClientTimeoutClientResponseErrorstatusClientErrorJSONDecodeError)	r   BRAND_FETCH_APIrequest_headerrequest_urlsessionresponsehttp_errreq_erre r#   3/var/www/html/XCapMarket/utils/brand/brand_fetch.pyfetch_brand_details
   sd   


2r%   )	r   r   r   fastapir   utils.url_parserr   strr%   r#   r#   r#   r$   <module>   s    