o
    fsix                     @   s&   d dl Z d dlmZ G dd dZdS )    N)settingsc                   @   s>   e Zd ZdZdd Zdd Zdd Zded	ee	 fd
dZ
dS )EMSApiClientz
    Client for the ICTAZ External EMS API.

    Authenticates with JWT and fetches paid registrations.
    Token is cached in-memory for the lifetime of this instance.
    c                 C   s(   t jd| _t j| _t j| _d | _d S )N/)	r   EMS_API_BASE_URLrstripbase_urlEMS_API_USERNAMEusernameEMS_API_PASSWORDpassword_tokenself r   B/var/www/html/smartRegister/backend/apps/attendance/ems_service.py__init__   s   
zEMSApiClient.__init__c                 C   sp   | j  d}tj|| j| jddd}|  | }|dp)|dp)|d}|s3td| || _	|S )	Nz/api/auth/signin?authMethod=jwt)r	   r      )jsontimeouttokenaccessTokenjwtz5EMS auth response did not contain a token. Response: )
r   requestspostr	   r   raise_for_statusr   get
ValueErrorr   )r   urlrespdatar   r   r   r   _authenticate   s   zEMSApiClient._authenticatec                 C   s   | j s|   | j S )N)r   r    r   r   r   r   
_get_token#   s   zEMSApiClient._get_token
event_typereturnc                 C   s|   |   }| j d}tj|d|idd| idd}|jdkr6d| _|   }tj|d|idd| idd}|  | S )	z
        Fetch all paid registrations for the given eventType (e.g. 'AGM', 'CONFERENCE').
        Returns a list of member dicts from the EMS API.
        z/api/registrations/paid-report	eventTypeAuthorizationzBearer <   )paramsheadersr   i  N)r!   r   r   r   status_coder   r   r   )r   r"   r   r   r   r   r   r   fetch_paid_registrations(   s&   
z%EMSApiClient.fetch_paid_registrationsN)__name__
__module____qualname____doc__r   r    r!   strlistdictr*   r   r   r   r   r      s    r   )r   django.confr   r   r   r   r   r   <module>   s    