o
    aqeMU                     @   sR  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlmZ ddlmZ ddlmZmZmZ ddlmZ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 Z dd
l m!Z!m"Z" ddl#m$Z% ddl#m&Z& ddl'm(Z(m)Z) ddl*m+Z+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3 G dd dZ4dS )z5A Python module for interacting with Slack's Web API.    N)HTTPResponse)
SSLContext)BinaryIODictList)OptionalUnion)	HTTPError)	urlencode)RequesturlopenOpenerDirectorProxyHandlerHTTPSHandler)FormData	BasicAuth)SlackRequestError)convert_bool_to_0_or_1get_user_agent)_get_event_loop_build_req_args_get_url_files_to_data_request_with_session)show_2020_01_deprecation)SlackResponsec                   @   s   e Zd ZdZdeddddddddddfdee dededeej d	ee	 d
ee de
de
deej dee dee dee fddZdddddddddedededeeef dededededeejef fddZdeded edefd!d"Zdeeef fd#d$Zdefd%d&Zdeeef fd'd(Zdi i i i i d)ded*ed+eeef d,ed-eeef deeejf d.eeef defd/d0Zd*ed1eeeeef f deeef fd2d3Zded4e
d5e
d.edeeef f
d6d7Zed8eded9ed:ede
f
d;d<Z dS )=
BaseClientzhttps://www.slack.com/api/N   Ftokenbase_urltimeoutloopsslproxy	run_asyncuse_sync_aiohttpsessionheadersuser_agent_prefixuser_agent_suffixc                 C   sp   |d u rd n|  | _|| _|| _|| _|| _|| _|| _|	| _|
p#i | _	t
||| j	d< tt| _|| _d S )Nz
User-Agent)stripr   r   r    r"   r#   r$   r%   r&   r'   r   logging	getLogger__name___logger_event_loop)selfr   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)    r1   V/var/www/html/humari/django-venv/lib/python3.10/site-packages/slack/web/base_client.py__init__*   s   


zBaseClient.__init__POST)	http_verbfilesdataparamsjsonr'   auth
api_methodr5   r6   r7   r8   r9   r:   returnc                C   s   t | j|}	|p	i }|| j t| j|||||||| j| jd
}
t| | j	s+| j
rR| jdu r4t | _tj| j||	|
d| jd}| j	rG|S | j
rP| j|S dS | j|	|
dS )a  Create a request and execute the API call to Slack.

        Args:
            api_method (str): The target Slack API method.
                e.g. 'chat.postMessage'
            http_verb (str): HTTP Verb. e.g. 'POST'
            files (dict): Files to multipart upload.
                e.g. {image OR file: file_object OR file_path}
            data: The body to attach to the request. If a dictionary is
                provided, form-encoding will take place.
                e.g. {'key1': 'value1', 'key2': 'value2'}
            params (dict): The URL parameters to append to the URL.
                e.g. {'key1': 'value1', 'key2': 'value2'}
            json (dict): JSON for the body to attach to the request
                (if files or data is not specified).
                e.g. {'key1': 'value1', 'key2': 'value2'}
            headers (dict): Additional request headers
            auth (dict): A dictionary that consists of client_id and client_secret

        Returns:
            (SlackResponse)
                The server's response to an HTTP request. Data
                from the response can be accessed like a dict.
                If the response included 'next_cursor' it can
                be iterated on to execute subsequent requests.

        Raises:
            SlackApiError: The following Slack API call failed:
                'chat.postMessage'.
            SlackRequestError: Json data can only be submitted as
                POST requests.
        )
r   r5   r6   r7   r8   r9   r'   r:   r"   r#   Nr5   api_urlreq_args)r!   )r>   r?   )r   r   updater'   r   r   r"   r#   r   r$   r%   r/   r   asyncioensure_future_sendrun_until_complete
_sync_send)r0   r;   r5   r6   r7   r8   r9   r'   r:   r>   r?   futurer1   r1   r2   api_callH   s:   -
zBaseClient.api_callr>   r?   c              	      s   t |}z"d|v rt|d |d< | j|||dI dH }W |D ]}|  q n
|D ]}|  q*w | |||| jd}tdi i || S )aR  Sends the request out for transmission.

        Args:
            http_verb (str): The HTTP verb. e.g. 'GET' or 'POST'.
            api_url (str): The Slack API url. e.g. 'https://slack.com/api/chat.postMessage'
            req_args (dict): The request arguments to be attached to the request.
            e.g.
            {
                json: {
                    'attachments': [{"pretext": "pre-hello", "text": "text-world"}],
                    'channel': '#random'
                }
            }
        Returns:
            The response parsed into a SlackResponse object.
        r8   r=   N)clientr5   r>   r?   r%   r1   )r   r   _requestcloser%   r   validate)r0   r5   r>   r?   
