Skip to content

Commit fb17f13

Browse files
Akop KesheshyanAkop Kesheshyan
authored andcommitted
init
0 parents  commit fb17f13

File tree

8 files changed

+387
-0
lines changed

8 files changed

+387
-0
lines changed

.gitignore

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
.pytest_cache/
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
db.sqlite3
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
78+
# celery beat schedule file
79+
celerybeat-schedule
80+
81+
# SageMath parsed files
82+
*.sage.py
83+
84+
# Environments
85+
.env
86+
.venv
87+
env/
88+
venv/
89+
ENV/
90+
env.bak/
91+
venv.bak/
92+
93+
# Spyder project settings
94+
.spyderproject
95+
.spyproject
96+
97+
# Rope project settings
98+
.ropeproject
99+
100+
# mkdocs documentation
101+
/site
102+
103+
# mypy
104+
.mypy_cache/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Akop Kesheshyan
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

MANIFEST.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include LICENSE
2+
include bitrix24
3+
<<<<<<< HEAD
4+
include README.rst
5+
=======
6+
include README.md
7+
>>>>>>> a4dbdba3abc7bbb6024b32a1383cf6d7552a94c4

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Bitrix24 REST API for Python
2+
3+
Easy way to communicate with bitrix24 portal over REST without OAuth 2.0
4+
5+
## Description
6+
7+
Bitrix24-python-rest is a simple API wrapper for working with Bitrix24
8+
REST API over webhooks.
9+
10+
## Features
11+
12+
- Works both with cloud and on-premises versions of bitrix24, much more
13+
- Super easy for setting up. No OAuth implemetation required
14+
- Compatible with latests Bitrix24 REST API
15+
16+
## Requirements
17+
- Python 2.6+ or 3.2+
18+
- requests
19+
20+
## Installation
21+
```
22+
pip install bitrix24-python-rest
23+
```
24+
25+
## Quickstart
26+
27+
```python
28+
from bitrix24 import *
29+
30+
bx24 = Bitrix24('https://example.bitrix24.com/rest/1/33olqeits4avuyqu')
31+
32+
print(bx24.callMethod('crm.product.list'))
33+
```
34+
35+
## Advanced usage
36+
37+
You can define filters and additional parameters in any order:
38+
39+
```python
40+
bx24.callMethod('crm.deal.list',
41+
order={'STAGE_ID': 'ASC'},
42+
filter={'>PROBABILITY': 50},
43+
select=['ID', 'TITLE', 'STAGE_ID', 'PROBABILITY'])
44+
```
45+
46+
Catch the server error with exception:
47+
48+
```python
49+
try:
50+
bx24.callMethod('tasks.task.add', fields={'TITLE': 'task for test', 'RESPONSIBLE_ID': 1})
51+
except BitrixError as message:
52+
print(message)
53+
```
54+
55+
## Notes
56+
List methods return all available items at once. For large collections
57+
of data use limits.
58+
59+
## Author
60+
61+
Akop Kesheshyan - <akop.kesheshyan@icloud.com>
62+
63+
New contributers and pull requests are welcome.

bitrix24/__init__.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# ____ _ _ _ ____ _ _ ____ _____ ____ _____
4+
# | __ )(_) |_ _ __(_)_ _|___ \| || | | _ \| ____/ ___|_ _|
5+
# | _ \| | __| '__| \ \/ / __) | || |_ | |_) | _| \___ \ | |
6+
# | |_) | | |_| | | |> < / __/|__ _| | _ <| |___ ___) || |
7+
# |____/|_|\__|_| |_/_/\_\_____| |_| |_| \_\_____|____/ |_|
8+
9+
10+
"""
11+
Bitrix24 REST library
12+
~~~~~~~~~~~~~~~~~~~~~
13+
14+
Bitrix24 REST provides easy way to communicate with bitrix24 portal over REST without OAuth 2.0.
15+
usage:
16+
17+
>>> from bitrix24 import Bitrix24
18+
>>> bx24 = Bitrix24('https://example.bitrix24.com/rest/1/33olqeits4avuyqu')
19+
>>> r = bx24.callMethod('crm.product.list')
20+
21+
Copyright (c) 2019 by Akop Kesheshyan.
22+
"""
23+
24+
__version__ = '1.0.1'
25+
__author__ = 'Akop Kesheshyan <akop.kesheshyan@icloud.com>'
26+
__license__ = 'MIT'
27+
__copyright__ = 'Copyright 2019 Akop Kesheshyan'
28+
29+
from .bitrix24 import Bitrix24
30+
from .exceptions import BitrixError

