Merge pull request #4 from gwinn/master

Support for v3, v4 & v5 API versions
This commit is contained in:
Alex Lushpai 2018-03-20 20:47:10 +03:00 committed by GitHub
commit 5e154a2f02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 2664 additions and 666 deletions

3
.gitignore vendored
View File

@ -1,4 +1,7 @@
*.swp *.swp
*.pyc *.pyc
*.nose*
/*.egg-info /*.egg-info
/dist/ /dist/
/venv/
/.vscode/

9
.travis.yml Normal file
View File

@ -0,0 +1,9 @@
language: python
python:
- '3.4'
- '3.5'
- '3.6'
before_install:
- pip install -r requirements.txt
script:
- nosetests -v

View File

@ -1,6 +1,6 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2015 RetailDriver LLC Copyright (c) 2015-2018 RetailDriver LLC
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -1,31 +1,80 @@
retailCRM API python client [![Build Status](https://img.shields.io/travis/retailcrm/api-client-python/master.svg?style=flat-square)](https://travis-ci.org/retailcrm/api-client-python)
[![PyPI](https://img.shields.io/pypi/v/retailcrm.svg?style=flat-square)](https://pypi.python.org/pypi/retailcrm)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/retailcrm.svg?style=flat-square)](https://pypi.python.org/pypi/retailcrm)
retailCRM python API client
=========================== ===========================
### Install This is python retailCRM API client. This library allows to use all available API versions.
## Install
``` ```
pip install retailcrm pip install retailcrm
``` ```
### Usage ## Usage
#### API version 3 order create
```python ```python
# coding utf-8
import retailcrm import retailcrm
client = retailcrm.Client('https://demo.intarocrm.ru', 'uLxXKBwjQteE9NkO3cJAqTXNwvKktaTc') client = retailcrm.v3('https://demo.intarocrm.ru', 'uLxXKBwjQteE9NkO3cJAqTXNwvKktaTc')
order = { order = {
'firstName': 'Ivan', 'firstName': 'John',
'lastName': 'Ivanov', 'lastName': 'Doe',
'phone': '+79000000000', 'phone': '+79000000000',
'email': 'ivan@example.com', 'email': 'john@example.com',
'orderMethod': 'call-request', 'orderMethod': 'call-request',
} }
result = crm.orders_create(order) result = client.order_create(order)
``` ```
### Documentation #### API version 4 customers history
* http://www.retailcrm.pro/docs/Developers/ApiVersion3 ```python
# coding utf-8
import retailcrm
client = retailcrm.v4('https://demo.intarocrm.ru', 'uLxXKBwjQteE9NkO3cJAqTXNwvKktaTc')
result = client.customers_history(filter={'sinceId': '1500', 'startDate': '2018-03-01'})
print(result['pagination']['totalCount'])
```
#### API version 5 task create
```python
# coding utf-8
import retailcrm
client = retailcrm.v5('https://demo.intarocrm.ru', 'uLxXKBwjQteE9NkO3cJAqTXNwvKktaTc')
site = 'example-com'
task = {
'text': 'Product availability problem',
'commentary': 'Take a look ASAP',
'order': {
'externalId': '100500'
},
'performerId': 1
}
result = client.task_create(task, site)
```
## Documentation
* [English](http://www.retailcrm.pro/docs/Developers/Index)
* [Russian](http://www.retailcrm.ru/docs/Developers/Index)

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
multidimensional-urlencode==0.0.4
nose==1.3.7
requests==2.18.4

View File

@ -1,2 +1,9 @@
from client import Client # coding=utf-8
from response import Response
"""
Init
"""
from retailcrm.versions.v3 import Client as v3
from retailcrm.versions.v4 import Client as v4
from retailcrm.versions.v5 import Client as v5

View File

@ -1,642 +0,0 @@
# coding=utf-8
import requests
import json
from response import Response
class Client(object):
"""retailCRM API client"""
apiVersion = '3'
def __init__(self, crm_url, api_key):
self.apiUrl = crm_url + '/api/v' + self.apiVersion + '/'
self.apiKey = api_key
self.parameters = {'apiKey': api_key}
def make_request(self, url, method='GET'):
"""
:param url: string
:param method: string
:return: Response
"""
global result
if method == 'GET':
result = requests.get(url, params=self.parameters)
elif method == 'POST':
result = requests.post(url, data=self.parameters)
response_code = result.status_code
response_body = result.json()
return Response(response_code, response_body)
def orders(self, filters, limit=20, page=1):
"""
:param filters: array
:param limit: integer
:param page: integer
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
url = self.apiUrl + 'orders'
return self.make_request(url)
def orders_get(self, uid, by='externalId', site=None):
"""
:param uid: string
:param by: string
:param site: string
:return: Response
"""
url = self.apiUrl + 'orders/' + str(uid)
if site is not None:
self.parameters['site'] = site
if by != 'externalId':
self.parameters['by'] = by
return self.make_request(url)
def orders_create(self, order, site=None):
"""
:param order:
:param site:
:return:
"""
data_json = json.dumps(order)
self.parameters['order'] = data_json
if site is not None:
self.parameters['site'] = site
url = self.apiUrl + 'orders/create'
return self.make_request(url, 'POST')
def orders_edit(self, order, site=None):
"""
:param order:
:param site:
:return:
"""
data_json = json.dumps(order)
self.parameters['order'] = data_json
if site is not None:
self.parameters['site'] = site
url = self.apiUrl + 'orders/' + str(order['externalId']) + '/edit'
return self.make_request(url, 'POST')
def orders_upload(self, orders, site=None):
"""
:param orders:
:param site:
:return:
"""
data_json = json.dumps(orders)
self.parameters['orders'] = data_json
if site is not None:
self.parameters['site'] = site
url = self.apiUrl + 'orders/upload'
return self.make_request(url, 'POST')
def orders_fix_external_ids(self, orders, site=None):
"""
:param orders:
:param site:
:return:
"""
data_json = json.dumps(orders)
self.parameters['orders'] = data_json
if site is not None:
self.parameters['site'] = site
url = self.apiUrl + 'orders/fix-external-ids'
return self.make_request(url, 'POST')
def orders_statuses(self, ids, external_ids):
"""
:param ids:
:param external_ids:
:return:
"""
self.parameters['ids'] = ids
self.parameters['externalIds'] = external_ids
url = self.apiUrl + 'orders/statuses'
return self.make_request(url)
def orders_history(self, start_date=None, end_date=None, limit=100, offset=0, skip_my_changes=True):
"""
:param start_date:
:param end_date:
:param limit:
:param offset:
:param skip_my_changes:
:return:
"""
self.parameters['startDate'] = start_date
self.parameters['endDate'] = end_date
self.parameters['limit'] = limit
self.parameters['offset'] = offset
self.parameters['skipMyChanges'] = skip_my_changes
url = self.apiUrl + 'orders/history'
return self.make_request(url)
def customers(self, filters, limit=20, page=0):
"""
:param filters:
:param limit:
:param page:
:return:
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
url = self.apiUrl + 'customers'
return self.make_request(url)
def customers_get(self, uid, by='externalId', site=None):
"""
:param uid:
:param by:
:param site:
:return:
"""
url = self.apiUrl + 'customers/' + str(uid)
if by != 'externalId':
self.parameters['by'] = by
if site is not None:
self.parameters['site'] = site
return self.make_request(url)
def customers_create(self, customer, site=None):
"""
:param customer:
:param site:
:return:
"""
data_json = json.dumps(customer)
self.parameters['customer'] = data_json
if site is not None:
self.parameters['site'] = site
url = self.apiUrl + 'customers/create'
return self.make_request(url, 'POST')
def customers_edit(self, customer, site=None):
"""
:param customer:
:param site:
:return:
"""
data_json = json.dumps(customer)
self.parameters['customer'] = data_json
if site is not None:
self.parameters['site'] = site
url = self.apiUrl + 'customers/' + customer['externalId'] + '/edit'
return self.make_request(url, 'POST')
def customers_upload(self, customers, site=None):
"""
:param customers:
:param site:
:return:
"""
data_json = json.dumps(customers)
self.parameters['customers'] = data_json
if site is not None:
self.parameters['site'] = site
url = self.apiUrl + 'customers/upload'
return self.make_request(url, 'POST')
def customers_fix_external_ids(self, customers, site=None):
"""
:param customers:
:param site:
:return:
"""
data_json = json.dumps(customers)
self.parameters['customers'] = data_json
if site is not None:
self.parameters['site'] = site
url = self.apiUrl + 'customers/fix-external-ids'
return self.make_request(url, 'POST')
def inventories(self, filters, limit=20, page=1):
"""
:param filters:
:param limit:
:param page:
:return:
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
url = self.apiUrl + 'store/inventories'
return self.make_request(url)
def inventories_upload(self, offers):
"""
:param offers:
:return:
"""
data_json = json.dumps(offers)
self.parameters['offers'] = data_json
url = self.apiUrl + 'store/inventories/upload'
return self.make_request(url, 'POST')
def packs(self, filters, limit=20, page=1):
"""
:param filters:
:param limit:
:param page:
:return:
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
url = self.apiUrl + 'orders/packs'
return self.make_request(url)
def packs_get(self, uid):
"""
:param uid:
:return:
"""
url = self.apiUrl + 'orders/packs/' + str(uid)
return self.make_request(url)
def packs_create(self, pack):
"""
:param pack:
:return:
"""
data_json = json.dumps(pack)
self.parameters['pack'] = data_json
url = self.apiUrl + 'orders/packs/create'
return self.make_request(url, 'POST')
def packs_edit(self, pack, uid):
"""
:param pack:
:param uid:
:return:
"""
data_json = json.dumps(pack)
self.parameters['pack'] = data_json
url = self.apiUrl + 'orders/packs/' + str(uid) + '/edit'
return self.make_request(url, 'POST')
def packs_delete(self, uid):
"""
:param uid:
:return:
"""
url = self.apiUrl + 'orders/packs/' + str(uid) + '/delete'
return self.make_request(url, 'POST')
def packs_history(self, filters, limit=20, page=1):
"""
:param filters:
:param limit:
:param page:
:return:
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
url = self.apiUrl + 'orders/packs/history'
return self.make_request(url)
def countries(self):
"""
:return:
"""
url = self.apiUrl + 'reference/countries'
return self.make_request(url)
def delivery_types(self):
"""
:return:
"""
url = self.apiUrl + 'reference/delivery-types'
return self.make_request(url)
def delivery_types_edit(self, delivery_type):
"""
:param delivery_type:
:return:
"""
data_json = json.dumps(delivery_type)
self.parameters['deliveryType'] = data_json
url = self.apiUrl + 'reference/delivery-types/' + delivery_type['code'] + '/edit'
return self.make_request(url, 'POST')
def delivery_services(self):
"""
:return:
"""
url = self.apiUrl + 'reference/delivery-services'
return self.make_request(url)
def delivery_services_edit(self, delivery_service):
"""
:param delivery_service:
:return:
"""
data_json = json.dumps(delivery_service)
self.parameters['deliveryService'] = data_json
url = self.apiUrl + 'reference/delivery-services/' + delivery_service['code'] + '/edit'
return self.make_request(url, 'POST')
def payment_types(self):
"""
:return:
"""
url = self.apiUrl + 'reference/payment-types'
return self.make_request(url)
def payment_types_edit(self, payment_type):
"""
:param payment_type:
:return:
"""
data_json = json.dumps(payment_type)
self.parameters['paymentType'] = data_json
url = self.apiUrl + 'reference/payment-types/' + payment_type['code'] + '/edit'
return self.make_request(url, 'POST')
def payment_statuses(self):
"""
:return:
"""
url = self.apiUrl + 'reference/payment-statuses'
return self.make_request(url)
def payment_statuses_edit(self, payment_status):
"""
:param payment_status:
:return:
"""
data_json = json.dumps(payment_status)
self.parameters['paymentStatus'] = data_json
url = self.apiUrl + 'reference/payment-statuses/' + payment_status['code'] + '/edit'
return self.make_request(url, 'POST')
def product_statuses(self):
"""
:return:
"""
url = self.apiUrl + 'reference/product-statuses'
return self.make_request(url)
def product_statuses_edit(self, product_status):
"""
:param product_status:
:return:
"""
data_json = json.dumps(product_status)
self.parameters['productStatus'] = data_json
url = self.apiUrl + 'reference/product-statuses/' + product_status['code'] + '/edit'
return self.make_request(url, 'POST')
def order_types(self):
"""
:return:
"""
url = self.apiUrl + 'reference/order-types'
return self.make_request(url)
def order_types_edit(self, order_type):
"""
:param order_type:
:return:
"""
data_json = json.dumps(order_type)
self.parameters['orderType'] = data_json
url = self.apiUrl + 'reference/order-types/' + order_type['code'] + '/edit'
return self.make_request(url, 'POST')
def order_methods(self):
"""
:return:
"""
url = self.apiUrl + 'reference/order-methods'
return self.make_request(url)
def order_methods_edit(self, order_method):
"""
:param order_method:
:return:
"""
data_json = json.dumps(order_method)
self.parameters['orderMethod'] = data_json
url = self.apiUrl + 'reference/order-methods/' + order_method['code'] + '/edit'
return self.make_request(url, 'POST')
def status_groups(self):
"""
:return
"""
url = self.apiUrl + 'reference/status-groups'
return self.make_request(url)
def statuses(self):
"""
:return:
"""
url = self.apiUrl + 'reference/statuses'
return self.make_request(url)
def statuses_edit(self, status):
"""
:param status:
:return:
"""
data_json = json.dumps(status)
self.parameters['status'] = data_json
url = self.apiUrl + 'reference/statuses/' + status['code'] + '/edit'
return self.make_request(url, 'POST')
def stores(self):
"""
:return:
"""
url = self.apiUrl + 'reference/stores'
return self.make_request(url)
def stores_edit(self, store):
"""
:param store:
:return:
"""
data_json = json.dumps(store)
self.parameters['status'] = data_json
url = self.apiUrl + 'reference/stores/' + store['code'] + '/edit'
return self.make_request(url, 'POST')
def statistic_update(self):
"""
:return
"""
url = self.apiUrl + 'statistic/update'
return self.make_request(url)
def telephony_call_event(self, phone, call_type, code, status):
"""
:param phone:
:param call_type:
:param code:
:param status:
:return:
"""
self.parameters['hangupStatus'] = status
self.parameters['phone'] = phone
self.parameters['code'] = code
self.parameters['type'] = call_type
url = self.apiUrl + 'telephony/call/event'
return self.make_request(url, 'POST')
def telephony_calls_upload(self, calls):
"""
:param calls:
:return:
"""
data_json = json.dumps(calls)
self.parameters['calls'] = data_json
url = self.apiUrl + 'telephony/calls/upload'
return self.make_request(url, 'POST')
def telephony_settings(self, code, client_id, make_call_url, active, name, image):
"""
:param code:
:param client_id:
:param make_call_url:
:param active:
:param name:
:param image:
:return:
"""
self.parameters['code'] = code
self.parameters['clientId'] = client_id
self.parameters['makeCallUrl'] = make_call_url
self.parameters['active'] = active
self.parameters['name'] = name
self.parameters['image'] = image
url = self.apiUrl + 'telephony/settings/' + str(code)
return self.make_request(url, 'POST')
def telephony_manager(self, phone, details=True):
"""
:param phone:
:param details:
:return:
"""
self.parameters['phone'] = phone
self.parameters['details'] = details
url = self.apiUrl + 'telephony/manager'
return self.make_request(url)

View File

@ -1,5 +1,9 @@
# coding=utf-8 # coding=utf-8
"""
Response class
"""
class Response(object): class Response(object):
""" """
@ -7,23 +11,38 @@ class Response(object):
""" """
def __init__(self, code, body): def __init__(self, code, body):
self.response_body = body self.__status_code = code
self.status_code = code self.__response_body = body
def get_status_code(self): def get_status_code(self):
""" """
:return: integer :return: integer
""" """
return self.status_code return self.__status_code
def get_response(self): def get_response(self):
""" """
:return: dict :return: string
""" """
return self.response_body return self.__response_body
def is_successfull(self): def is_successful(self):
""" """
:return: boolean :return: boolean
""" """
return int(self.status_code) < 400 return int(self.__status_code) < 400
def get_error_msg(self):
"""
:return: string
"""
return self.__response_body['errorMsg']
def get_errors(self):
"""
:return: collection
"""
errors = {
} if not self.__response_body['errors'] else self.__response_body['errors']
return errors

View File

@ -0,0 +1,64 @@
# coding=utf-8
"""
API Client base class
"""
import requests
from multidimensional_urlencode import urlencode as query_builder
from retailcrm.response import Response
class Base(object):
"""retailCRM API client"""
def __init__(self, crm_url, api_key, version):
self.api_url = crm_url + '/api'
self.api_key = api_key
self.api_version = version
self.parameters = {}
def get(self, url, version=True):
"""
Get request
:param url: string
:param version: boolean
:return: Response
"""
base_url = self.api_url + '/' + self.api_version if version else self.api_url
requests_url = base_url + url if not self.parameters else base_url + \
url + "?" + query_builder(self.parameters)
response = requests.get(requests_url, headers={
'X-API-KEY': self.api_key})
return Response(response.status_code, response.json())
def post(self, url):
"""
Post request
:return: Response
"""
requests_url = self.api_url + url
response = requests.post(requests_url, data=self.parameters, headers={
'X-API-KEY': self.api_key})
return Response(response.status_code, response.json())
def api_versions(self):
"""
:return: Response
"""
return self.get('/api-versions', False)
def api_credentials(self):
"""
:return: Response
"""
return self.get('/credentials', False)
def statistic_update(self):
"""
:return Response
"""
return self.get('/statistic/update')

525
retailcrm/versions/v3.py Normal file
View File

@ -0,0 +1,525 @@
# coding=utf-8
"""
API Client version 3
"""
import json
from retailcrm.versions.base import Base
class Client(Base):
"""retailCRM API client"""
apiVersion = 'v3'
def __init__(self, crm_url, api_key):
Base.__init__(self, crm_url, api_key, self.apiVersion)
def customers(self, filters=None, limit=20, page=1):
"""
:param filters:
:param limit:
:param page:
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/customers')
def customer_create(self, customer, site=None):
"""
:param customer:
:param site:
:return: Response
"""
self.parameters['customer'] = json.dumps(customer)
if site is not None:
self.parameters['site'] = site
return self.post('/customers/create')
def customers_fix_external_ids(self, customers, site=None):
"""
:param customers:
:param site:
:return: Response
"""
self.parameters['customers'] = json.dumps(customers)
if site is not None:
self.parameters['site'] = site
return self.post('/customers/fix-external-ids')
def customers_upload(self, customers, site=None):
"""
:param customers:
:param site:
:return: Response
"""
self.parameters['customers'] = json.dumps(customers)
if site is not None:
self.parameters['site'] = site
return self.post('/customers/upload')
def customer(self, uid, uid_type='externalId', site=None):
"""
:param uid:
:param uid_type:
:param site:
:return: Response
"""
if uid_type != 'externalId':
self.parameters['by'] = uid_type
if site is not None:
self.parameters['site'] = site
return self.get('/customers/' + str(uid))
def customer_edit(self, customer, uid_type='externalId', site=None):
"""
:param customer:
:param uid_type:
:param site:
:return: Response
"""
self.parameters['customer'] = json.dumps(customer)
if uid_type != 'externalId':
self.parameters['by'] = uid_type
if site is not None:
self.parameters['site'] = site
return self.post('/customers/' + customer[uid_type] + '/edit')
def orders(self, filters=None, limit=20, page=1):
"""
:param filters: array
:param limit: integer
:param page: integer
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/orders')
def order_create(self, order, site=None):
"""
:param order: object
:param site: string
:return: Response
"""
self.parameters['order'] = json.dumps(order)
if site is not None:
self.parameters['site'] = site
return self.post('/orders/create')
def orders_fix_external_ids(self, orders, site=None):
"""
:param orders: object
:param site: string
:return: Response
"""
self.parameters['orders'] = json.dumps(orders)
if site is not None:
self.parameters['site'] = site
return self.post('/orders/fix-external-ids')
def orders_history(self, start=None, end=None, limit=100, offset=0, skip=True):
"""
:param start:
:param end:
:param limit:
:param offset:
:param skip:
:return: Response
"""
self.parameters['startDate'] = start
self.parameters['endDate'] = end
self.parameters['limit'] = limit
self.parameters['offset'] = offset
self.parameters['skipMyChanges'] = skip
return self.get('/orders/history')
def orders_statuses(self, ids, external_ids):
"""
:param ids: array
:param external_ids: array
:return: Response
"""
self.parameters['ids'] = ids
self.parameters['externalIds'] = external_ids
return self.get('/orders/statuses')
def orders_upload(self, orders, site=None):
"""
:param orders: object
:param site: string
:return: Response
"""
self.parameters['orders'] = json.dumps(orders)
if site is not None:
self.parameters['site'] = site
return self.post('/orders/upload')
def order(self, uid, uid_type='externalId', site=None):
"""
:param uid: string
:param uid_type: string
:param site: string
:return: Response
"""
if site is not None:
self.parameters['site'] = site
if uid_type != 'externalId':
self.parameters['by'] = uid_type
return self.get('/orders/' + str(uid))
def order_edit(self, order, uid_type='externalId', site=None):
"""
:param order: object
:param uid_type: string
:param site: string
:return: Response
"""
self.parameters['order'] = json.dumps(order)
if site is not None:
self.parameters['site'] = site
if uid_type != 'externalId':
self.parameters['by'] = uid_type
return self.post('/orders/' + str(order[uid_type]) + '/edit')
def packs(self, filters=None, limit=20, page=1):
"""
:param filters:
:param limit:
:param page:
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/orders/packs')
def pack_create(self, pack):
"""
:param pack:
:return: Response
"""
self.parameters['pack'] = json.dumps(pack)
return self.post('/orders/packs/create')
def packs_history(self, filters=None, limit=20, page=1):
"""
:param filters:
:param limit:
:param page:
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/orders/packs/history')
def pack(self, uid):
"""
:param uid:
:return: Response
"""
return self.get('/orders/packs/' + str(uid))
def pack_delete(self, uid):
"""
:param uid:
:return: Response
"""
return self.post('/orders/packs/' + str(uid) + '/delete')
def pack_edit(self, pack):
"""
:param pack:
:return: Response
"""
self.parameters['pack'] = json.dumps(pack)
return self.post('/orders/packs/' + str(pack['id']) + '/edit')
def countries(self):
"""
:return: Response
"""
return self.get('/reference/countries')
def delivery_services(self):
"""
:return: Response
"""
return self.get('/reference/delivery-services')
def delivery_services_edit(self, delivery_service):
"""
:param delivery_service:
:return: Response
"""
self.parameters['deliveryService'] = json.dumps(delivery_service)
return self.post('/reference/delivery-services/' + delivery_service['code'] + '/edit')
def delivery_types(self):
"""
:return: Response
"""
return self.get('/reference/delivery-types')
def delivery_types_edit(self, delivery_type):
"""
:param delivery_type:
:return: Response
"""
self.parameters['deliveryType'] = json.dumps(delivery_type)
return self.post('/reference/delivery-types/' + delivery_type['code'] + '/edit')
def order_methods(self):
"""
:return: Response
"""
return self.get('/reference/order-methods')
def order_methods_edit(self, order_method):
"""
:param order_method:
:return: Response
"""
self.parameters['orderMethod'] = json.dumps(order_method)
return self.post('/reference/order-methods/' + order_method['code'] + '/edit')
def order_types(self):
"""
:return: Response
"""
return self.get('/reference/order-types')
def order_types_edit(self, order_type):
"""
:param order_type:
:return: Response
"""
self.parameters['orderType'] = json.dumps(order_type)
return self.post('/reference/order-types/' + order_type['code'] + '/edit')
def payment_statuses(self):
"""
:return: Response
"""
return self.get('/reference/payment-statuses')
def payment_statuses_edit(self, payment_status):
"""
:param payment_status:
:return: Response
"""
self.parameters['paymentStatus'] = json.dumps(payment_status)
return self.post('/reference/payment-statuses/' + payment_status['code'] + '/edit')
def payment_types(self):
"""
:return: Response
"""
return self.get('/reference/payment-types')
def payment_types_edit(self, payment_type):
"""
:param payment_type:
:return: Response
"""
self.parameters['paymentType'] = json.dumps(payment_type)
return self.post('/reference/payment-types/' + payment_type['code'] + '/edit')
def product_statuses(self):
"""
:return: Response
"""
return self.get('/reference/product-statuses')
def product_statuses_edit(self, product_status):
"""
:param product_status:
:return: Response
"""
self.parameters['productStatus'] = json.dumps(product_status)
return self.post('/reference/product-statuses/' + product_status['code'] + '/edit')
def sites(self):
"""
:return: Response
"""
return self.get('/reference/sites')
def sites_edit(self, site):
"""
:param site:
:return: Response
"""
self.parameters['site'] = json.dumps(site)
return self.post('/reference/sites/' + site['code'] + '/edit')
def status_groups(self):
"""
:return
"""
return self.get('/reference/status-groups')
def statuses(self):
"""
:return: Response
"""
return self.get('/reference/statuses')
def statuses_edit(self, status):
"""
:param status:
:return: Response
"""
self.parameters['status'] = json.dumps(status)
return self.post('/reference/statuses/' + status['code'] + '/edit')
def stores(self):
"""
:return: Response
"""
return self.get('/reference/stores')
def stores_edit(self, store):
"""
:param store:
:return: Response
"""
self.parameters['status'] = json.dumps(store)
return self.post('/reference/stores/' + store['code'] + '/edit')
def inventories(self, filters=None, limit=20, page=1):
"""
:param filters:
:param limit:
:param page:
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/store/inventories')
def inventories_upload(self, offers):
"""
:param offers:
:return: Response
"""
self.parameters['offers'] = json.dumps(offers)
return self.post('/store/inventories/upload')
def telephony_call_event(self, phone, call_type, code, status):
"""
:param phone:
:param call_type:
:param code:
:param status:
:return: Response
"""
self.parameters['hangupStatus'] = status
self.parameters['phone'] = phone
self.parameters['code'] = code
self.parameters['type'] = call_type
return self.post('/telephony/call/event')
def telephony_calls_upload(self, calls):
"""
:param calls:
:return: Response
"""
self.parameters['calls'] = json.dumps(calls)
return self.post('/telephony/calls/upload')
def telephony_manager(self, phone, details=True):
"""
:param phone:
:param details:
:return: Response
"""
self.parameters['phone'] = phone
self.parameters['details'] = details
return self.get('/telephony/manager')
def telephony_settings(self, code, client_id, make_call_url, active, name, image):
"""
:param code:
:param client_id:
:param make_call_url:
:param active:
:param name:
:param image:
:return: Response
"""
self.parameters['code'] = code
self.parameters['clientId'] = client_id
self.parameters['makeCallUrl'] = make_call_url
self.parameters['active'] = active
self.parameters['name'] = name
self.parameters['image'] = image
return self.post('/telephony/settings/' + str(code))

650
retailcrm/versions/v4.py Normal file
View File

@ -0,0 +1,650 @@
# coding=utf-8
"""
API Client version 4
"""
import json
from retailcrm.versions.base import Base
class Client(Base):
"""retailCRM API client"""
apiVersion = 'v4'
def __init__(self, crm_url, api_key):
Base.__init__(self, crm_url, api_key, self.apiVersion)
def customers(self, filters=None, limit=20, page=1):
"""
:param filters:
:param limit:
:param page:
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/customers')
def customer_create(self, customer, site=None):
"""
:param customer:
:param site:
:return: Response
"""
self.parameters['customer'] = json.dumps(customer)
if site is not None:
self.parameters['site'] = site
return self.post('/customers/create')
def customers_fix_external_ids(self, customers, site=None):
"""
:param customers:
:param site:
:return: Response
"""
self.parameters['customers'] = json.dumps(customers)
if site is not None:
self.parameters['site'] = site
return self.post('/customers/fix-external-ids')
def customers_history(self, filters=None, limit=20, page=1):
"""
:param filters:
:param limit:
:param page:
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/customers/history')
def customers_upload(self, customers, site=None):
"""
:param customers:
:param site:
:return: Response
"""
self.parameters['customers'] = json.dumps(customers)
if site is not None:
self.parameters['site'] = site
return self.post('/customers/upload')
def customer(self, uid, uid_type='externalId', site=None):
"""
:param uid:
:param uid_type:
:param site:
:return: Response
"""
if uid_type != 'externalId':
self.parameters['by'] = uid_type
if site is not None:
self.parameters['site'] = site
return self.get('/customers/' + str(uid))
def customer_edit(self, customer, uid_type='externalId', site=None):
"""
:param customer:
:param uid_type:
:param site:
:return: Response
"""
self.parameters['customer'] = json.dumps(customer)
if uid_type != 'externalId':
self.parameters['by'] = uid_type
if site is not None:
self.parameters['site'] = site
return self.post('/customers/' + customer[uid_type] + '/edit')
def delivery_setting(self, code):
"""
:param code:
:return: Response
"""
return self.get('/delivery/generic/setting/' + str(code))
def delivery_setting_edit(self, configuration):
"""
:param configuration::
:return: Response
"""
self.parameters['configuration'] = json.dumps(configuration)
return self.post('/delivery/generic/setting/' + str(configuration['code'])) + '/edit'
def delivery_tracking(self, code, status_update):
"""
:param code:
:param status_update:
:return: Response
"""
self.parameters['statusUpdate'] = json.dumps(status_update)
return self.post('/delivery/generic/' + str(code) + '/tracking')
def marketplace_setting_edit(self, configuration):
"""
:param configuration::
:return: Response
"""
self.parameters['configuration'] = json.dumps(configuration)
return self.post('/marketplace/external/setting/' + str(configuration['code'])) + '/edit'
def orders(self, filters=None, limit=20, page=1):
"""
:param filters: array
:param limit: integer
:param page: integer
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/orders')
def order_create(self, order, site=None):
"""
:param order: object
:param site: string
:return: Response
"""
self.parameters['order'] = json.dumps(order)
if site is not None:
self.parameters['site'] = site
return self.post('/orders/create')
def orders_fix_external_ids(self, orders, site=None):
"""
:param orders: object
:param site: string
:return: Response
"""
self.parameters['orders'] = json.dumps(orders)
if site is not None:
self.parameters['site'] = site
return self.post('/orders/fix-external-ids')
def orders_history(self, filters=None, limit=20, page=1):
"""
:param filters:
:param limit:
:param page:
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/orders/history')
def orders_statuses(self, ids, external_ids):
"""
:param ids: array
:param external_ids: array
:return: Response
"""
self.parameters['ids'] = ids
self.parameters['externalIds'] = external_ids
return self.get('/orders/statuses')
def orders_upload(self, orders, site=None):
"""
:param orders: object
:param site: string
:return: Response
"""
self.parameters['orders'] = json.dumps(orders)
if site is not None:
self.parameters['site'] = site
return self.post('/orders/upload')
def order(self, uid, uid_type='externalId', site=None):
"""
:param uid: string
:param uid_type: string
:param site: string
:return: Response
"""
if site is not None:
self.parameters['site'] = site
if uid_type != 'externalId':
self.parameters['by'] = uid_type
return self.get('/orders/' + str(uid))
def order_edit(self, order, uid_type='externalId', site=None):
"""
:param order: object
:param uid_type: string
:param site: string
:return: Response
"""
self.parameters['order'] = json.dumps(order)
if site is not None:
self.parameters['site'] = site
if uid_type != 'externalId':
self.parameters['by'] = uid_type
return self.post('/orders/' + str(order[uid_type]) + '/edit')
def packs(self, filters=None, limit=20, page=1):
"""
:param filters:
:param limit:
:param page:
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/orders/packs')
def pack_create(self, pack):
"""
:param pack:
:return: Response
"""
self.parameters['pack'] = json.dumps(pack)
return self.post('/orders/packs/create')
def packs_history(self, filters=None, limit=20, page=1):
"""
:param filters:
:param limit:
:param page:
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/orders/packs/history')
def pack(self, uid):
"""
:param uid:
:return: Response
"""
return self.get('/orders/packs/' + str(uid))
def pack_delete(self, uid):
"""
:param uid:
:return: Response
"""
return self.post('/orders/packs/' + str(uid) + '/delete')
def pack_edit(self, pack):
"""
:param pack:
:param uid:
:return: Response
"""
self.parameters['pack'] = json.dumps(pack)
return self.post('/orders/packs/' + str(pack['id']) + '/edit')
def countries(self):
"""
:return: Response
"""
return self.get('/reference/countries')
def delivery_services(self):
"""
:return: Response
"""
return self.get('/reference/delivery-services')
def delivery_services_edit(self, delivery_service):
"""
:param delivery_service:
:return: Response
"""
self.parameters['deliveryService'] = json.dumps(delivery_service)
return self.post('/reference/delivery-services/' + delivery_service['code'] + '/edit')
def delivery_types(self):
"""
:return: Response
"""
return self.get('/reference/delivery-types')
def delivery_types_edit(self, delivery_type):
"""
:param delivery_type:
:return: Response
"""
self.parameters['deliveryType'] = json.dumps(delivery_type)
return self.post('/reference/delivery-types/' + delivery_type['code'] + '/edit')
def order_methods(self):
"""
:return: Response
"""
return self.get('/reference/order-methods')
def order_methods_edit(self, order_method):
"""
:param order_method:
:return: Response
"""
self.parameters['orderMethod'] = json.dumps(order_method)
return self.post('/reference/order-methods/' + order_method['code'] + '/edit')
def order_types(self):
"""
:return: Response
"""
return self.get('/reference/order-types')
def order_types_edit(self, order_type):
"""
:param order_type:
:return: Response
"""
self.parameters['orderType'] = json.dumps(order_type)
return self.post('/reference/order-types/' + order_type['code'] + '/edit')
def payment_statuses(self):
"""
:return: Response
"""
return self.get('/reference/payment-statuses')
def payment_statuses_edit(self, payment_status):
"""
:param payment_status:
:return: Response
"""
self.parameters['paymentStatus'] = json.dumps(payment_status)
return self.post('/reference/payment-statuses/' + payment_status['code'] + '/edit')
def payment_types(self):
"""
:return: Response
"""
return self.get('/reference/payment-types')
def payment_types_edit(self, payment_type):
"""
:param payment_type:
:return: Response
"""
self.parameters['paymentType'] = json.dumps(payment_type)
return self.post('/reference/payment-types/' + payment_type['code'] + '/edit')
def price_types(self):
"""
:return: Response
"""
return self.get('/reference/price-types')
def price_types_edit(self, price_type):
"""
:param price_type:
:return: Response
"""
self.parameters['priceType'] = json.dumps(price_type)
return self.post('/reference/price-types/' + price_type['code'] + '/edit')
def product_statuses(self):
"""
:return: Response
"""
return self.get('/reference/product-statuses')
def product_statuses_edit(self, product_status):
"""
:param product_status:
:return: Response
"""
self.parameters['productStatus'] = json.dumps(product_status)
return self.post('/reference/product-statuses/' + product_status['code'] + '/edit')
def sites(self):
"""
:return: Response
"""
return self.get('/reference/sites')
def sites_edit(self, site):
"""
:param site:
:return: Response
"""
self.parameters['site'] = json.dumps(site)
return self.post('/reference/sites/' + site['code'] + '/edit')
def status_groups(self):
"""
:return
"""
return self.get('/reference/status-groups')
def statuses(self):
"""
:return: Response
"""
return self.get('/reference/statuses')
def statuses_edit(self, status):
"""
:param status:
:return: Response
"""
self.parameters['status'] = json.dumps(status)
return self.post('/reference/statuses/' + status['code'] + '/edit')
def stores(self):
"""
:return: Response
"""
return self.get('/reference/stores')
def stores_edit(self, store):
"""
:param store:
:return: Response
"""
self.parameters['status'] = json.dumps(store)
return self.post('/reference/stores/' + store['code'] + '/edit')
def inventories(self, filters=None, limit=20, page=1):
"""
:param filters:
:param limit:
:param page:
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/store/inventories')
def inventories_upload(self, offers):
"""
:param offers:
:return: Response
"""
self.parameters['offers'] = json.dumps(offers)
return self.post('/store/inventories/upload')
def prices_upload(self, prices):
"""
:param prices:
:return: Response
"""
self.parameters['prices'] = json.dumps(prices)
return self.post('/store/prices/upload')
def products(self, filters=None, limit=20, page=1):
"""
:param filters: array
:param limit: integer
:param page: integer
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/store/products')
def store_setting(self, code):
"""
:param code:
:return: Response
"""
return self.get('/store/setting/' + str(code))
def store_setting_edit(self, configuration):
"""
:param configuration::
:return: Response
"""
self.parameters['configuration'] = json.dumps(configuration)
return self.post('/store/setting/' + str(configuration['code'])) + '/edit'
def telephony_call_event(self, event):
"""
:param event:
:return: Response
"""
self.parameters['event'] = json.dumps(event)
return self.post('/telephony/call/event')
def telephony_calls_upload(self, calls):
"""
:param calls:
:return: Response
"""
self.parameters['calls'] = json.dumps(calls)
return self.post('/telephony/calls/upload')
def telephony_manager(self, phone, details=True):
"""
:param phone:
:param details:
:return: Response
"""
self.parameters['phone'] = phone
self.parameters['details'] = details
return self.get('/telephony/manager')
def telephony_setting(self, code):
"""
:param code:
:return: Response
"""
return self.get('/telephony/setting/' + str(code))
def telephony_setting_edit(self, configuration):
"""
:param configuration::
:return: Response
"""
self.parameters['configuration'] = json.dumps(configuration)
return self.post('/telephony/setting/' + str(configuration['code'])) + '/edit'
def user_groups(self, limit=20, page=1):
"""
:param limit: integer
:param page: integer
:return: Response
"""
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/user-groups')
def users(self, filters=None, limit=20, page=1):
"""
:param filters: array
:param limit: integer
:param page: integer
:return: Response
"""
self.parameters['filter'] = filters
self.parameters['limit'] = limit
self.parameters['page'] = page
return self.get('/users')
def user(self, uid):
"""
:param uid:
:return: Response
"""
return self.get('/users/' + str(uid))

1075
retailcrm/versions/v5.py Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,16 +1,45 @@
# coding=utf-8 # coding=utf-8
"""
Setup file
"""
import os
from setuptools import setup from setuptools import setup
# Utility function to read the README file.
# Used for the long_description.
def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()
setup( setup(
name='retailcrm', name='retailcrm',
version='3.0.5', version='5.0.0',
description='Client for retailCRM API', description='retailCRM multi version API client',
long_description=read('README.md'),
url='https://github.com/retailcrm/api-client-python', url='https://github.com/retailcrm/api-client-python',
author='retailCRM', author='retailCRM',
author_email='integration@retailcrm.ru', author_email='integration@retailcrm.ru',
keywords='crm, saas, rest, e-commerce', keywords='crm, saas, rest, e-commerce',
license='MIT', license='MIT',
packages=['retailcrm'], packages=['retailcrm', 'tests'],
package_data={}, package_data={},
install_requires=['requests'] install_requires=['requests', 'multidimensional_urlencode', 'nose'],
classifiers=[
'Development Status :: 5 - Production/Stable',
'Environment :: Other Environment',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3 :: Only',
'Topic :: Software Development :: Libraries :: Python Modules',
]
) )

69
tests/v3_tests.py Normal file
View File

@ -0,0 +1,69 @@
# coding=utf-8
"""
retailCRM API client v3 tests
"""
import unittest
import os
import retailcrm
class TestVersion3(unittest.TestCase):
"""
TestClass for v3
"""
def setUp(self):
"""
Setup
"""
self.client = retailcrm.v3(
os.getenv('RETAILCRM_URL'), os.getenv('RETAILCRM_KEY'))
def test_wrong_api_url(self):
"""
V3 Test wrong api url
"""
client = retailcrm.v3(
'https://epoqwieqwpoieqpwoeiqpwoeiq.retailcrm.ru', '98sdf9sj8fsd9fjs9dfjs98')
response = client.statistic_update()
self.assertTrue(response.is_successful(), False)
self.assertEqual(response.get_error_msg(), 'Account does not exist.')
def test_wrong_api_key(self):
"""
V3 Test wrong api key
"""
client = retailcrm.v3(os.getenv('RETAILCRM_URL'),
'98sdf9sj8fsd9fjs9dfjs98')
response = client.statistic_update()
self.assertEqual(response.get_error_msg(), 'Wrong "apiKey" value.')
def test_missing_api_key(self):
"""
V3 Test missing api key
"""
client = retailcrm.v3(os.getenv('RETAILCRM_URL'), None)
response = client.statistic_update()
self.assertEqual(response.get_error_msg(), '"apiKey" is missing.')
def test_api_versions(self):
"""
V3 Test api-versions method
"""
response = self.client.api_versions()
self.assertTrue(response.is_successful(), True)
def test_api_credentials(self):
"""
V3 Test api-credentials method
"""
response = self.client.api_credentials()
self.assertTrue(response.is_successful(), True)

69
tests/v4_tests.py Normal file
View File

@ -0,0 +1,69 @@
# coding=utf-8
"""
retailCRM API client v4 tests
"""
import unittest
import os
import retailcrm
class TestVersion4(unittest.TestCase):
"""
TestClass for v4
"""
def setUp(self):
"""
Setup
"""
self.client = retailcrm.v4(
os.getenv('RETAILCRM_URL'), os.getenv('RETAILCRM_KEY'))
def test_wrong_api_url(self):
"""
V4 Test wrong api url
"""
client = retailcrm.v4(
'https://epoqwieqwpoieqpwoeiqpwoeiq.retailcrm.ru', '98sdf9sj8fsd9fjs9dfjs98')
response = client.statistic_update()
self.assertTrue(response.is_successful(), False)
self.assertEqual(response.get_error_msg(), 'Account does not exist.')
def test_wrong_api_key(self):
"""
V4 Test wrong api key
"""
client = retailcrm.v3(os.getenv('RETAILCRM_URL'),
'98sdf9sj8fsd9fjs9dfjs98')
response = client.statistic_update()
self.assertEqual(response.get_error_msg(), 'Wrong "apiKey" value.')
def test_missing_api_key(self):
"""
V4 Test missing api key
"""
client = retailcrm.v4(os.getenv('RETAILCRM_URL'), None)
response = client.statistic_update()
self.assertEqual(response.get_error_msg(), '"apiKey" is missing.')
def test_api_versions(self):
"""
V4 Test api-versions method
"""
response = self.client.api_versions()
self.assertTrue(response.is_successful(), True)
def test_api_credentials(self):
"""
V4 Test api-credentials method
"""
response = self.client.api_credentials()
self.assertTrue(response.is_successful(), True)

69
tests/v5_tests.py Normal file
View File

@ -0,0 +1,69 @@
# coding=utf-8
"""
retailCRM API client v5 tests
"""
import unittest
import os
import retailcrm
class TestVersion5(unittest.TestCase):
"""
TestClass for v5
"""
def setUp(self):
"""
Setup
"""
self.client = retailcrm.v5(
os.getenv('RETAILCRM_URL'), os.getenv('RETAILCRM_KEY'))
def test_wrong_api_url(self):
"""
V5 Test wrong api url
"""
client = retailcrm.v5(
'https://epoqwieqwpoieqpwoeiqpwoeiq.retailcrm.ru', '98sdf9sj8fsd9fjs9dfjs98')
response = client.statistic_update()
self.assertTrue(response.is_successful(), False)
self.assertEqual(response.get_error_msg(), 'Account does not exist.')
def test_wrong_api_key(self):
"""
V5 Test wrong api key
"""
client = retailcrm.v5(os.getenv('RETAILCRM_URL'),
'98sdf9sj8fsd9fjs9dfjs98')
response = client.statistic_update()
self.assertEqual(response.get_error_msg(), 'Wrong "apiKey" value.')
def test_missing_api_key(self):
"""
V5 Test missing api key
"""
client = retailcrm.v5(os.getenv('RETAILCRM_URL'), None)
response = client.statistic_update()
self.assertEqual(response.get_error_msg(), '"apiKey" is missing.')
def test_api_versions(self):
"""
V5 Test api-versions method
"""
response = self.client.api_versions()
self.assertTrue(response.is_successful(), True)
def test_api_credentials(self):
"""
V5 Test api-credentials method
"""
response = self.client.api_credentials()
self.assertTrue(response.is_successful(), True)