1212# limitations under the License.
1313# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
1414
15- import base64
1615import os
1716import re
18- from email import encoders
19- from email .mime .base import MIMEBase
20- from email .mime .multipart import MIMEMultipart
21- from email .mime .text import MIMEText
22- from typing import TYPE_CHECKING , Any , Dict , List , Optional , Union
17+ from typing import TYPE_CHECKING , Any , Dict , List , Literal , Optional , Union
2318
2419if TYPE_CHECKING :
2520 from googleapiclient .discovery import Resource
@@ -65,6 +60,9 @@ def __init__(
6560 (default: :obj:`None`)
6661 """
6762 super ().__init__ (timeout = timeout )
63+
64+ self ._credentials = self ._authenticate ()
65+
6866 self .gmail_service : Any = self ._get_gmail_service ()
6967 self .people_service : Any = self ._get_people_service ()
7068
@@ -614,6 +612,8 @@ def get_attachment(
614612 save result.
615613 """
616614 try :
615+ import base64
616+
617617 attachment = (
618618 self .gmail_service .users ()
619619 .messages ()
@@ -785,8 +785,8 @@ def list_gmail_labels(self) -> Dict[str, Any]:
785785 def create_label (
786786 self ,
787787 name : str ,
788- label_list_visibility : str = "labelShow" ,
789- message_list_visibility : str = "show" ,
788+ label_list_visibility : Literal [ "labelShow" , "labelHide" ] = "labelShow" ,
789+ message_list_visibility : Literal [ "show" , "hide" ] = "show" ,
790790 ) -> Dict [str , Any ]:
791791 r"""Create a new Gmail label.
792792
@@ -1034,8 +1034,7 @@ def _get_gmail_service(self):
10341034 from googleapiclient .discovery import build
10351035
10361036 try :
1037- creds = self ._authenticate ()
1038- service = build ('gmail' , 'v1' , credentials = creds )
1037+ service = build ('gmail' , 'v1' , credentials = self ._credentials )
10391038 return service
10401039 except Exception as e :
10411040 raise ValueError (f"Failed to build Gmail service: { e } " ) from e
@@ -1045,8 +1044,7 @@ def _get_people_service(self):
10451044 from googleapiclient .discovery import build
10461045
10471046 try :
1048- creds = self ._authenticate ()
1049- service = build ('people' , 'v1' , credentials = creds )
1047+ service = build ('people' , 'v1' , credentials = self ._credentials )
10501048 return service
10511049 except Exception as e :
10521050 raise ValueError (f"Failed to build People service: { e } " ) from e
@@ -1113,6 +1111,13 @@ def _create_message(
11131111 is_html : bool = False ,
11141112 ) -> Dict [str , str ]:
11151113 r"""Create a message object for sending."""
1114+
1115+ import base64
1116+ from email import encoders
1117+ from email .mime .base import MIMEBase
1118+ from email .mime .multipart import MIMEMultipart
1119+ from email .mime .text import MIMEText
1120+
11161121 message = MIMEMultipart ()
11171122 message ['to' ] = ', ' .join (to_list )
11181123 message ['subject' ] = subject
@@ -1192,6 +1197,8 @@ def _get_header_value(
11921197
11931198 def _extract_message_body (self , message : Dict [str , Any ]) -> str :
11941199 r"""Extract message body from message payload."""
1200+ import base64
1201+
11951202 payload = message .get ('payload' , {})
11961203
11971204 # Handle multipart messages
0 commit comments