Skip to content

Commit 1979746

Browse files
authored
Merge pull request #25 from green-api/dev
Some refactoring
2 parents b0ec3ab + a92ece5 commit 1979746

File tree

12 files changed

+516
-427
lines changed

12 files changed

+516
-427
lines changed

tests/test_methods.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import unittest
2-
from unittest.mock import patch, Mock
2+
from unittest.mock import Mock, patch
33

44
from whatsapp_api_client_python.API import GreenApi
55

66
api = GreenApi("", "")
77

8+
path = "examples/data/green-api-logo_2.png"
9+
810

911
class MethodsTestCase(unittest.TestCase):
10-
@patch("whatsapp_api_client_python.API.requests.request")
12+
@patch("whatsapp_api_client_python.API.Session.request")
1113
def test_methods(self, mock_request):
1214
mock_request.return_value = Mock(
1315
status_code=200, text="{\"example\": {\"key\": \"value\"}}"
@@ -41,7 +43,7 @@ def account_methods(self):
4143
api.account.reboot(),
4244
api.account.logout(),
4345
api.account.qr(),
44-
# api.account.setProfilePicture("")
46+
api.account.setProfilePicture(path)
4547
]
4648

4749
@property
@@ -58,7 +60,7 @@ def group_methods(self):
5860
api.groups.removeGroupParticipant("", ""),
5961
api.groups.setGroupAdmin("", ""),
6062
api.groups.removeAdmin("", ""),
61-
# api.groups.setGroupPicture("", ""),
63+
api.groups.setGroupPicture("", path),
6264
api.groups.leaveGroup("")
6365
]
6466

@@ -80,14 +82,14 @@ def queue_methods(self):
8082

8183
@property
8284
def read_mark_methods(self):
83-
return [api.marking.readChat("", "")]
85+
return [api.marking.readChat("")]
8486

8587
@property
8688
def receiving_methods(self):
8789
return [
8890
api.receiving.receiveNotification(),
8991
api.receiving.deleteNotification(0),
90-
api.receiving.downloadFile("")
92+
api.receiving.downloadFile("", "")
9193
]
9294

9395
@property
@@ -97,7 +99,7 @@ def sending_methods(self):
9799
api.sending.sendButtons("", "", []),
98100
api.sending.sendTemplateButtons("", "", []),
99101
api.sending.sendListMessage("", "", "", []),
100-
# api.sending.sendFileByUpload("", ""),
102+
api.sending.sendFileByUpload("", path),
101103
api.sending.sendFileByUrl("", "", ""),
102104
api.sending.sendLocation("", 0.0, 0.0),
103105
api.sending.sendContact("", {}),
@@ -115,7 +117,7 @@ def service_methods(self):
115117
api.serviceMethods.deleteMessage("", ""),
116118
api.serviceMethods.archiveChat(""),
117119
api.serviceMethods.unarchiveChat(""),
118-
api.serviceMethods.setDisappearingChat("", 0)
120+
api.serviceMethods.setDisappearingChat("")
119121
]
120122

121123

whatsapp_api_client_python/API.py

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,69 @@
1-
from array import array
2-
import requests
3-
import json
4-
from whatsapp_api_client_python.response import Response
1+
from typing import Optional
52

6-
from whatsapp_api_client_python.tools.account import Account
7-
from whatsapp_api_client_python.tools.device import Device
8-
from whatsapp_api_client_python.tools.groups import Groups
9-
from whatsapp_api_client_python.tools.journals import Journals
10-
from whatsapp_api_client_python.tools.marking import Marking
11-
from whatsapp_api_client_python.tools.queues import Queues
12-
from whatsapp_api_client_python.tools.receiving import Receiving
13-
from whatsapp_api_client_python.tools.sending import Sending
14-
from whatsapp_api_client_python.tools.serviceMethods import ServiceMethods
15-
from whatsapp_api_client_python.tools.webhooks import Webhooks
3+
from requests import Session
164

5+
from .response import Response
6+
from .tools import (
7+
account,
8+
device,
9+
groups,
10+
journals,
11+
marking,
12+
queues,
13+
receiving,
14+
sending,
15+
serviceMethods,
16+
webhooks
17+
)
1718

18-
class GreenApi:
19-
'REST API class'
2019

20+
class GreenApi:
2121
host: str
2222
idInstance: str
2323
apiTokenInstance: str
2424

25-
def __init__(self,
26-
idInstance: str,
27-
apiTokenInstance: str,
28-
host: str = 'https://api.green-api.com') -> None:
25+
def __init__(
26+
self,
27+
idInstance: str,
28+
apiTokenInstance: str,
29+
host: str = "https://api.green-api.com"
30+
):
2931
self.host = host
3032
self.idInstance = idInstance
3133
self.apiTokenInstance = apiTokenInstance
3234

