Skip to content

Commit

Permalink
Merge pull request #30 from green-api/SW-2065
Browse files Browse the repository at this point in the history
Added Retry
  • Loading branch information
Amele9 authored Aug 24, 2023
2 parents 4185a5b + 590df2f commit c99bfc0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="whatsapp-api-client-python",
version="0.0.40",
version="0.0.41",
description=(
"This library helps you easily create"
" a Python application with WhatsApp API."
Expand Down
32 changes: 23 additions & 9 deletions whatsapp_api_client_python/API.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional

from requests import Session
from requests.adapters import HTTPAdapter, Retry

from .response import Response
from .tools import (
Expand Down Expand Up @@ -35,6 +36,9 @@ def __init__(
self.idInstance = idInstance
self.apiTokenInstance = apiTokenInstance

self.session = Session()
self.__prepare_session()

self.account = account.Account(self)
self.device = device.Device(self)
self.groups = groups.Groups(self)
Expand All @@ -59,15 +63,25 @@ def request(
url = url.replace("{{apiTokenInstance}}", self.apiTokenInstance)

try:
with Session() as session:
if not files:
response = session.request(
method=method, url=url, json=payload
)
else:
response = session.request(
method=method, url=url, data=payload, files=files
)
if not files:
response = self.session.request(
method=method, url=url, json=payload
)
else:
response = self.session.request(
method=method, url=url, data=payload, files=files
)
except Exception as error:
return Response(None, f"Other error occurred: {error}.")
return Response(response.status_code, response.text)

def __prepare_session(self) -> None:
retry = Retry(
total=3,
backoff_factor=1.0,
allowed_methods=None,
status_forcelist=[400, 429]
)

self.session.mount("http://", HTTPAdapter(max_retries=retry))
self.session.mount("https://", HTTPAdapter(max_retries=retry))

0 comments on commit c99bfc0

Please sign in to comment.