o
    aqe                     @   s  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lmZm	Z	m
Z
mZmZ d dlmZ d dlZd dlmZmZmZ d dlmZmZ d d	lmZ d
efddZdeded
efddZdede	e dedede	e d
eeef fddZde	e dededeeef dedededede	e de	e d
efd d!Zd"ed
ee
 fd#d$Zd%e	e d&e d'eded(ed"ed
eee!f fd)d*Z"dS )+    N)AbstractEventLoop)Logger)
SSLContext)UnionOptionalBinaryIOListDicturljoin)FormData	BasicAuthClientSession)SlackRequestErrorSlackApiError)get_user_agentreturnc                  C   s4   zt  W S  ty   t  } t |  |  Y S w )z.Retrieves the event loop or creates a new one.)asyncioget_event_loopRuntimeErrornew_event_loopset_event_loop)loop r   _/var/www/html/humari/django-venv/lib/python3.10/site-packages/slack/web/async_internal_utils.py_get_event_loop   s   

r   base_url
api_methodc                 C   s
   t | |S )a.  Joins the base Slack URL and an API method to form an absolute URL.

    Args:
        base_url (str): The base URL
        api_method (str): The Slack Web API method. e.g. 'chat.postMessage'

    Returns:
        The absolute API URL.
            e.g. 'https://www.slack.com/api/chat.postMessage'
    r
   )r   r   r   r   r   _get_url   s   
r   headerstokenhas_json	has_filesrequest_specific_headersc                 C   s   ddi}| du sd| vrt  |d< |r|dd|i | du r#i } ||  |r/|| |r8|ddi |r@|dd |S )a3  Constructs the headers need for a request.
    Args:
        has_json (bool): Whether or not the request has json.
        has_files (bool): Whether or not the request has files.
        request_specific_headers (dict): Additional headers specified by the user for a specific request.

    Returns:
        The headers dictionary.
            e.g. {
                'Content-Type': 'application/json;charset=utf-8',
                'Authorization': 'Bearer xoxb-1234-1243',
                'User-Agent': 'Python/3.6.8 slack/2.1.0 Darwin/17.7.0'
            }
    zContent-Typez!application/x-www-form-urlencodedNz
User-AgentAuthorizationz	Bearer {}zapplication/json;charset=utf-8)r   updateformatpop)r   r    r!   r"   r#   final_headersr   r   r   _get_headers(   s    


r)   	http_verbfilesdataparamsjsonauthsslproxyc        
      	   C   s
  |d u}
|d u}|
r|dkrd}t ||rt|d |d }|d ur1t|tr1dd | D }|d urCt|trCdd | D }|d urUt|trUdd | D }| } |d urdd	|v rd|d	} |d urqd	|v rq|d	} t|| |
||d
||||||	|d}|S )NPOSTz`Json data can only be submitted as POST requests. GET requests should use the 'params' argument.	client_idclient_secretc                 S      i | ]\}}|d ur||qS Nr   .0kvr   r   r   
<dictcomp>q       z#_build_req_args.<locals>.<dictcomp>c                 S   r5   r6   r   r7   r   r   r   r;   s   r<   c                 S   r5   r6   r   r7   r   r   r   r;   u   r<   r    )r   r    r!   r"   r#   )r   r,   r+   r-   r.   r0   r1   r/   )r   r   
isinstancedictitemsr'   r)   )r    r*   r+   r,   r-   r.   r   r/   r0   r1   r!   r"   msgreq_argsr   r   r   _build_req_argsZ   sD   

rB   rA   c                 C   sz   g }|  dd }|d ur;| D ]*\}}t|tr1t|ddd}|| | d ||i q| d ||i q|S )Nr+   zutf-8ignorerbr,   )r'   r?   r=   stropenencodeappendr%   )rA   
open_filesr+   r9   r:   fr   r   r   _files_to_data   s   

rK   current_sessiontimeoutloggerapi_urlc                    s6  d}| o| j  }|r| }ntjtj|d|ddd}d}zp|j||fi |4 I dH K}	i }
z	|	 I dH }
W n, tjyM   |d| d Y n tj	j
yf } zdt| }t||	d}~ww |
|	j|	jd}W d  I dH  n1 I dH sw   Y  W |s| I dH  |S |s| I dH  w w )	zSubmit the HTTP request with the running session or a new session.
    Returns:
        A dictionary of the response data.
    N)totalr/   )rM   r/   z7No response data returned from the following API call: .z#Failed to parse the response body: )r,   r   status_code)closedaiohttpr   ClientTimeoutr'   requestr.   ContentTypeErrordebugdecoderJSONDecodeErrorrE   r   r   statusclose)rL   rM   rN   r*   rO   rA   sessionuse_running_sessionresponseresr,   emessager   r   r   _request_with_session   sH   



(rc   )#r   r.   r   loggingr   r0   r   typingr   r   r   r   r	   urllib.parser   rT   r   r   r   slack.errorsr   r   	slack.webr   r   rE   r   r>   boolr)   rB   rK   intanyrc   r   r   r   r   <module>   s    


2
	

5
