
    ti4                         d dl Z d dlZd dlZd dlZd dlmZ  e         e j
                  d      Z e j
                  d      Z e j
                  d      Z e j
                  dd      Z	da
d ad Zd	 Zy)
    N)load_dotenvZOHO_CLIENT_IDZOHO_CLIENT_SECRETZOHO_REFRESH_TOKENZOHO_BASE_URLzhttps://www.zohoapis.inc                  .   t         r!t        j                         t        k  rt         S d} t        t        t
        dd}t        j                  | |      }|j                         }d|v r"|d   a t        j                         dz   at         S t        d|       y )Nz'https://accounts.zoho.in/oauth/v2/tokenrefresh_token)r	   	client_idclient_secret
grant_type)dataaccess_tokeni  z Failed to get access token:)
r   timetoken_expiryr   r   r   requestspostjsonprint)urlr   responseresults       4/var/www/html/zoho_frontend/frontend/zoho_service.pyget_access_tokenr      s    		l2
3C+#+%	D }}St,H]]_Fn-yy{T),f5    c                    t               }|sg ddS dd| i}t         d|  }t        j                  ||      }|j                  dk(  r|j                         S t        d|  d	|j                   d
|j                          dg iS )NzAccess token not found)r   errorAuthorizationzZoho-oauthtoken z/crm/v2/)headers   zFailed to fetch z: z, r   )r   r   r   getstatus_coder   r   text)module_namer   r   r   r   s        r   fetch_zoho_datar$   )   s    #%L%=>>"2<. ABGO8K=
1C||C1Hs"}} R0D0D/ERWX|r   )osr   r   dotenvr   getenvr   r   r   r   r   r   r   r$    r   r   <module>r)      su    	     +,RYY34 RYY34 		/+DE2r   