return new class for each ox.API call
This commit is contained in:
parent
d38da54a17
commit
714729fee7
1 changed files with 86 additions and 83 deletions
169
ox/api.py
169
ox/api.py
|
@ -13,94 +13,97 @@ from .form import MultiPartForm
|
||||||
|
|
||||||
__all__ = ['API']
|
__all__ = ['API']
|
||||||
|
|
||||||
|
def API(url, cj=None):
|
||||||
|
class API(object):
|
||||||
|
__version__ = 0.0
|
||||||
|
__name__ = 'ox'
|
||||||
|
DEBUG = False
|
||||||
|
debuglevel = 0
|
||||||
|
|
||||||
class API(object):
|
def __init__(self, url, cj=None):
|
||||||
__version__ = 0.0
|
if cj:
|
||||||
__name__ = 'ox'
|
self._cj = cj
|
||||||
DEBUG = False
|
else:
|
||||||
debuglevel = 0
|
self._cj = cookielib.CookieJar()
|
||||||
|
self._opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self._cj),
|
||||||
|
urllib2.HTTPHandler(debuglevel=self.debuglevel))
|
||||||
|
self._opener.addheaders = [
|
||||||
|
('User-Agent', '%s/%s' % (self.__name__, self.__version__))
|
||||||
|
]
|
||||||
|
|
||||||
def __init__(self, url, cj=None):
|
self.url = url
|
||||||
if cj:
|
r = self._request('api', {'docs': True})
|
||||||
self._cj = cj
|
self._properties = r['data']['actions']
|
||||||
else:
|
self._actions = r['data']['actions'].keys()
|
||||||
self._cj = cookielib.CookieJar()
|
for a in r['data']['actions']:
|
||||||
self._opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self._cj),
|
self._add_action(a)
|
||||||
urllib2.HTTPHandler(debuglevel=self.debuglevel))
|
|
||||||
self._opener.addheaders = [
|
|
||||||
('User-Agent', '%s/%s' % (self.__name__, self.__version__))
|
|
||||||
]
|
|
||||||
|
|
||||||
self.url = url
|
def _add_method(self, method, name):
|
||||||
r = self._request('api', {'docs': True})
|
if name is None:
|
||||||
self._properties = r['data']['actions']
|
name = method.func_name
|
||||||
self._actions = r['data']['actions'].keys()
|
setattr(self.__class__, name, method)
|
||||||
for a in r['data']['actions']:
|
|
||||||
self._add_action(a)
|
|
||||||
|
|
||||||
def _add_method(self, method, name):
|
def _add_action(self, action):
|
||||||
if name is None:
|
def method(self, *args, **kw):
|
||||||
name = method.func_name
|
if not kw:
|
||||||
setattr(self.__class__, name, method)
|
if args:
|
||||||
|
kw = args[0]
|
||||||
|
else:
|
||||||
|
kw = None
|
||||||
|
return self._request(action, kw)
|
||||||
|
method.__doc__ = self._properties[action]['doc']
|
||||||
|
method.func_name = str(action)
|
||||||
|
self._add_method(method, action)
|
||||||
|
|
||||||
def _add_action(self, action):
|
def _json_request(self, url, form):
|
||||||
def method(self, *args, **kw):
|
result = {}
|
||||||
if not kw:
|
|
||||||
if args:
|
|
||||||
kw = args[0]
|
|
||||||
else:
|
|
||||||
kw = None
|
|
||||||
return self._request(action, kw)
|
|
||||||
method.__doc__ = self._properties[action]['doc']
|
|
||||||
method.func_name = str(action)
|
|
||||||
self._add_method(method, action)
|
|
||||||
|
|
||||||
def _json_request(self, url, form):
|
|
||||||
result = {}
|
|
||||||
try:
|
|
||||||
body = str(form)
|
|
||||||
request = urllib2.Request(str(url))
|
|
||||||
request.add_header('Content-type', form.get_content_type())
|
|
||||||
request.add_header('Content-Length', str(len(body)))
|
|
||||||
request.add_header('Accept-Encoding', 'gzip, deflate')
|
|
||||||
request.add_data(body)
|
|
||||||
f = self._opener.open(request)
|
|
||||||
result = f.read()
|
|
||||||
if f.headers.get('content-encoding', None) == 'gzip':
|
|
||||||
result = gzip.GzipFile(fileobj=StringIO.StringIO(result)).read()
|
|
||||||
result = result.decode('utf-8')
|
|
||||||
return json.loads(result)
|
|
||||||
except urllib2.HTTPError, e:
|
|
||||||
if self.DEBUG:
|
|
||||||
import webbrowser
|
|
||||||
if e.code >= 500:
|
|
||||||
with open('/tmp/error.html', 'w') as f:
|
|
||||||
f.write(e.read())
|
|
||||||
webbrowser.open_new_tab('/tmp/error.html')
|
|
||||||
|
|
||||||
result = e.read()
|
|
||||||
try:
|
try:
|
||||||
|
body = str(form)
|
||||||
|
request = urllib2.Request(str(url))
|
||||||
|
request.add_header('Content-type', form.get_content_type())
|
||||||
|
request.add_header('Content-Length', str(len(body)))
|
||||||
|
request.add_header('Accept-Encoding', 'gzip, deflate')
|
||||||
|
request.add_data(body)
|
||||||
|
f = self._opener.open(request)
|
||||||
|
result = f.read()
|
||||||
|
if f.headers.get('content-encoding', None) == 'gzip':
|
||||||
|
result = gzip.GzipFile(fileobj=StringIO.StringIO(result)).read()
|
||||||
result = result.decode('utf-8')
|
result = result.decode('utf-8')
|
||||||
result = json.loads(result)
|
return json.loads(result)
|
||||||
except:
|
except urllib2.HTTPError, e:
|
||||||
result = {'status':{}}
|
if self.DEBUG:
|
||||||
result['status']['code'] = e.code
|
import webbrowser
|
||||||
result['status']['text'] = str(e)
|
if e.code >= 500:
|
||||||
return result
|
with open('/tmp/error.html', 'w') as f:
|
||||||
except:
|
f.write(e.read())
|
||||||
if self.DEBUG:
|
webbrowser.open_new_tab('/tmp/error.html')
|
||||||
import webbrowser
|
|
||||||
import traceback
|
result = e.read()
|
||||||
traceback.print_exc()
|
try:
|
||||||
if result:
|
result = result.decode('utf-8')
|
||||||
with open('/tmp/error.html', 'w') as f:
|
result = json.loads(result)
|
||||||
f.write(str(result))
|
except:
|
||||||
webbrowser.open_new_tab('/tmp/error.html')
|
result = {'status':{}}
|
||||||
raise
|
result['status']['code'] = e.code
|
||||||
|
result['status']['text'] = str(e)
|
||||||
|
return result
|
||||||
|
except:
|
||||||
|
if self.DEBUG:
|
||||||
|
import webbrowser
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
if result:
|
||||||
|
with open('/tmp/error.html', 'w') as f:
|
||||||
|
f.write(str(result))
|
||||||
|
webbrowser.open_new_tab('/tmp/error.html')
|
||||||
|
raise
|
||||||
|
|
||||||
|
def _request(self, action, data=None):
|
||||||
|
form = MultiPartForm()
|
||||||
|
form.add_field('action', action)
|
||||||
|
if data:
|
||||||
|
form.add_field('data', json.dumps(data))
|
||||||
|
return self._json_request(self.url, form)
|
||||||
|
|
||||||
|
return API(url, cj)
|
||||||
|
|
||||||
def _request(self, action, data=None):
|
|
||||||
form = MultiPartForm()
|
|
||||||
form.add_field('action', action)
|
|
||||||
if data:
|
|
||||||
form.add_field('data', json.dumps(data))
|
|
||||||
return self._json_request(self.url, form)
|
|
||||||
|
|
Loading…
Reference in a new issue