o
    Vh                     @   sD   d Z ddlmZ ddlmZ dZdd Zdd Zdd	 Zd
d ZdS )z8Implementation of nested form-data encoding function(s).   )
basestring)	urlencodec                    sz   t ttf t| }tdd |D std|}t fdd|D r2t|}t fdd|D s#t|g|R i |S )aT  Handle nested form-data queries and serialize them appropriately.

    There are times when a website expects a nested form data query to be sent
    but, the standard library's urlencode function does not appropriately
    handle the nested structures. In that case, you need this function which
    will flatten the structure first and then properly encode it for you.

    When using this to send data in the body of a request, make sure you
    specify the appropriate Content-Type header for the request.

    .. code-block:: python

        import requests
        from requests_toolbelt.utils import formdata

        query = {
           'my_dict': {
               'foo': 'bar',
               'biz': 'baz",
            },
            'a': 'b',
        }

        resp = requests.get(url, params=formdata.urlencode(query))
        # or
        resp = requests.post(
            url,
            data=formdata.urlencode(query),
            headers={
                'Content-Type': 'application/x-www-form-urlencoded'
            },
        )

    Similarly, you can specify a list of nested tuples, e.g.,

    .. code-block:: python

        import requests
        from requests_toolbelt.utils import formdata

        query = [
            ('my_list', [
                ('foo', 'bar'),
                ('biz', 'baz'),
            ]),
            ('a', 'b'),
        ]

        resp = requests.get(url, params=formdata.urlencode(query))
        # or
        resp = requests.post(
            url,
            data=formdata.urlencode(query),
            headers={
                'Content-Type': 'application/x-www-form-urlencoded'
            },
        )

    For additional parameter and return information, see the official
    `urlencode`_ documentation.

    .. _urlencode:
        https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode
    c                 s   s    | ]}t |V  qd S N)_is_two_tuple).0i r   t/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/requests_toolbelt/utils/formdata.py	<genexpr>N   s    zurlencode.<locals>.<genexpr>zQExpected query to be able to be converted to a list comprised of length 2 tuples.c                 3   s    | ]
\}}t | V  qd S r   )
isinstance)r   _vexpand_classesr   r	   r
   S   s    )	dictlisttuple_to_kv_listall
ValueErrorany_expand_query_values
_urlencode)queryargskwargsoriginal_query_list
query_listr   r   r	   r   
   s   
Ar   c                 C   s   t | drt|  S | S )Nitems)hasattrr   r   )dict_or_listr   r   r	   r   Y   s   
r   c                 C   s   t | ttfot| dkS )Nr   )r   r   r   len)itemr   r   r	   r   _   s   r   c                    sX   g }| D ]%\}}t |tr|||f q|d  t|}| fdd|D  q|S )Nz[%s]c                 3   s     | ]\}} | |fV  qd S r   r   )r   kr   key_fmtr   r	   r
   k   s    z'_expand_query_values.<locals>.<genexpr>)r   r   appendr   extend)r   r   keyvalue
value_listr   r$   r	   r   c   s   
r   N)	__doc___compatr   r   r   __all__r   r   r   r   r   r   r	   <module>   s   O