-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspree_osv.py
57 lines (50 loc) · 1.74 KB
/
spree_osv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from osv import osv, fields
import time
import datetime
import netsvc
import urllib
import pycurl
from StringIO import StringIO
import base64
try:
import json
except:
raise osv.except_osv(_('Error!'), _('The json module is not installed'))
from base_external_referentials import external_osv
from tools.translate import _
class Connection(object):
def __init__(self, location, username, password, debug=False):
#Append / if not there
if not location[-1] == '/':
location += '/'
self.corelocation = location
self.location = location + "api/"
self.username = username
self.password = password
self.debug = debug
self.result = {}
self.logger = netsvc.Logger()
def connect(self):
curl = pycurl.Curl()
curl.setopt(curl.USERPWD, '%s:%s' % (str(self.username), str(self.password)))
self.ser = curl
return True
def call(self, method, *arguments):
if arguments:
arguments = list(arguments)[0]
else:
arguments = []
response_buffer = StringIO()
url = self.location + method
self.ser.setopt(pycurl.URL, str(url))
self.ser.setopt(pycurl.WRITEFUNCTION, response_buffer.write)
self.ser.perform()
self.ser.close()
data = response_buffer.getvalue()
data = json.loads(data)
return data
class spree_osv(external_osv.external_osv):
DEBUG = False
def external_connection(self, cr, uid, referential, DEBUG=False):
attr_conn = Connection(referential.location, referential.apiusername, referential.apipass, DEBUG)
return attr_conn.connect() and attr_conn or False