open_filesresfr7   r1   r1   r2   rC      s(   


zBaseClient._sendc                   s"   t | j| j| j|||dI dH S )zSubmit the HTTP request with the running session or a new session.
        Returns:
            A dictionary of the response data.
        )current_sessionr    loggerr5   r>   r?   N)r   r&   r    r.   )r0   r5   r>   r?   r1   r1   r2   rI      s   zBaseClient._requestc              	   C   s  d|v r|d nd }d|v r|d nd }d|v r|d nd }d|v r&|d nd }d|v r0|d nd }|r=d|v r=| dnd }d|v rG|d nd }	|	d urrt|	trY|	 |d< nt|	trc|	|d< n| jd	|	 d
t|	 d i }
|r{|
| |r|
| | j	||i |
|||dS )Nr8   r7   r6   r9   r'   r   r:   AuthorizationzAs the auth: z: z is unsupported, skipped)r   urlquery_paramsbody_paramsr6   	json_bodyadditional_headers)
get
isinstancer   encodestrr.   warningtyper@   _urllib_api_call)r0   r>   r?   r8   r7   r6   _jsonr'   r   r:   rT   r1   r1   r2   rE      s<   




zBaseClient._sync_sendc                 C   s4   | j ||d}t|d t|d t|d dS )a  This method is supposed to be used only for SlackResponse pagination

        You can paginate using Python's for iterator as below:

          for response in client.conversations_list(limit=100):
              # do something with each response here
        rR   argsstatusr'   body)status_coder'   r7   )_perform_urllib_http_requestintdictr9   loads)r0   r>   r?   responser1   r1   r2   _request_for_pagination   s
   

z"BaseClient._request_for_pagination)r   rS   rU   rT   r6   rV   rR   rS   rU   rT   rV   c                C   s  g }z;t |}t |}| jjtjkrCdtdtfdd}	dd | D }
| jd| d|	| d	|	| d
|	| d| d|
  i }|durt|trt	|dkr|rf| D ]\}}|
||i qZ| D ]8\}}t|trt|ddd}|| |
||i qjt|ttfr|
|t|i qj|
||i qj| j|p| jtdu|du|d}|||||d}|rt|}d|v r| d| n| d| }| j||d}|drz	t|d }W n tjjy } zdt| }t||d}~ww d}|rt|}|
| n|}||d< t| d|||t|d |d dd  W |D ]}|j!s<|"  q2S |D ]}|j!sK|"  qAw )aL  Performs a Slack API request and returns the result.

        :param token: Slack API Token (either bot token or user token)
        :param url: a complete URL (e.g., https://www.slack.com/api/chat.postMessage)
        :param query_params: query string
        :param json_body: json data structure (it's still a dict at this point),
            if you give this argument, body_params and files will be skipped
        :param body_params: form params
        :param files: files to upload
        :param additional_headers: request headers to append
        :return: API response
        valuesr<   c                 S   s$   | rt | ts	i S dd |  D S )Nc                 S   s$   i | ]\}}|t |trd n|qS )z(bytes))rX   bytes.0kvr1   r1   r2   
<dictcomp>-  s    zGBaseClient._urllib_api_call.<locals>.convert_params.<locals>.<dictcomp>)rX   rf   items)rj   r1   r1   r2   convert_params*  s
   z3BaseClient._urllib_api_call.<locals>.convert_paramsc                 S   s&   i | ]\}}||  d krdn|qS )authorizationz
(redacted))lowerrl   r1   r1   r2   rp   2  s    z/BaseClient._urllib_api_call.<locals>.<dictcomp>zSending a request - url: z, query_params: z, body_params: z	, files: z, json_body: z, headers: Nr   utf-8ignorerb)r   has_json	has_filesrV   )r'   r7   r8   r6   r9   ?&r_   rb   z#Failed to parse the response body: r8   r4   r'   ra   F)rH   r5   r>   r?   r7   r'   rc   r%   )#r   r.   levelr+   DEBUGrf   rq   debugrX   lenr@   rZ   openrY   append	bytearrayrk   ioBytesIO_build_urllib_request_headersr   r9   r
   rd   rW   rg   decoderJSONDecodeErrorerrSlackApiErrorcopyr   rK   closedrJ   )r0   r   rR   rS   rU   rT   r6   rV   files_to_closerr   r'   request_datarn   ro   rN   request_headersrequest_argsqrh   response_body_dataemessage
all_paramsr1   r1   r2   r]   
  s   	