bitrix24/bitrix24.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
Bitrix24
5+
~~~~~~~~~~~~
6+
7+
This module implements the Bitrix24 REST API.
8+
9+
:copyright: (c) 2019 by Akop Kesheshyan.
10+
11+
"""
12+
import requests
13+
from time import sleep
14+
from .exceptions import BitrixError
15+
16+
17+
class Bitrix24(object):
18+
"""A user-created :class:`Bitrix24 <Bitrix24>` object.
19+
Used to sent to the server.
20+
21+
:param domain: REST call domain, including account name, user ID and secret code.
22+
:param timeout: (Optional) waiting for a response after a given number of seconds.
23+
Usage::
24+
>>> from bitrix24 import Bitrix24
25+
>>> bx24 = Bitrix24('https://example.bitrix24.com/rest/1/33olqeits4avuyqu')
26+
>>> bx24.callMethod('crm.product.list')
27+
"""
28+
29+
def __init__(self, domain, timeout=60):
30+
self.domain = domain
31+
self.timeout = timeout
32+
33+
def _prepare_params(self, params):
34+
"""Transforms list of params to a valid bitrix array."""
35+
36+
new_params = {}
37+
for index, value in params.items():
38+
if type(value) is dict:
39+
for i, v in value.items():
40+
new_params['%s[%s]' % (index, i)] = v
41+
else:
42+
new_params[index] = value
43+
return new_params
44+
45+
def callMethod(self, method, **params):
46+
"""Calls a REST method with specified parameters.
47+
48+
:param url: REST method name.
49+
:param \*\*params: Optional arguments which will be converted to a POST request string.
50+
:return: Returning the REST method response as an array, an object or a scalar
51+
"""
52+
53+
try:
54+
url = '{0}/{1}.json'.format(self.domain, method)
55+
56+
p = self._prepare_params(params)
57+
58+
if method.rsplit('.', 1)[0] in ['add', 'update', 'delete', 'set']:
59+
r = requests.post(url, data=p, timeout=self.timeout).json()
60+
else:
61+
r = requests.get(url, params=p, timeout=self.timeout).json()
62+
except ValueError:
63+
if r['error'] not in 'QUERY_LIMIT_EXCEEDED':
64+
raise BitrixError(r)
65+
# Looks like we need to wait until expires limitation time by Bitrix24 API
66+
sleep(2)
67+
return self.callMethod(method, **params)
68+
69+
if 'error' in r:
70+
raise BitrixError(r)
71+
if 'page' not in params:
72+
params['page'] = 1
73+
if 'next' in r and r['total'] > (r['next']*params['page']):
74+
params['page'] += 1
75+
return r['result'] + self.callMethod(method, **params)
76+
return r['result']

bitrix24/exceptions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
Bitrix24.exceptions
5+
~~~~~~~~~~~~~~~~~~~
6+
7+
This module contains the set of Bitrix24 REST exceptions.
8+
9+
:copyright: (c) 2019 by Akop Kesheshyan.
10+
11+
"""
12+
13+
14+
class BitrixError(ValueError):
15+
def __init__(self, response):
16+
super().__init__(response['error_description'])
17+
self.code = response['error']

setup.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# ____ _ _ _ ____ _ _ ____ _____ ____ _____
5+
# | __ )(_) |_ _ __(_)_ _|___ \| || | | _ \| ____/ ___|_ _|
6+
# | _ \| | __| '__| \ \/ / __) | || |_ | |_) | _| \___ \ | |
7+
# | |_) | | |_| | | |> < / __/|__ _| | _ <| |___ ___) || |
8+
# |____/|_|\__|_| |_/_/\_\_____| |_| |_| \_\_____|____/ |_|
9+
10+
"""
11+
Setup file for Bitrix24 REST API
12+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13+
14+
Copyright (c) 2019 by Akop Kesheshyan.
15+
"""
16+
17+
from distutils.core import setup
18+
from setuptools import find_packages
19+
20+
# read the contents of your README file
21+
from os import path
22+
this_directory = path.abspath(path.dirname(__file__))
23+
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
24+
long_description = f.read()
25+
26+
setup(
27+
name='bitrix24-python-rest',
28+
version='1.0.0',
29+
install_requires=['requests'],
30+
packages=find_packages(),
31+
url='https://github.com/akopkesheshyan/bitrix24-python-rest',
32+
license='MIT',
33+
author='Akop Kesheshyan',
34+
author_email='akop.kesheshyan@icloud.com',
35+
description='Bitrix24 REST API wrapper provides easy way to communicate with bitrix24 portal over REST without OAuth',
36+
long_description=long_description,
37+
long_description_content_type='text/markdown',
38+
keywords='bitrix24 api rest',
39+
classifiers=[
40+
# How mature is this project? Common values are
41+
# 3 - Alpha
42+
# 4 - Beta
43+
# 5 - Production/Stable
44+
'Development Status :: 5 - Production/Stable',
45+
46+
# Indicate who your project is intended for
47+
'Intended Audience :: Developers',
48+
'Natural Language :: Russian',
49+
'Natural Language :: English',
50+
'Operating System :: OS Independent',
51+
'Topic :: Software Development :: Libraries :: Python Modules',
52+
53+
# Pick your license as you wish (should match "license" above)
54+
'License :: OSI Approved :: MIT License',
55+
56+
# Specify the Python versions you support here. In particular, ensure
57+
# that you indicate whether you support Python 2, Python 3 or both.
58+
'Programming Language :: Python :: 2',
59+
'Programming Language :: Python :: 2.6',
60+
'Programming Language :: Python :: 2.7',
61+
'Programming Language :: Python :: 3',
62+
'Programming Language :: Python :: 3.2',
63+
'Programming Language :: Python :: 3.3',
64+
'Programming Language :: Python :: 3.4',
65+
'Programming Language :: Python :: 3.5',
66+
'Programming Language :: Python :: 3.6',
67+
'Programming Language :: Python :: 3.7',
68+
],
69+
)

0 commit comments

Comments
 (0)