22from pydantic import BaseModel , Field
33import base64
44
5+ # Create a router for Base64 encoding/decoding routes
56router = APIRouter (prefix = "/tools/base64" , tags = ["Base64 Tools" ])
67
7- # Input model for encoding plain text
8+ # Request model for encoding input
89class Base64EncodeInput (BaseModel ):
910 content : str = Field (
1011 example = "hello world" ,
1112 description = "The plain text string to encode into Base64"
1213 )
1314
14- # Output model for encoded result
15+ # Response model for encoded result
1516class Base64EncodeResponse (BaseModel ):
1617 encoded : str = Field (
1718 example = "aGVsbG8gd29ybGQ=" ,
18- description = "The Base64-encoded string"
19+ description = "The Base64-encoded output string"
1920 )
2021
21- # Input model for decoding Base64 string
22+ # Request model for decoding input
2223class Base64DecodeInput (BaseModel ):
2324 encoded : str = Field (
2425 example = "aGVsbG8gd29ybGQ=" ,
2526 description = "The Base64-encoded string to decode"
2627 )
2728
28- # Output model for decoded result
29+ # Response model for decoded result
2930class Base64DecodeResponse (BaseModel ):
3031 decoded : str = Field (
3132 example = "hello world" ,
@@ -36,14 +37,17 @@ class Base64DecodeResponse(BaseModel):
3637 "/encode" ,
3738 response_model = Base64EncodeResponse ,
3839 summary = "Encode a string to Base64" ,
39- response_description = "Returns the Base64-encoded version of the input "
40+ response_description = "Returns the Base64-encoded result "
4041)
4142def encode_base64 (data : Base64EncodeInput ):
4243 """
43- Encode a plain text string into Base64.
44+ Encode a plain text string into Base64 format .
4445
45- Accepts a `content` field containing a UTF-8 string,
46- and returns its Base64-encoded version.
46+ Parameters:
47+ data (Base64EncodeInput): JSON object with a 'content' field.
48+
49+ Returns:
50+ Base64EncodeResponse: Encoded Base64 string.
4751 """
4852 try :
4953 encoded = base64 .b64encode (data .content .encode ("utf-8" )).decode ("utf-8" )
@@ -54,15 +58,18 @@ def encode_base64(data: Base64EncodeInput):
5458@router .post (
5559 "/decode" ,
5660 response_model = Base64DecodeResponse ,
57- summary = "Decode a Base64 string to plain text " ,
58- response_description = "Returns the decoded string "
61+ summary = "Decode a Base64 string" ,
62+ response_description = "Returns the decoded plain text result "
5963)
6064def decode_base64 (data : Base64DecodeInput ):
6165 """
62- Decode a Base64 string back into plain text.
66+ Decode a Base64 string into a plain UTF-8 string.
67+
68+ Parameters:
69+ data (Base64DecodeInput): JSON object with an 'encoded' field.
6370
64- Accepts an `encoded` field containing a Base64 string,
65- and returns the decoded UTF-8 string.
71+ Returns:
72+ Base64DecodeResponse: Decoded plain text string.
6673 """
6774 try :
6875 decoded_bytes = base64 .b64decode (data .encoded .encode ("utf-8" ))
0 commit comments