U
    g
                     @   sr   d Z ddlZddlmZmZmZmZmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ eG dd	 d	ZdS )
zSThis module contains a class that holds the parameters of a request to the Bot API.    N)AnyDictListOptionalUnionfinal)	urlencode)TextEncoding)UploadFileDict)RequestParameterc                
   @   s   e Zd ZdZdZdeee  dddZe	e
eeeeee e
eef f f ddd	Ze	e
eef dd
dZdee
eef  edddZdeee
eef  edddZe	edddZe	edddZdS )RequestDataa?  Instances of this class collect the data needed for one request to the Bot API, including
    all parameters and files to be sent along with the request.

    .. versionadded:: 20.0

    Warning:
        How exactly instances of this are created should be considered an implementation detail
        and not part of PTBs public API. Users should exclusively rely on the documented
        attributes, properties and methods.

    Attributes:
        contains_files (:obj:`bool`): Whether this object contains files to be uploaded via
            ``multipart/form-data``.
    )_parameterscontains_filesN)
parametersc                 C   s$   |pg | _ tdd | j D | _d S )Nc                 s   s   | ]}|j V  qd S N)Zinput_files.0param r   A/tmp/pip-unpacked-wheel-swnnwir2/telegram/request/_requestdata.py	<genexpr>2   s     z'RequestData.__init__.<locals>.<genexpr>)r   anyr   )selfr   r   r   r   __init__0   s    
zRequestData.__init__)returnc                 C   s   dd | j D S )a  Gives the parameters as mapping of parameter name to the parameter value, which can be
        a single object of type :obj:`int`, :obj:`float`, :obj:`str` or :obj:`bool` or any
        (possibly nested) composition of lists, tuples and dictionaries, where each entry, key
        and value is of one of the mentioned types.

        Returns:
            Dict[:obj:`str`, Union[:obj:`str`, :obj:`int`, List[any], Dict[any, any]]]
        c                 S   s    i | ]}|j d k	r|j|j qS r   )valuenamer   r   r   r   
<dictcomp>>   s   
 z*RequestData.parameters.<locals>.<dictcomp>r   r   r   r   r   r   4   s    
zRequestData.parametersc                 C   s   dd | j D S )a  Gives the parameters as mapping of parameter name to the respective JSON encoded
        value.

        Tip:
            By default, this property uses the standard library's :func:`json.dumps`.
            To use a custom library for JSON encoding, you can directly encode the keys of
            :attr:`parameters` - note that string valued keys should not be JSON encoded.

        Returns:
            Dict[:obj:`str`, :obj:`str`]
        c                 S   s    i | ]}|j d k	r|j|j qS r   )Z
json_valuer   r   r   r   r   r   Q   s   
 z/RequestData.json_parameters.<locals>.<dictcomp>r   r   r   r   r   json_parametersD   s    zRequestData.json_parameters)encode_kwargsr   c                 C   s   |rt | jf|S t | jS )a  Encodes the parameters with :func:`urllib.parse.urlencode`.

        Args:
            encode_kwargs (Dict[:obj:`str`, any], optional): Additional keyword arguments to pass
                along to :func:`urllib.parse.urlencode`.

        Returns:
            :obj:`str`
        )r   r    )r   r!   r   r   r   url_encoded_parametersW   s    
z"RequestData.url_encoded_parameters)urlr!   r   c                 C   s   | j |d}| d| S )a  Shortcut for attaching the return value of :meth:`url_encoded_parameters` to the
        :paramref:`url`.

        Args:
            url (:obj:`str`): The URL the parameters will be attached to.
            encode_kwargs (Dict[:obj:`str`, any], optional): Additional keyword arguments to pass
                along to :func:`urllib.parse.urlencode`.

        Returns:
            :obj:`str`
        )r!   ?)r"   )r   r#   r!   Zurl_parametersr   r   r   parametrized_urle   s    zRequestData.parametrized_urlc                 C   s   t | jtjS )a  The :attr:`parameters` as UTF-8 encoded JSON payload.

        Tip:
            By default, this property uses the standard library's :func:`json.dumps`.
            To use a custom library for JSON encoding, you can directly encode the keys of
            :attr:`parameters` - note that string valued keys should not be JSON encoded.

        Returns:
            :obj:`bytes`
        )jsondumpsr    encoder	   ZUTF_8r   r   r   r   json_payloadt   s    zRequestData.json_payloadc                 C   s(   i }| j D ]}|j}|r
|| q
|S )zGives the files contained in this object as mapping of part name to encoded content.

        .. versionchanged:: 21.5
            Content may now be a file handle.
        )r   multipart_dataupdate)r   r*   r   Zm_datar   r   r   r*      s    
zRequestData.multipart_data)N)N)N)__name__
__module____qualname____doc__	__slots__r   r   r   r   propertyr   strr   intr   r   r    r"   r%   bytesr)   r
   r*   r   r   r   r   r      s   0 r   )r/   r&   typingr   r   r   r   r   r   urllib.parser   Ztelegram._utils.stringsr	   Ztelegram._utils.typesr
   Z"telegram.request._requestparameterr   r   r   r   r   r   <module>   s    