o
    .j                     @  s  d dl mZ d dlmZ d dlmZ d dl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 d	d
lmZ d	dlmZmZ d	dlmZmZ d	dlmZ d	dlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) ededdZ*d)ddZ+d*ddZ,d+d!d"Z-d,d&d'Zd(S )-    )annotations)deepcopy)Message)HeaderParser)BufferedReader	RawIOBase)dumps)loads)AnyIterableMappingTypeVar   )CustomHeader)HeaderHeaders)decodeencode)CaseInsensitiveDict)
class_to_header_namedecode_partialsextract_class_nameextract_encoded_headersheader_content_splitheader_name_to_classis_content_json_objectis_legal_header_namenormalize_strtransform_possible_encodedTT)bound	covariantraw_headersr
   returnr   c                 C  s8  t | tr	t| S d}t | tr+| dr | dr tt| S t j	| dd
 }nct | ts:t | ts:t | trOtt | trB| n|  pGd\}}t|S t | tsYt | tr^| 
 }n0tt| }|r|dv rg }| jjD ]}| jj|D ]	}|||f qyqpn	|dv r| j
 }|du rtd	t|  d
tt|}t|dkrt| dkrt | tst | tr| jt | trdnddd}t|dkrt|d S g }	|D ]A\}
}t|
du rqt|}|du rt |d}n|g}t|dkrt!|
dkr|D ]}|	t"|
| qq|	t"|
| qt|	 S )a  
    Just decode anything that could contain headers. That simple PERIOD.
    If passed with a Headers instance, return a deep copy of it.
    :param raw_headers: Accept bytes, str, fp, dict, JSON, email.Message, requests.Response, niquests.Response, urllib3.HTTPResponse and httpx.Response.
    :raises:
        TypeError: If passed argument cannot be parsed to extract headers from it.
    N{}T)headersonly    )zrequests.models.Responsezniquests.models.Responsezniquests.models.AsyncResponse)zhttpx._models.Responsezurllib3.response.HTTPResponsez)urllib3._async.response.AsyncHTTPResponsez$urllib3_future.response.HTTPResponsez0urllib3_future._async.response.AsyncHTTPResponsezCannot parse type z' as it is not supported by kiss-header.r      

r   )maxsplit   F,subject)#
isinstancer   r   str
startswithendswithr   
json_loadsr   parsestritemsbytesr   r   r   readparse_itr   r   r   typerawheadersgetlistappend	TypeErrorr   r   lensplitr   r   r   r   r   )r"   r;   decodednot_decodedrheader_nameheader_contentrevised_headers	next_iterlist_of_headersheadcontentis_json_objentriesentry rN   V/home/thesage/.local/lib/python3.10/site-packages/niquests/_vendor/kiss_headers/api.pyr8      s~   
	


r8   r;   r   c              	   C  s   t  stdt }| D ]:}|j|v rqzt|jt  d }W n ty/   d||j< Y qw |jrB|jdd	 dd
 nd||j< q|S )	zT
    Return a brief explanation of each header present in headers if available.
    zPYou cannot use explain() function without properly importing the public package.r   zUnknown explanation.r)    z   zMissing docstring.)r   __subclasses__LookupErrorr   namer   r>   __doc__replacelstriprstrip)r;   explanationsheadertarget_classrN   rN   rO   explain   s&   

 
r\   targetHeaders | Headerdesired_outputtype[T]T | list[T] | Nonec                 C  s   t |tstd| dt|}t| tr"| |}|du r!dS n%t| tr>t| jt|kr;td| j	 d| d| }n	td| j	 dt|t
sQ||_	|S |D ]}||_	qS|S )ah  Experimental. Transform a Header or Headers object to its target `CustomHeader` subclass
    to access more ready-to-use methods. eg. You have a Header object named 'Set-Cookie' and you wish
    to extract the expiration date as a datetime.
    >>> header = Header("Set-Cookie", "1P_JAR=2020-03-16-21; expires=Wed, 15-Apr-2020 21:27:31 GMT")
    >>> header["expires"]
    'Wed, 15-Apr-2020 21:27:31 GMT'
    >>> from kiss_headers import SetCookie
    >>> set_cookie = get_polymorphic(header, SetCookie)
    >>> set_cookie.get_expire()
    datetime.datetime(2020, 4, 15, 21, 27, 31, tzinfo=datetime.timezone.utc)
    z6The desired output should be a subclass of Header not .Nz:The target class does not match the desired output class. z != z(Unable to apply get_polymorphic on type )
issubclassr   r>   r   r/   r   getr   rT   	__class__list)r]   r_   desired_output_header_namerC   rZ   rN   rN   rO   get_polymorphic   s.   





rh   kwargs
Any | Noner0   c                 K  s   t t| fi |S )N)
json_dumpsr   )r;   ri   rN   rN   rO   r      s   r   N)r"   r
   r#   r   )r;   r   r#   r   )r]   r^   r_   r`   r#   ra   )r;   r   ri   rj   r#   r0   ).
__future__r   copyr   email.messager   email.parserr   ior   r   jsonr   rk   r	   r3   typingr
   r   r   r   builderr   modelsr   r   
serializerr   r   
structuresr   utilsr   r   r   r   r   r   r   r   r   r   r   r8   r\   rh   rN   rN   rN   rO   <module>   s$    0

d
/