1- # -*- coding: UTF -8 -*-
1+ # -*- coding: utf -8 -*-
22'''
33 magento.api
44
55 Generic API for magento
66
7- :copyright: (c) 2010 by Sharoon Thomas.
8- :copyright: (c) 2010 by Openlabs Technologies & Consulting (P) LTD
9- :license: AGPLv3, see LICENSE for more details
7+ :license: BSD, see LICENSE for more details
108'''
119
1210PROTOCOLS = []
2422else :
2523 PROTOCOLS .append ('soap' )
2624
25+ from . import rest
26+ try :
27+ import requests
28+ import json
29+ except ImportError :
30+ pass
31+ else :
32+ PROTOCOLS .append ('rest' )
33+
2734from magento .utils import expand_url
2835
2936
@@ -33,7 +40,8 @@ class API(object):
3340 """
3441
3542 def __init__ (self , url , username , password ,
36- version = '1.3.2.4' , full_url = False , protocol = 'xmlrpc' , transport = None ):
43+ version = '1.3.2.4' , full_url = False , protocol = 'xmlrpc' , transport = None ,
44+ verify_ssl = True ):
3745 """
3846 This is the Base API class which other APIs have to subclass. By
3947 default the inherited classes also get the properties of this
@@ -98,6 +106,7 @@ def store_views(self):
98106 :param protocol: 'xmlrpc' and 'soap' are valid values
99107 :param transport: optional xmlrpclib.Transport subclass for
100108 use in xmlrpc requests
109+ :param verify_ssl: for REST API, skip SSL validation if False
101110 """
102111 assert protocol \
103112 in PROTOCOLS , "protocol must be %s" % ' OR ' .join (PROTOCOLS )
@@ -109,6 +118,7 @@ def store_views(self):
109118 self .transport = transport
110119 self .session = None
111120 self .client = None
121+ self .verify_ssl = verify_ssl
112122
113123 def connect (self ):
114124 """
@@ -121,6 +131,10 @@ def connect(self):
121131 self .url , allow_none = True , transport = self .transport )
122132 else :
123133 self .client = ServerProxy (self .url , allow_none = True )
134+ elif self .protocol == 'rest' :
135+ # Use an authentication token as the password
136+ self .client = rest .Client (self .url , self .password ,
137+ verify_ssl = self .verify_ssl )
124138 else :
125139 self .client = Client (self .url )
126140
@@ -134,6 +148,8 @@ def __enter__(self):
134148 if self .protocol == 'xmlrpc' :
135149 self .session = self .client .login (
136150 self .username , self .password )
151+ elif self .protocol == 'rest' :
152+ self .session = True
137153 else :
138154 self .session = self .client .service .login (
139155 self .username , self .password )
@@ -147,7 +163,7 @@ def __exit__(self, type, value, traceback):
147163 """
148164 if self .protocol == 'xmlrpc' :
149165 self .client .endSession (self .session )
150- else :
166+ elif self . protocol == 'soap' :
151167 self .client .service .endSession (self .session )
152168 self .session = None
153169
@@ -157,6 +173,8 @@ def call(self, resource_path, arguments):
157173 """
158174 if self .protocol == 'xmlrpc' :
159175 return self .client .call (self .session , resource_path , arguments )
176+ elif self .protocol == 'rest' :
177+ return self .client .call (resource_path , arguments )
160178 else :
161179 return self .client .service .call (
162180 self .session , resource_path , arguments )
0 commit comments