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 )
0 commit comments