$

	
zBaseClient._urllib_api_callr`   c             
   C   s@  |d }|d rt |d }d|d< n|d rdt  }d|d }|d	 }t }|d }| D ]o\}	}
t|
d
d}|r|
	 rd}t|
dd}|r\t
|trZ|dn|}d|v rd|d }t|d pld}d|	 d| dd| d }|
 }
nd|	 d}t|
d}
|| ||d |d ||
 q6|| | }d| |d< t||d< n|d rt|d }d|d< nd}t
|tr|d}zq| drCtd|||d}d}| jdurt
| jtrtjt| j| jdt| jd }n	td!| j d"d}|r"|j || j!d#}n	t"|| j| j!d$}|j#$ p2d}| |}|j%|j#|d%W S td&|  t&y } z2|j%|j#d'}|j%d(kri|d d) |d d*< |j#$ ppd}| |}||d+< |W  Y d}~S d}~w t'y } z| j()d,|  |d}~ww )-a  Performs an HTTP request and parses the response.

        :param url: a complete URL (e.g., https://www.slack.com/api/chat.postMessage)
        :param args: args has "headers", "data", "params", and "json"
            "headers": Dict[str, str]
            "data": Dict[str, any]
            "params": Dict[str, str],
            "json": Dict[str, any],
        :return: dict {status: int, headers: Headers, body: str}
        r'   r9   application/json;charset=utf-8Content-Typer7   z--------------s   
--asciis   --
readableNzUploaded filenameru   filenamer   zapplication/octet-streamz(
Content-Disposition: form-data; name="z"; filename="z"
zContent-Type: z
s   
zmultipart/form-data; boundary=zContent-Lengthr8   !application/x-www-form-urlencodedhttpr4   )methodrR   r7   r'   )r   https)contextzInvalid proxy detected: z must be a str value)r    )r   r    )ra   r'   rb   zInvalid URL detected: )ra   r'   i  zretry-afterzRetry-Afterrb   z.Failed to send a request to Slack API server: )*r9   dumpsuuiduuid4rY   r   r   rq   getattrr   rX   rk   decode	mimetypes
guess_typereadrZ   writegetvaluer   r
   rt   
startswithr   r#   urllibrequestbuild_openerr   r   r"   r   r   r    r   r'   get_content_charsetcoder	   	Exceptionr.   error)r0   rR   r`   r'   rb   boundarysep_boundaryend_boundaryr7   keyvaluer   r   	name_attrmimetypetitlereqopenerrespcharsetr   r   r1   r1   r2   rd     s   










z'BaseClient._perform_urllib_http_requestrx   ry   c                 C   s`   ddi}| | j |r| dd|i |r| | |r&| ddi |r.|dd  |S )Nr   r   rQ   z	Bearer {}r   )r@   r'   formatpop)r0   r   rx   ry   rV   r'   r1   r1   r2   r     s   
z(BaseClient._build_urllib_request_headerssigning_secret	timestamp	signaturec                 C   sV   t dt td| d| }t| }t||tj	 }d| }t
||S )a  
        Slack creates a unique string for your app and shares it with you. Verify
        requests from Slack with confidence by verifying signatures using your
        signing secret.

        On each HTTP request that Slack sends, we add an X-Slack-Signature HTTP
        header. The signature is created by combining the signing secret with the
        body of the request we're sending using a standard HMAC-SHA256 keyed hash.

        https://api.slack.com/docs/verifying-requests-from-slack#how_to_make_a_request_signature_in_4_easy_steps__an_overview

        Args:
            signing_secret: Your application's signing secret, available in the
                Slack API dashboard
            data: The raw body of the incoming request - no headers, just the body.
            timestamp: from the 'X-Slack-Request-Timestamp' header
            signature: from the 'X-Slack-Signature' header - the calculated signature
                should match this.

        Returns:
            True if signatures matches
        zqAs this method is deprecated since slackclient 2.6.0, use `from slack.signature import SignatureVerifier` insteadzv0::zv0=)warningswarnDeprecationWarningrZ   rY   hmacnewhashlibsha256	hexdigestcompare_digest)r   r7   r   r   
format_reqencoded_secretrequest_hashcalculated_signaturer1   r1   r2   validate_slack_signature  s   

z#BaseClient.validate_slack_signature)!r-   
__module____qualname__BASE_URLr   rZ   re   rA   AbstractEventLoopr   boolaiohttpClientSessionrf   r3   r   r   Futurer   rG   rC   r   anyrI   rE   ri   r   r   r]   rd   r   staticmethodr   r1   r1   r1   r2   r   '   s   	

"
	

T
)$


	

v

q

r   )5__doc__rA   r   r   r   r   r9   r+   r   r   r   r   http.clientr   r"   r   typingr   r   r   r   r   urllib.errorr	   urllib.parser
   urllib.requestr   r   r   r   r   r   r   r   slack.errorserrorsr   r   	slack.webr   r   slack.web.async_internal_utilsr   r   r   r   r   slack.web.deprecationr   slack.web.slack_responser   r   r1   r1   r1   r2   <module>   s8    