Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Direct host #72

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions whatsapp_api_client_python/API.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ class GreenApi:
media: str
idInstance: str
apiTokenInstance: str
useDirectHost: bool

def __init__(
self,
idInstance: str,
apiTokenInstance: str,
debug_mode: bool = False,
raise_errors: bool = False,
useDirectHost: bool = False,
host: str = "https://api.green-api.com",
media: str = "https://media.green-api.com",
host_timeout: float = 180, # sec per retry
Expand All @@ -43,8 +45,10 @@ def __init__(
self.media = media
self.debug_mode = debug_mode
self.raise_errors = raise_errors
self.useDirectHost = useDirectHost
self.host, self.media = self._determine_host(idInstance) if self.useDirectHost else (host, media)

# Change default values in init() if required
# Change default values in init() if required (slow connection)
self.host_timeout = host_timeout
self.media_timeout = media_timeout

Expand All @@ -69,6 +73,18 @@ def __init__(
self.logger = logging.getLogger("whatsapp-api-client-python")
self.__prepare_logger()

def _determine_host(self, idInstance: str) -> str:
mapping = {
"1103": ("https://1103.api.green-api.com", "https://1103.media.green-api.com"),
"1104": ("https://1103.api.green-api.com", "https://1103.media.green-api.com"),
"5700": ("https://5700.api.green-api.com", "https://5700.media.green-api.com"),
"7103": ("https://7103.api.greenapi.com", "https://7103.media.greenapi.com"),
"7105": ("https://7105.api.greenapi.com", "https://7105.media.greenapi.com"),
"77": ("https://7700.api.greenapi.com", "https://7700.media.greenapi.com"),
"7": ("https://api.greenapi.com", "https://media.greenapi.com")
}
return next((url for key, url in mapping.items() if idInstance.startswith(key)), ("https://api.green-api.com", "https://media.green-api.com"))

def request(
self,
method: str,
Expand Down Expand Up @@ -213,4 +229,4 @@ def request(

url = url.replace("{{partnerToken}}", self.partnerToken)

return super().request(method, url, payload, files)
return super().request(method, url, payload, files)