python-ox/ox/api.py

114 lines
3.5 KiB
Python
Raw Normal View History

2011-10-13 17:28:18 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
# GPL 2011
2011-10-21 16:44:45 +00:00
from __future__ import with_statement
2011-10-13 17:28:18 +00:00
import cookielib
import gzip
import StringIO
2011-10-13 17:28:18 +00:00
import urllib2
2013-11-03 15:39:57 +00:00
from . import __version__
2011-10-13 17:28:18 +00:00
from .utils import json
from .form import MultiPartForm
2013-11-03 15:39:57 +00:00
__all__ = ['getAPI', 'API']
2011-10-13 17:28:18 +00:00
2013-11-03 15:39:57 +00:00
def getAPI(url, cj=None):
class A(API):
pass
return A(url, cj)
2011-10-13 17:28:18 +00:00
2013-11-03 15:39:57 +00:00
class API(object):
__version__ = __version__
__name__ = 'ox'
DEBUG = False
debuglevel = 0
2011-10-13 17:28:18 +00:00
2013-11-03 15:39:57 +00:00
def __init__(self, url, cj=None):
if cj:
self._cj = cj
else:
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__))
]
2011-10-13 17:28:18 +00:00
2013-11-03 15:39:57 +00:00
self.url = url
r = self._request('api', {'docs': True})
self._properties = r['data']['actions']
self._actions = r['data']['actions'].keys()
for a in r['data']['actions']:
self._add_action(a)
2011-10-13 17:28:18 +00:00
2013-11-03 15:39:57 +00:00
def _add_method(self, method, name):
if name is None:
name = method.func_name
setattr(self.__class__, name, method)
2011-10-13 17:28:18 +00:00
2013-11-03 15:39:57 +00:00
def _add_action(self, action):
def method(self, *args, **kw):
if not kw:
if args:
kw = args[0]
else:
kw = None
return self._request(action, kw)
if 'doc' in self._properties[action]:
method.__doc__ = self._properties[action]['doc']
2013-11-03 15:39:57 +00:00
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()
2011-10-13 17:28:18 +00:00
try:
2011-10-13 18:07:01 +00:00
result = result.decode('utf-8')
2013-11-03 15:39:57 +00:00
result = json.loads(result)
2011-10-13 17:28:18 +00:00
except:
2013-11-03 15:39:57 +00:00
result = {'status':{}}
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
2013-11-02 16:40:01 +00:00
2013-11-03 15:39:57 +00:00
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)
2011-10-13 17:28:18 +00:00