2018-03-20 20:32:43 +03:00
|
|
|
# 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):
|
2020-12-15 13:31:03 +03:00
|
|
|
"""RetailCRM API client"""
|
2018-03-20 20:32:43 +03:00
|
|
|
|
|
|
|
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
|
2018-04-22 21:39:27 +03:00
|
|
|
requests_url = base_url + url if not self.parameters else base_url + url + "?" + query_builder(self.parameters)
|
2018-03-20 20:32:43 +03:00
|
|
|
response = requests.get(requests_url, headers={
|
|
|
|
'X-API-KEY': self.api_key})
|
2021-03-17 20:58:34 +03:00
|
|
|
self.parameters = {}
|
2018-03-20 20:32:43 +03:00
|
|
|
|
|
|
|
return Response(response.status_code, response.json())
|
|
|
|
|
2018-04-22 21:39:27 +03:00
|
|
|
def post(self, url, version=True):
|
2018-03-20 20:32:43 +03:00
|
|
|
"""
|
|
|
|
Post request
|
|
|
|
:return: Response
|
|
|
|
"""
|
2018-04-22 21:39:27 +03:00
|
|
|
base_url = self.api_url + '/' + self.api_version if version else self.api_url
|
|
|
|
requests_url = base_url + url
|
2018-03-20 20:32:43 +03:00
|
|
|
response = requests.post(requests_url, data=self.parameters, headers={
|
|
|
|
'X-API-KEY': self.api_key})
|
2021-03-17 20:58:34 +03:00
|
|
|
self.parameters = {}
|
|
|
|
|
2018-03-20 20:32:43 +03:00
|
|
|
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')
|