Skip to content

Commit d640212

Browse files
authored
Merge pull request #20 from Ivansstyle/dev
Add possibility to use unverified ssl connection for testing and add 403 code and html response check
2 parents 795ce60 + fadce18 commit d640212

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

bitrix24/bitrix24.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
:copyright: (c) 2019 by Akop Kesheshyan.
1010
1111
"""
12+
import warnings
13+
1214
import requests
1315
from time import sleep
1416
from urllib.parse import urlparse
@@ -27,13 +29,14 @@ class Bitrix24(object):
2729
>>> bx24.callMethod('crm.product.list')
2830
"""
2931

30-
def __init__(self, domain, timeout=60):
32+
def __init__(self, domain, timeout=60, safe=True):
3133
"""Create Bitrix24 API object
3234
:param domain: str Bitrix24 webhook domain
3335
:param timeout: int Timeout for API request in seconds
3436
"""
3537
self.domain = self._prepare_domain(domain)
3638
self.timeout = timeout
39+
self.safe = safe
3740

3841
def _prepare_domain(self, domain):
3942
"""Normalize user passed domain to a valid one."""
@@ -85,9 +88,21 @@ def callMethod(self, method, **params):
8588
p = self._prepare_params(params)
8689

8790
if method.rsplit('.', 1)[0] in ['add', 'update', 'delete', 'set']:
88-
r = requests.post(url, data=p, timeout=self.timeout).json()
91+
r = requests.post(url, data=p, timeout=self.timeout, verify=self.safe).json()
8992
else:
90-
r = requests.get(url, params=p, timeout=self.timeout).json()
93+
r = requests.get(url, params=p, timeout=self.timeout, verify=self.safe)
94+
try:
95+
r = r.json()
96+
except requests.exceptions.JSONDecodeError as e:
97+
warnings.warn("bitrix24: JSON decode error...")
98+
if r.status_code == 403:
99+
warnings.warn(f"bitrix24: Forbidden: {method}. Check your bitrix24 webhook settings. Returning None! ")
100+
return None
101+
elif r.ok:
102+
return r.content
103+
104+
105+
91106
except ValueError:
92107
if r['error'] not in 'QUERY_LIMIT_EXCEEDED':
93108
raise BitrixError(r)

0 commit comments

Comments
 (0)