
    Áj                        d dl mZ d dlZd dlmZmZmZmZ d dlm	Z	 g dZ
 G d de      Z ed      Zdd	Zdd
ZddZddddZddZy)    )annotationsN)AnyDictOptionalcast)	SecretStr)r   _NonObjectPayloadError_wrap_secret_fields_unwrap_secret_json_dumps_secrets_strip_tracebackc                  $     e Zd ZdZd fdZ xZS )r	   u  Raised by :func:`_wrap_secret_fields` for JSON payloads that are not
    objects. Carries only the payload's type name — a non-object payload can
    be an echo of the request (assertion included), so it must never bind in
    a caller's frame or ride along in an exception.
    c                8    t         |   d|        || _        y )Nzexpected a JSON object, got )super__init__	type_name)selfr   	__class__s     I/tmp/pip-target-k_scjv0h/lib/python/anthropic/lib/credentials/_secrets.pyr   z_NonObjectPayloadError.__init__   s    7	{CD"    )r   strreturnNone)__name__
__module____qualname____doc__r   __classcell__)r   s   @r   r	   r	      s    # #r   r	   )	typeversion
expires_at
token_type
expires_inscopeerrorerror_description	error_uric                   t        | t              s!t        |       j                  }~ t	        |      t        d|       }|D ]<  }|t        vst        |j                  |      t              s,t        ||         ||<   > |S )ui  Wrap the secret fields of a parsed JSON object in ``SecretStr``.

    Called at every boundary where credential material enters SDK code.
    Traceback frames retain their locals, so any dict a raise site (or a
    frame an error merely propagates through) still holds must already be
    redacted — ``SecretStr`` renders as ``SecretStr('**********')`` under
    crash reporters that capture and render locals.

    String values are secret unless their key is in ``_PLAIN_KEYS``; wrapped
    empty strings stay falsy (``SecretStr`` defines ``__len__`` across the
    supported pydantic range), so ``if not creds.get("access_token")`` checks
    behave unchanged. Only top-level values are wrapped — the credential
    formats are flat; revisit if a nested shape ever appears. Mutates
    ``payload`` in place — a copy would leave the raw-valued original
    reachable — and returns it.

    Non-object payloads raise :class:`_NonObjectPayloadError` from this frame,
    with the payload unbound first, so the raw value never lands in any frame
    of the traceback — callers translate to their own redacted error.
    Dict[str, Any])

isinstancedictr   r   r	   r   _PLAIN_KEYSgetr   r   )payloadr   mappingkeys       r   r
   r
   3   sz    * gt$M**	$Y//#W-G 3k!jS1A3&G$WS\2GCL3 Nr   c                F    t        | t              r| j                         S | S )zInverse of :func:`_wrap_secret_fields` for a single value; pass-through
    for values that were never wrapped (absent or non-string fields).)r*   r   get_secret_valuevalues    r   r   r   S   s!     (2%'C5!!#NNr   c                    t        | t              r| j                         S t        dt	        |       j
                   d      )NzObject of type z is not JSON serializable)r*   r   r2   	TypeErrorr   r   r3   s    r   _json_defaultr7   Y   s;    %#%%''
od5k&:&:%;;TU
VVr   )indentc               X    t        j                  | |t              j                  d      S )z``json.dumps`` with ``SecretStr`` values unwrapped at dump time.

    Returns bytes so call sites can pass the result inline (request content,
    ``os.write``) without binding the raw serialization to a local.
    )r8   defaultzutf-8)jsondumpsr7   encode)r.   r8   s     r   r   r   _   s"     ::gfmDKKGTTr   c                    d| _         | S )u  Detach the frames chained onto ``err`` before raising from it.

    Foreign frames (json decoder, httpx transport) hold raw payloads —
    request bodies, response text, credentials-file contents — as locals.
    Dropping the traceback removes them from every renderer and programmatic
    chain-walker, while the cause's type and message (which never carry the
    payload) stay visible in renderings.
    N)__traceback__)errs    r   r   r   h   s     CJr   )r.   r   r   r)   )r4   r   r   r   )r.   r   r8   zOptional[int]r   bytes)r@   BaseExceptionr   rB   )
__future__r   r;   typingr   r   r   r   pydanticr   __all__r6   r	   	frozensetr,   r
   r   r7   r   r    r   r   <module>rI      s\    "  , , 	#Y 	#  "@OW BF U
r   