Skip to content

Commit 3204d73

Browse files
committed
Improve SharePoint login error handling with retry logic
1 parent 4aa9fb6 commit 3204d73

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/office365_service/sharepoint_service.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from office365.runtime.http.request_options import RequestOptions
1111

1212

13-
1413
def handle_sharepoint_errors(max_retries: int = 5, delay_seconds: int = 3):
1514
"""
1615
Decorador para tratar exceções de requisições do SharePoint, com lógica
@@ -99,15 +98,22 @@ def login(self, username, password):
9998
print(f"Fazendo login no SharePoint com o usuário {username}...")
10099
self.username = username
101100
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
111117

112118
@handle_sharepoint_errors()
113119
def obter_pasta(self, caminho_pasta: str) -> Folder | None:
@@ -273,4 +279,4 @@ def obter_pasta_por_nome(self, pasta_raiz: Folder, nome):
273279
def obter_arquivo_por_nome(self, pasta: Folder, nome):
274280
arquivos = list(self.listar_arquivos(pasta))
275281
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

Comments
 (0)