From fe262b267d60d3c97815dd2076405c3b7a20f077 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Tue, 20 Mar 2018 20:32:43 +0300 Subject: [PATCH] new major version with multiply API version support --- .gitignore | 3 + .travis.yml | 9 + LICENSE | 2 +- README.md | 69 ++- requirements.txt | 18 + retailcrm/__init__.py | 11 +- retailcrm/client.py | 642 --------------------- retailcrm/response.py | 33 +- retailcrm/versions/base.py | 64 +++ retailcrm/versions/v3.py | 525 ++++++++++++++++++ retailcrm/versions/v4.py | 650 ++++++++++++++++++++++ retailcrm/versions/v5.py | 1075 ++++++++++++++++++++++++++++++++++++ setup.py | 37 +- tests/v3_tests.py | 69 +++ tests/v4_tests.py | 69 +++ tests/v5_tests.py | 69 +++ 16 files changed, 2679 insertions(+), 666 deletions(-) create mode 100644 .travis.yml create mode 100644 requirements.txt delete mode 100644 retailcrm/client.py create mode 100644 retailcrm/versions/base.py create mode 100644 retailcrm/versions/v3.py create mode 100644 retailcrm/versions/v4.py create mode 100644 retailcrm/versions/v5.py create mode 100644 tests/v3_tests.py create mode 100644 tests/v4_tests.py create mode 100644 tests/v5_tests.py diff --git a/.gitignore b/.gitignore index dc1ff5a..53afa79 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ *.swp *.pyc +*.nose* /*.egg-info /dist/ +/venv/ +/.vscode/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..336824d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: python +python: + - '3.4' + - '3.5' + - '3.6' +before_install: + - pip install -r requirements.txt +script: + - nosetests -v diff --git a/LICENSE b/LICENSE index 8d17b56..dce51a1 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ 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 of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 27d3c97..ba77c90 100644 --- a/README.md +++ b/README.md @@ -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 ``` -### Usage +## Usage + +#### API version 3 order create ```python +# coding utf-8 + import retailcrm -client = retailcrm.Client('https://demo.intarocrm.ru', 'uLxXKBwjQteE9NkO3cJAqTXNwvKktaTc') +client = retailcrm.v3('https://demo.intarocrm.ru', 'uLxXKBwjQteE9NkO3cJAqTXNwvKktaTc') order = { - 'firstName': 'Ivan', - 'lastName': 'Ivanov', + 'firstName': 'John', + 'lastName': 'Doe', 'phone': '+79000000000', - 'email': 'ivan@example.com', + 'email': 'john@example.com', '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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..687b0b6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,18 @@ +astroid==1.6.2 +attrs==17.4.0 +autopep8==1.3.4 +certifi==2018.1.18 +chardet==3.0.4 +idna==2.6 +isort==4.3.4 +lazy-object-proxy==1.3.1 +mccabe==0.6.1 +multidimensional-urlencode==0.0.4 +nose==1.3.7 +py==1.5.2 +pycodestyle==2.3.1 +pylint==1.8.3 +requests==2.18.4 +six==1.11.0 +urllib3==1.22 +wrapt==1.10.11 diff --git a/retailcrm/__init__.py b/retailcrm/__init__.py index e84d0e5..24adf76 100644 --- a/retailcrm/__init__.py +++ b/retailcrm/__init__.py @@ -1,2 +1,9 @@ -from client import Client -from response import Response +# coding=utf-8 + +""" +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 diff --git a/retailcrm/client.py b/retailcrm/client.py deleted file mode 100644 index 5eb68a8..0000000 --- a/retailcrm/client.py +++ /dev/null @@ -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) diff --git a/retailcrm/response.py b/retailcrm/response.py index 8ca3e35..0abec50 100644 --- a/retailcrm/response.py +++ b/retailcrm/response.py @@ -1,5 +1,9 @@ # coding=utf-8 +""" +Response class +""" + class Response(object): """ @@ -7,23 +11,38 @@ class Response(object): """ 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): """ :return: integer """ - return self.status_code + return self.__status_code 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 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 diff --git a/retailcrm/versions/base.py b/retailcrm/versions/base.py new file mode 100644 index 0000000..42ddd81 --- /dev/null +++ b/retailcrm/versions/base.py @@ -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') diff --git a/retailcrm/versions/v3.py b/retailcrm/versions/v3.py new file mode 100644 index 0000000..8e26418 --- /dev/null +++ b/retailcrm/versions/v3.py @@ -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)) diff --git a/retailcrm/versions/v4.py b/retailcrm/versions/v4.py new file mode 100644 index 0000000..c9a7d53 --- /dev/null +++ b/retailcrm/versions/v4.py @@ -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)) diff --git a/retailcrm/versions/v5.py b/retailcrm/versions/v5.py new file mode 100644 index 0000000..2bca9c2 --- /dev/null +++ b/retailcrm/versions/v5.py @@ -0,0 +1,1075 @@ +# coding=utf-8 + +""" +API Client version 5 +""" + +import json + +from retailcrm.versions.base import Base + + +class Client(Base): + """retailCRM API client""" + + apiVersion = 'v5' + + def __init__(self, crm_url, api_key): + Base.__init__(self, crm_url, api_key, self.apiVersion) + + def costs(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('/costs') + + def cost_create(self, cost, site=None): + """ + :param cost: + :param site: + :return: Response + """ + self.parameters['cost'] = json.dumps(cost) + + if site is not None: + self.parameters['site'] = site + + return self.post('/costs/create') + + def costs_delete(self, ids): + """ + :param ids: + :return: Response + """ + self.parameters['ids'] = json.dumps(ids) + + return self.post('/costs/delete') + + def costs_upload(self, costs): + """ + :param costs: + :return: Response + """ + self.parameters['costs'] = json.dumps(costs) + + return self.post('/costs/upload') + + def cost(self, uid): + """ + :param uid: + :return: Response + """ + + return self.get('/costs/' + str(uid)) + + def cost_delete(self, uid): + """ + :param uid: + :return: Response + """ + + return self.post('/costs/' + str(uid) + '/delete') + + def cost_edit(self, cost, site=None): + """ + :param cost: + :param site: + :return: Response + """ + self.parameters['cost'] = json.dumps(cost) + + if site is not None: + self.parameters['site'] = site + + return self.post('/costs/' + str(cost['id']) + '/edit') + + def custom_fields(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('/custom-fields') + + def custom_dictionaries(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('/custom-fields/dictionaries') + + def custom_dictionary_create(self, dictionary): + """ + :param dictionary: + :return: Response + """ + self.parameters['customDictionary'] = json.dumps(dictionary) + + return self.post('/custom-fields/dictionaries/create') + + def custom_dictionary(self, code): + """ + :param code: + :return: Response + """ + + return self.get('/custom-fields/dictionaries/' + str(code)) + + def custom_dictionary_edit(self, dictionary): + """ + :param dictionary: + :return: Response + """ + self.parameters['customDictionary'] = json.dumps(dictionary) + + return self.post('/custom-fields/dictionaries/' + str(dictionary['code']) + '/create') + + def custom_field_create(self, field): + """ + :param field: + :return: Response + """ + self.parameters['customField'] = json.dumps(field) + + return self.post('/custom-fields/' + str(field['entity']) + '/create') + + def custom_field(self, code, entity): + """ + :param code: + :param entity: + :return: Response + """ + + return self.get('/custom-fields/' + str(entity) + '/' + str(code)) + + def custom_field_edit(self, field): + """ + :param field: + :return: Response + """ + + entity = str(field['entity']) + code = str(field['code']) + self.parameters['customField'] = json.dumps(field) + + return self.post('/custom-fields/' + entity + '/' + code + '/create') + + 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 customers_combine(self, customers, result): + """ + :param customers: array of object + :param result: object + :return: Response + """ + self.parameters['customers'] = json.dumps(customers) + self.parameters['resultCustomer'] = json.dumps(result) + + return self.post('/customers/combine') + + 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 customer_notes(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/notes') + + def customer_note_create(self, note, site=None): + """ + :param note: object + :param site: string + :return: Response + """ + self.parameters['note'] = json.dumps(note) + + if site is not None: + self.parameters['site'] = site + + return self.post('/customers/notes/create') + + def customer_note_delete(self, uid): + """ + :param uid: string + :return: Response + """ + + return self.post('/customers/notes/' + str(uid) + '/delete') + + 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/' + str(customer[uid_type]) + '/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 delivery_shipments(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('/delivery/shipments') + + def delivery_shipment_create(self, shipment, delivery_type, site=None): + """ + :param shipment: + :param delivery_type: + :param site: + :return: Response + """ + self.parameters['deliveryShipment'] = json.dumps(shipment) + self.parameters['deliveryType'] = delivery_type + + if site is not None: + self.parameters['site'] = site + + return self.post('/delivery/shipment/create') + + def delivery_shipment(self, uid): + """ + :param uid: + :return: Response + """ + + return self.get('/delivery/shipments/' + str(uid)) + + def delivery_shipment_edit(self, shipment, site=None): + """ + :param shipment: + :param site: + :return: Response + """ + self.parameters['deliveryShipment'] = json.dumps(shipment) + + if site is not None: + self.parameters['site'] = site + + return self.post('/delivery/shipment/' + shipment['id'] + '/edit') + + def integration_module(self, code): + """ + :param code: + :return: Response + """ + + return self.get('/integration-modules/' + str(code)) + + def integration_module_edit(self, configuration): + """ + :param configuration:: + :return: Response + """ + self.parameters['integrationModule'] = json.dumps(configuration) + + return self.post('/integration-modules/' + 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 orders_combine(self, order, result_order, technique='ours'): + """ + :param order: object + :param result_order: object + :param technique: string + :return: Response + """ + self.parameters['technique'] = technique + self.parameters['order'] = json.dumps(order) + self.parameters['resultOrder'] = json.dumps(result_order) + + return self.post('/orders/combine') + + 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 order_payment_create(self, payment, site=None): + """ + :param payment: object + :param site: string + :return: Response + """ + self.parameters['payment'] = json.dumps(payment) + + if site is not None: + self.parameters['site'] = site + + return self.post('/orders/payments/create') + + def order_payment_delete(self, uid): + """ + :param uid: string + :return: Response + """ + + return self.post('/orders/payments/' + str(uid) + '/delete') + + def order_payment_edit(self, payment, uid_type='externalId', site=None): + """ + :param payment: object + :param uid_type: string + :param site: string + :return: Response + """ + self.parameters['payment'] = json.dumps(payment) + + if site is not None: + self.parameters['site'] = site + + if uid_type != 'externalId': + self.parameters['by'] = uid_type + + return self.post('/orders/payments/' + str(payment[uid_type]) + '/edit') + + 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 cost_groups(self): + """ + :return: Response + """ + + return self.get('/reference/cost-groups') + + def cost_groups_edit(self, cost_group): + """ + :param cost_group: + :return: Response + """ + self.parameters['costGroup'] = json.dumps(cost_group) + + return self.post('/reference/cost-groups/' + cost_group['code'] + '/edit') + + def cost_items(self): + """ + :return: Response + """ + + return self.get('/reference/cost-items') + + def cost_items_edit(self, cost_item): + """ + :param cost_item: + :return: Response + """ + self.parameters['costItem'] = json.dumps(cost_item) + + return self.post('/reference/delivery-services/' + cost_item['code'] + '/edit') + + def countries(self): + """ + :return: Response + """ + + return self.get('/reference/countries') + + def couriers(self): + """ + :return: Response + """ + + return self.get('/reference/couriers') + + def couriers_create(self, courier): + """ + :param courier: + :return: Response + """ + self.parameters['courier'] = json.dumps(courier) + + return self.post('/reference/couriers/create') + + def couriers_edit(self, courier): + """ + :param courier: + :return: Response + """ + self.parameters['courier'] = json.dumps(courier) + + return self.post('/reference/couriers/' + courier['code'] + '/edit') + + 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 legal_entities(self): + """ + :return: Response + """ + + return self.get('/reference/legal-entities') + + def legal_entities_edit(self, legal_entity): + """ + :param legal_entity: + :return: Response + """ + self.parameters['legalEntity'] = json.dumps(legal_entity) + + return self.post('/reference/legal-entities/' + legal_entity['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 segments(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('/segments') + + 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 product_groups(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/product-groups') + + 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 products_properties(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/properties') + + def tasks(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('/tasks') + + def task_create(self, task, site=None): + """ + :param task: object + :param site: string + :return: Response + """ + self.parameters['task'] = json.dumps(task) + + if site is not None: + self.parameters['site'] = site + + return self.post('/tasks/create') + + def task(self, uid): + """ + :param uid: string + :return: Response + """ + + return self.get('/tasks/' + str(uid)) + + def task_edit(self, task, site=None): + """ + :param task: object + :param site: string + :return: Response + """ + self.parameters['task'] = json.dumps(task) + + if site is not None: + self.parameters['site'] = site + + return self.post('/tasks/' + str(task['id']) + '/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 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)) + + def user_status(self, uid, status): + """ + :param uid: + :param status: + :return: Response + """ + + return self.post('/users/' + str(uid)) + '/' + status diff --git a/setup.py b/setup.py index 61990d0..1be3644 100644 --- a/setup.py +++ b/setup.py @@ -1,16 +1,45 @@ # coding=utf-8 + +""" +Setup file +""" + +import os 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( name='retailcrm', - version='3.0.5', - description='Client for retailCRM API', + version='5.0.0', + description='retailCRM multi version API client', + long_description=read('README.md'), url='https://github.com/retailcrm/api-client-python', author='retailCRM', author_email='integration@retailcrm.ru', keywords='crm, saas, rest, e-commerce', license='MIT', - packages=['retailcrm'], + packages=['retailcrm', 'tests'], 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', + ] ) diff --git a/tests/v3_tests.py b/tests/v3_tests.py new file mode 100644 index 0000000..ba73983 --- /dev/null +++ b/tests/v3_tests.py @@ -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) diff --git a/tests/v4_tests.py b/tests/v4_tests.py new file mode 100644 index 0000000..b444819 --- /dev/null +++ b/tests/v4_tests.py @@ -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) diff --git a/tests/v5_tests.py b/tests/v5_tests.py new file mode 100644 index 0000000..c8fe02f --- /dev/null +++ b/tests/v5_tests.py @@ -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)