33-
self.account = Account(self)
34-
self.device = Device(self)
35-
self.groups = Groups(self)
36-
self.journals = Journals(self)
37-
self.marking = Marking(self)
38-
self.queues = Queues(self)
39-
self.receiving = Receiving(self)
40-
self.sending = Sending(self)
41-
self.serviceMethods = ServiceMethods(self)
42-
self.webhooks = Webhooks(self)
35+
self.account = account.Account(self)
36+
self.device = device.Device(self)
37+
self.groups = groups.Groups(self)
38+
self.journals = journals.Journals(self)
39+
self.marking = marking.Marking(self)
40+
self.queues = queues.Queues(self)
41+
self.receiving = receiving.Receiving(self)
42+
self.sending = sending.Sending(self)
43+
self.serviceMethods = serviceMethods.ServiceMethods(self)
44+
self.webhooks = webhooks.Webhooks(self)
45+
46+
def request(
47+
self,
48+
method: str,
49+
url: str,
50+
payload: Optional[dict] = None,
51+
files: Optional[dict] = None
52+
) -> Response:
53+
url = url.replace("{{host}}", self.host)
54+
url = url.replace("{{idInstance}}", self.idInstance)
55+
url = url.replace("{{apiTokenInstance}}", self.apiTokenInstance)
4356

44-
def request(self, method: str, url: str,
45-
payload: any = None, files: array = None):
46-
url = url.replace('{{host}}', self.host)
47-
url = url.replace('{{idInstance}}', self.idInstance)
48-
url = url.replace('{{apiTokenInstance}}', self.apiTokenInstance)
49-
status_code = 0
50-
text = ''
5157
try:
52-
headers = {}
53-
payloadData = None
54-
if payload is not None:
55-
if files is None:
56-
headers = {
57-
'Content-Type': 'application/json'
58-
}
59-
payloadData = json.dumps(payload)
58+
with Session() as session:
59+
if not files:
60+
response = session.request(
61+
method=method, url=url, json=payload
62+
)
6063
else:
61-
payloadData = payload
62-
result = requests.request(method, url, headers = headers,
63-
data = payloadData,
64-
files = files)
65-
status_code = result.status_code
66-
text = result.text
67-
result.raise_for_status()
68-
except requests.HTTPError:
69-
return Response(status_code, text)
70-
except Exception as err:
71-
status_code = 0
72-
text = f'Other error occurred: {err}'
73-
return Response(status_code, text)
64+
response = session.request(
65+
method=method, url=url, data=payload, files=files
66+
)
67+
except Exception as error:
68+
return Response(None, f"Other error occurred: {error}.")
69+
return Response(response.status_code, response.text)
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import json
1+
from json import loads
2+
from typing import Optional
23

34

45
class Response:
5-
code: int
6-
data: json
7-
error: str
6+
code: Optional[int]
7+
data: Optional[dict] = None
8+
error: Optional[str] = None
89

9-
def __init__(self, code: int, text: str) -> None:
10+
def __init__(self, code: Optional[int], text: str):
1011
self.code = code
11-
if code == 200:
12-
self.data = json.loads(text)
13-
self.error = None
12+
if self.code == 200:
13+
self.data = loads(text)
1414
else:
1515
self.error = text
16-
self.data = None
Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
from whatsapp_api_client_python.response import Response
1+
from typing import TYPE_CHECKING
2+
3+
from ..response import Response
4+
5+
if TYPE_CHECKING:
6+
from ..API import GreenApi
27

38

49
class Device:
5-
def __init__(self, greenApi) -> None:
6-
self.greenApi = greenApi
7-
10+
def __init__(self, api: "GreenApi"):
11+
self.api = api
12+
813
def getDeviceInfo(self) -> Response:
9-
'The method is aimed for getting information about the device '\
10-
'(phone) running WhatsApp Business application.'
11-
12-
return self.greenApi.request('GET',
13-
'{{host}}/waInstance{{idInstance}}'
14-
'/GetDeviceInfo/{{apiTokenInstance}}')
14+
"""
15+
The method is aimed for getting information about the device
16+
(phone) running WhatsApp Business application.
17+
"""
18+
19+
return self.api.request(
20+
"GET", (
21+
"{{host}}/waInstance{{idInstance}}/"
22+
"getDeviceInfo/{{apiTokenInstance}}"
23+
)
24+
)

0 commit comments

Comments
 (0)