o
    ެe
                     @   s   d dl Z d dlmZ d dlZd dlZd dlZd dlmZ ej	ej
eZeed  dZdZdZejdZejd	Zd
d Zdd Zdd ZdS )    N)HTTPBasicAuth)load_dotenvz/.envz?https://api.planningcenteronline.com/services/v2/songs?offset=0zChttps://api.planningcenteronline.com/services/v2/songs/?per_page=25zAhttps://api.planningcenteronline.com/explorer/services/v2/people/usernamepasswordc                 C   s   t j| tttd}|jd}|jd}|jd}|dur8t|t|d kr8td| d | d	 |  	 |jd
kr]|jd}td| d  t	
t| t j| tttd}|S )z
    Function for requesting data from Planning Center API
    Checks if the request-rate-limit is reached
    :param - URL to API endpoint to call
    :return - full response object
    missing - error handling in case of issues with API call
    )authzX-PCO-API-Request-Rate-CountzX-PCO-API-Request-Rate-PeriodzX-PCO-API-Request-Rate-LimitN
   z### current rate count - z rate periode - z rate limit - i  zRetry-Afterz'### rate count reached - sleeping for: z second)requestsgetr   r   r   headersintprintstatus_codetimesleep)endpointresponsecurrent_rate_countrate_periode
rate_limitretry_after r   &/var/www/bmteknikk.ddns.net/callAPI.pyrequest_endpoint   s   	
r   c                 C   s    d|  }t | t|}| S )N7https://api.planningcenteronline.com/services/v2/songs/)r   r   json)song_idapi_endpoint_songsong_data_responser   r   r   request_song5   s   r   c                 C   s    d|  d | }t |}| S )Nr   z/arrangements/)r   r   )r   arrangement_idapi_arrangemnt_urlresponse_arrangement_datar   r   r   request_arrangment<   s   r"   )r   requests.authr   r   osr   dotenvr   pathdirnameabspath__file__ENV_DIRapi_endpoint_start_fromapi_endpointapi_endpoint_personenvironr	   r   r   r   r   r"   r   r   r   r   <module>   s     