10
10
from office365 .runtime .http .request_options import RequestOptions
11
11
12
12
13
-
14
13
def handle_sharepoint_errors (max_retries : int = 5 , delay_seconds : int = 3 ):
15
14
"""
16
15
Decorador para tratar exceções de requisições do SharePoint, com lógica
@@ -99,15 +98,22 @@ def login(self, username, password):
99
98
print (f"Fazendo login no SharePoint com o usuário { username } ..." )
100
99
self .username = username
101
100
self .password = password
102
- try :
103
- self .ctx .with_credentials (UserCredential (self .username , self .password ))
104
- self .ctx .load (self .ctx .web )
105
- self .ctx .execute_query ()
106
- print ("Login realizado com sucesso." )
107
- return True
108
- except ClientRequestException as e :
109
- print (f"Erro ao fazer login: { e } " )
110
- return False
101
+ for attempt in range (3 ):
102
+ try :
103
+ self .ctx .clear ()
104
+ self .ctx .with_credentials (UserCredential (username , password ))
105
+ self .ctx .load (self .ctx .web )
106
+ self .ctx .execute_query ()
107
+ print ("Login realizado com sucesso." )
108
+ return True
109
+ except ClientRequestException as e :
110
+ print (f"Erro ao fazer login: { e } " )
111
+ if attempt < 2 :
112
+ print (f"Tentando novamente (tentativa { attempt + 1 } /3)..." )
113
+ time .sleep (3 )
114
+ else :
115
+ print ("Falha após 3 tentativas. Abortando." )
116
+ return False
111
117
112
118
@handle_sharepoint_errors ()
113
119
def obter_pasta (self , caminho_pasta : str ) -> Folder | None :
@@ -273,4 +279,4 @@ def obter_pasta_por_nome(self, pasta_raiz: Folder, nome):
273
279
def obter_arquivo_por_nome (self , pasta : Folder , nome ):
274
280
arquivos = list (self .listar_arquivos (pasta ))
275
281
arquivo_encontrado = next ((arquivo for arquivo in arquivos if nome in arquivo .name ), None )
276
- return arquivo_encontrado
282
+ return arquivo_encontrado
0 commit comments