Skip to main content

The simplest zero dependency polyversion Python library for Bitrix24 REST API.

Project description

pybitrix24

The simplest zero dependency polyversion Python library for Bitrix24 REST API.

Features

  • Polyversion. Supported Python versions: 2.7, 3.5+.
  • Zero dependency. It's fast, lightweight and secure.
  • Reliable. Test coverage is more than 80%.
  • Just simple. Examples of usage and clear sources.

Installation

Install using pip:

$ pip install pybitrix24

Getting started

Preparation

The current section and next one can be skipped if only webhooks will be used.

To start making requests it's necessary to create an application in the marketplace first. Then create an instance of the main class with the minimum required configuration that contains hostname, client ID and secret arguments (hereafter placeholders prefixed with "my" will be used instead of real values):

>>> from pybitrix24 import Bitrix24
>>> bx24 = Bitrix24('my-subdomain.bitrix24.com', 'my.client.id', 'MyClientSecret')

Now is the time to authorize.

Bitrix24 uses OAuth2 and authorization code grant for authorization of applications. It means that account owner's credentials are hidden from developers for security reasons, therefore, it's not possible to obtain authorization code programmatically. The account owner should be always present when access is granted.

However, to make life a bit simpler there is a helper method that builds an authorization URL for requesting an authorization code:

>>> bx24.build_authorization_url()
'https://my-subdomain.bitrix24.com/oauth/authorize/?client_id=my.client.id&response_type=code'

Finally, when an authorization code is received both access and refresh tokens can be obtained:

>>> bx24.obtain_tokens('AnAuthorizationCode')
{'access_token': 'AnAccessToken', 'refresh_token': 'ARefreshToken', ...}

As it was mentioned earlier it's not possible to get the authorization code automatically but it's possible to refresh tokens after initial receiving to make the session longer (note that both tokens have 1 hour lifetime after that they'll be expired and an authorization code must be granted again):

>>> bx24.refresh_tokens()
{'access_token': 'ANewAccessToken', 'refresh_token': 'ANewRefreshToken', ...}

Congratulations, all the preparatory work is done!

Requesting resources with an access token

A further turn for requesting Bitrix24 resources. An access token injects automatically for all methods prefixed with call_ that are mentioned in this section.

To make a single call (this example requires the following permissions: user):

>>> bx24.call('user.get', {'ID': 1})
{'result': {...}}

To make a batch call that is a few calls per request (this example requires the following permissions: user,department):

>>> bx24.call_batch({
...     'get_user': ('user.current', {}), # or 'user.current'
...     'get_department': {
...         'method': 'department.get',
...         'params': {'ID': '$result[get_user][UF_DEPARTMENT]'}
...     }
... })
{'result': {'result': {...}}}

To bind an event (this method calls event.bind under the hood):

>>> bx24.call_event_bind('OnAppUpdate', 'https://example.com/')
{'result': {...}}

To unbind an event (this method calls event.unbind under the hood):

>>> bx24.call_event_unbind('OnAppUpdate', 'https://example.com/')
{'result': {...}}

Requesting resources with a webhook code

Requesting resources with an authorization code is suitable for development of 3rd-party applications that are often quite cumbersome. However, sometimes it's enough to send a few simple calls. This is where webhooks come to action.

If only webhooks are used the minimum required configuration is as simple as the following (use user_id argument if you need to make webhook calls on behalf of another user, by default 1 is used):

>>> from pybitrix24 import Bitrix24
>>> bx24 = Bitrix24('my-subdomain.bitrix24.com')

To make an inbound webhook call (this example requires the following permissions: user):

>>> bx24.call_webhook('xxxxxxxxxxxxxxxx', 'user.get', {'ID': 1})
{'result': {...}}

To make a batch call of inbound webhooks (this example requires the following permissions: user,department):

>>> bx24.call_batch_webhook('xxxxxxxxxxxxxxxx', {
...     'get_user': ('user.current', {}), # or 'user.current'
...     'get_department': {
...         'method': 'department.get',
...         'params': {'ID': '$result[get_user][UF_DEPARTMENT]'}
...     }
... })
{'result': {'result': {...}}}

That's the end of the quick introduction. Thanks!

For more details, please, explore source code or ask me. Good luck!

Copyright and License

Copyright © 2017-2020 Yurii Rabeshko. Code released under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pybitrix24-1.1.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file pybitrix24-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pybitrix24-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

File hashes

Hashes for pybitrix24-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67a0949941223e27936e2cb4d04c486a38d0e3086379e6cb547381284bbe3946
MD5 38a82e84e799b4d60fe19f2e37114e07
BLAKE2b-256 8dda4fcefc05ba312b129259486bfde983ae8a5e7f84849023cff732922b72c1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page