1- import base64
21import re
32from typing import TYPE_CHECKING
4- from typing import Annotated
5- from typing import Literal
63from typing import Union
74
8- from pydantic import EncodedBytes
9- from pydantic import EncoderProtocol
105from pydantic .alias_generators import to_snake
11- from pydantic_core import PydanticCustomError
126
137if TYPE_CHECKING :
148 from .base import BaseModel
@@ -29,57 +23,6 @@ def _int_to_str(status: int | None) -> str | None:
2923 return None if status is None else str (status )
3024
3125
32- # Copied from Pydantic 2.10 repository
33- class _Base64Encoder (EncoderProtocol ): # pragma: no cover
34- """Standard (non-URL-safe) Base64 encoder."""
35-
36- @classmethod
37- def decode (cls , data : bytes ) -> bytes :
38- """Decode the data from base64 encoded bytes to original bytes data.
39-
40- Args:
41- data: The data to decode.
42-
43- Returns:
44- The decoded data.
45-
46- """
47- try :
48- return base64 .b64decode (data )
49- except ValueError as e :
50- raise PydanticCustomError (
51- "base64_decode" , "Base64 decoding error: '{error}'" , {"error" : str (e )}
52- ) from e
53-
54- @classmethod
55- def encode (cls , value : bytes ) -> bytes :
56- """Encode the data from bytes to a base64 encoded bytes.
57-
58- Args:
59- value: The data to encode.
60-
61- Returns:
62- The encoded data.
63-
64- """
65- return base64 .b64encode (value )
66-
67- @classmethod
68- def get_json_format (cls ) -> Literal ["base64" ]:
69- """Get the JSON format for the encoded data.
70-
71- Returns:
72- The JSON format for the encoded data.
73-
74- """
75- return "base64"
76-
77-
78- # Compatibility with Pydantic <2.10
79- # https://pydantic.dev/articles/pydantic-v2-10-release#use-b64decode-and-b64encode-for-base64bytes-and-base64str-types
80- Base64Bytes = Annotated [bytes , EncodedBytes (encoder = _Base64Encoder )]
81-
82-
8326def _to_camel (string : str ) -> str :
8427 """Transform strings to camelCase.
8528
0 commit comments