make api more pythonic, add __doc__ to functions

This commit is contained in:
j 2010-12-04 15:14:54 +01:00
parent 7507b32383
commit 1ac0604b9f
2 changed files with 37 additions and 21 deletions

15
README
View file

@ -25,14 +25,15 @@ pandora client example:
just using the API: just using the API:
api = pandora_client.API("http://localhost:8000/api/") api = pandora_client.API("http://localhost:8000/api/")
r = api.find({ r = api.find(
'query': {'conditions':[]}, query={'conditions':[]},
'keys': ['title', 'year'], keys=['title', 'year'],
'range': [0, 100] range=[0, 100]
}) )
for i in r['data']['items']: for i in r['data']['items']:
... ...
api features and documentation is available at the api end point, forther api documentation is available as python docstrings.
i.e. http://localhost:8000/api/ api.find?
and there is a web api documentation at i.e. http://localhost:8000/api/

View file

@ -55,7 +55,7 @@ class Client(object):
self.api = API(self._config['url'], media_cache=self.media_cache()) self.api = API(self._config['url'], media_cache=self.media_cache())
if 'username' in self._config: if 'username' in self._config:
r = self.api.login({'username':self._config['username'], 'password':self._config['password']}) r = self.api.login(username=self._config['username'], password=self._config['password'])
if r['status']['code'] == 200: if r['status']['code'] == 200:
self.user = r['data']['user'] self.user = r['data']['user']
else: else:
@ -251,16 +251,28 @@ class API(object):
self.media_cache = default_media_cache self.media_cache = default_media_cache
self.url = url self.url = url
r = self._request('api', {}) r = self._request('apidoc', {})
self._actions = r['data']['actions'] self._doc = r['data']['actions']
self._actions = r['data']['actions'].keys()
for a in r['data']['actions']: for a in r['data']['actions']:
setattr(self.__class__, a, self._action(a)) self._add_action(a)
def _action(self, action): def _add_method(self, method, name):
def f(self, data=None): if name is None:
return self._request(action, data) name = method.func_name
return f setattr(self.__class__, name, method)
#return lambda self, data:
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)
method.__doc__ = self._doc[action]
method.func_name = str(action)
self._add_method(method, action)
def _json_request(self, url, form): def _json_request(self, url, form):
try: try:
@ -304,6 +316,8 @@ class API(object):
return self._json_request(self.url, form) return self._json_request(self.url, form)
def uploadVideo(self, filename, data, profile): def uploadVideo(self, filename, data, profile):
if DEBUG:
print filename
i = encode(filename, self.media_cache, profile) i = encode(filename, self.media_cache, profile)
#upload frames #upload frames
@ -322,6 +336,7 @@ class API(object):
url = self.url + 'upload/' + '?profile=' + str(profile) + '&oshash=' + i['oshash'] url = self.url + 'upload/' + '?profile=' + str(profile) + '&oshash=' + i['oshash']
ogg = Firefogg(cj=self._cj, debug=True) ogg = Firefogg(cj=self._cj, debug=True)
ogg.upload(url, i['video'], data) ogg.upload(url, i['video'], data)
if DEBUG:
print "done" print "done"
def uploadData(self, filename, oshash): def uploadData(self, filename, oshash):