mini api documentation framework

This commit is contained in:
j 2010-01-26 07:54:05 +05:30
parent cfc913723c
commit c3b589b90c
2 changed files with 140 additions and 57 deletions

View file

@ -29,6 +29,8 @@ from oxuser.views import api_login, api_logout, api_register, api_recover, api_p
def api(request): def api(request):
if not 'function' in request.POST:
return apidoc(request)
function = request.POST['function'] function = request.POST['function']
#FIXME: possible to do this in f #FIXME: possible to do this in f
#data = json.loads(request.POST['data']) #data = json.loads(request.POST['data'])
@ -44,7 +46,7 @@ def api(request):
def api_hello(request): def api_hello(request):
''' '''
function: helo api('hello', functio(result) { result.user })
''' '''
response = {'status': 200, 'statusText': 'ok'} response = {'status': 200, 'statusText': 'ok'}
response.user = request.user.json() response.user = request.user.json()
@ -194,7 +196,6 @@ def _parse_query(request):
def api_find(request): def api_find(request):
''' '''
function: find
data: {'q': query, 's': sort, 'r': range} data: {'q': query, 's': sort, 'r': range}
query: query string, can contain field:search more on query syntax at query: query string, can contain field:search more on query syntax at
@ -271,8 +272,6 @@ def api_find(request):
def api_getItem(request): def api_getItem(request):
''' '''
function: getItem
api('getItem', id) api('getItem', id)
return item with id return item with id
''' '''
@ -283,45 +282,92 @@ def api_getItem(request):
return render_to_json_response(response) return render_to_json_response(response)
def api_editItem(request): def api_editItem(request):
response = {'status': 500, 'statusText': 'not implemented'} '''
return render_to_json_response(response) api('editItem', {key: value})
def api_removeItem(request): {'status': 500, 'statusText': 'not implemented'}
response = {'status': 500, 'statusText': 'not implemented'} '''
return render_to_json_response(response)
def api_addLayer(request):
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_removeLayer(request):
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_editLayer(request):
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_addListItem(request):
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_removeListItem(request):
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_addList(request):
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_editList(request):
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_removeList(request):
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response) return render_to_json_response(response)
def api_removeItem(request):
''' '''
POST update api('removeItem', itemId)
> files: { {'status': 500, 'statusText': 'not implemented'}
oshash: { 'path': .., ..},
oshash: { 'path': .., ..},
}
''' '''
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_addLayer(request):
'''
api('addItem', {key: value})
{'status': 500, 'statusText': 'not implemented'}
'''
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_removeLayer(request):
'''
api('removeItem', layerId)
{'status': 500, 'statusText': 'not implemented'}
'''
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_editLayer(request):
'''
api('editLayer', {key: value})
{'status': 500, 'statusText': 'not implemented'}
'''
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_addListItem(request):
'''
api('addListItem', {key: value})
{'status': 500, 'statusText': 'not implemented'}
'''
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_removeListItem(request):
'''
api('removeListItem', {key: value})
{'status': 500, 'statusText': 'not implemented'}
'''
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_addList(request):
'''
api('addList', {key: value})
{'status': 500, 'statusText': 'not implemented'}
'''
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_editList(request):
'''
api('editList', {key: value})
{'status': 500, 'statusText': 'not implemented'}
'''
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
def api_removeList(request):
'''
api('removeList', {key: value})
{'status': 500, 'statusText': 'not implemented'}
'''
response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response)
#@login_required_json #@login_required_json
def api_update(request): def api_update(request):
'''
params:
archive = string
files = json
'''
print "update request" print "update request"
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
archive = data['archive'] archive = data['archive']
@ -357,12 +403,14 @@ def api_update(request):
response['rename'] = rename response['rename'] = rename
return render_to_json_response(response) return render_to_json_response(response)
def api_upload(request): #video, timeline, frame def api_upload(request): #video, timeline, frame
'''
upload video, timeline or frame
'''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response) return render_to_json_response(response)
def api_file(request): #FIXME: should this be file.files. or part of update def api_editFile(request): #FIXME: should this be file.files. or part of update
''' '''
change file / imdb link change file / imdb link
''' '''
@ -370,10 +418,20 @@ def api_file(request): #FIXME: should this be file.files. or part of update
return render_to_json_response(response) return render_to_json_response(response)
def api_parse(request): #parse path and return info def api_parse(request): #parse path and return info
response = {'status': 500, 'statusText': 'not implemented'} '''
api('guess', {path:'Foobar/The Matrix.avi'})
{imdb: ...}
'''
path = json.loads(request.POST['data'])['path']
response = utils.parsePath(path)
response = {'status': 500, 'statusText': 'ok', data: response}
return render_to_json_response(response) return render_to_json_response(response)
def api_guess(request): #guess imdb based on title, director, year def api_guess(request): #guess imdb based on title, director, year
'''
api('guess', {title:'The Matrix'})
{imdb: ...}
'''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': 500, 'statusText': 'not implemented'}
return render_to_json_response(response) return render_to_json_response(response)
@ -382,15 +440,6 @@ def api_guess(request): #guess imdb based on title, director, year
#FIXME: old stuff below... #FIXME: old stuff below...
''' '''
GET info?oshash=a41cde31c581e11d GET info?oshash=a41cde31c581e11d
@ -563,8 +612,44 @@ def add_video(request, oshash):
return render_to_json_response(response) return render_to_json_response(response)
def file_parse(request):
response = utils.parsePath(request.POST['path'])
return render_to_json_response(response)
def apidoc(request):
'''
this is used for online documentation at http://127.0.0.1:8000/api/
'''
import sys
def trim(docstring):
if not docstring:
return ''
# Convert tabs to spaces (following the normal Python rules)
# and split into a list of lines:
lines = docstring.expandtabs().splitlines()
# Determine minimum indentation (first line doesn't count):
indent = sys.maxint
for line in lines[1:]:
stripped = line.lstrip()
if stripped:
indent = min(indent, len(line) - len(stripped))
# Remove indentation (first line is special):
trimmed = [lines[0].strip()]
if indent < sys.maxint:
for line in lines[1:]:
trimmed.append(line[indent:].rstrip())
# Strip off trailing and leading blank lines:
while trimmed and not trimmed[-1]:
trimmed.pop()
while trimmed and not trimmed[0]:
trimmed.pop(0)
# Return a single string:
return '\n'.join(trimmed)
functions = filter(lambda x: x.startswith('api_'), globals().keys())
api = []
for f in sorted(functions):
api.append({
'name': f[4:],
'doc': trim(globals()[f].__doc__).replace('\n', '<br>\n')
})
context = RequestContext(request, {'api': api})
return render_to_response('api.html', context)

View file

@ -14,7 +14,6 @@ import models
def api_login(request): def api_login(request):
''' '''
function: login
data: {'username': username, 'password': password} data: {'username': username, 'password': password}
username/password to login username/password to login
@ -38,7 +37,7 @@ def api_login(request):
def api_logout(request): def api_logout(request):
''' '''
function: logout loggs out currenly logged in user
''' '''
response = {'status': 200, 'statusText': 'logged out'} response = {'status': 200, 'statusText': 'logged out'}
if request.user.is_authenticated(): if request.user.is_authenticated():
@ -47,7 +46,6 @@ def api_logout(request):
def api_register(request): def api_register(request):
''' '''
function: register
data: {'username': username, 'password': password, 'email': email} data: {'username': username, 'password': password, 'email': email}
username username