From bbea2c891ebcf902f01f67f0b413911319cc53c3 Mon Sep 17 00:00:00 2001 From: tikijian Date: Wed, 10 Sep 2014 23:59:23 +0400 Subject: [PATCH] 1 --- Intaro.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ test.py | 8 ++++++++ 2 files changed, 54 insertions(+) create mode 100644 Intaro.py create mode 100644 test.py diff --git a/Intaro.py b/Intaro.py new file mode 100644 index 0000000..7558db6 --- /dev/null +++ b/Intaro.py @@ -0,0 +1,46 @@ +import requests + +class IntaroApy: + """Intaro Api wrapper""" + + apiVersion = '3' + + def __init__(self, crmUrl, apiKey): + self.apiUrl = crmUrl + '/api/v' + IntaroApy.apiVersion + '/' + self.apiKey = apiKey + self.parameters = { 'apiKey': apiKey } + + def requestApi(self, url, method='GET', format='json'): + + #TODO: catch http exceptions + if method == 'GET': + result = requests.get(url, params=self.parameters) + elif method == 'POST': + result = requests.post(url, data=self.parameters) + + statusCode = result.status_code + r = result.json() + + # reset params dict + self.parameters = { 'apiKey': self.apiKey } + + if statusCode > 400 or r.has_key('success') and r['success'] == False : + #TODO: ApiException + pass + + if r.has_key('generatedAt') : + self.generatedAt = r['generatedAt'] + del r['generatedAt'] + + del r['success'] + + return r + + def orderGet(self, id, by='externalId') : + url = self.apiUrl + 'orders/' + str(id) + + if by != 'externalId' : + self.parameters['by'] = by + + return self.requestApi(url) + diff --git a/test.py b/test.py new file mode 100644 index 0000000..053097b --- /dev/null +++ b/test.py @@ -0,0 +1,8 @@ +from Intaro import IntaroApy +from pprint import pprint + +i = IntaroApy('https://g-lights.intarocrm.ru', 'pPsg3F79dj0OYkeYKwogOT9cccaipIKR') + +result = i.orderGet(1342) + +pprint(result)