浏览代码

Rearrange functions in api.py.

pull/24/head
Ben Kurtovic 9 年前
父节点
当前提交
6d87ad41ed
共有 1 个文件被更改,包括 24 次插入24 次删除
  1. +24
    -24
      copyvios/api.py

+ 24
- 24
copyvios/api.py 查看文件

@@ -6,12 +6,6 @@ from .sites import get_sites

__all__ = ["format_api_error", "handle_api_request"]

_HOOKS = {
"compare": _hook_check,
"search": _hook_check,
"sites": _hook_sites,
}

_CHECK_ERRORS = {
"no search method": "Either 'use_engine' or 'use_links' must be true",
"no URL": "The parameter 'url' is required for URL comparisons",
@@ -49,24 +43,6 @@ def format_api_error(code, info):
info = info.encode("utf8")
return {"status": "error", "error": {"code": code, "info": info}}

def handle_api_request():
query = Query()
if query.version:
try:
query.version = int(query.version)
except ValueError:
info = "The version string is invalid: {0}".format(query.version)
return format_api_error("invalid_version", info)
else:
query.version = 1

if query.version == 1:
action = query.action.lower() if query.action else ""
return _HOOKS.get(action, _hook_default)(query)

info = "The API version is unsupported: {0}".format(query.version)
return format_api_error("unsupported_version", info)

def _hook_default(query):
info = u"Unknown action: '{0}'".format(query.action.lower())
return format_api_error("unknown_action", info)
@@ -114,3 +90,27 @@ def _hook_check(query):
def _hook_sites(query):
langs, projects = get_sites()
return {"status": "ok", "langs": langs, "projects": projects}

_HOOKS = {
"compare": _hook_check,
"search": _hook_check,
"sites": _hook_sites,
}

def handle_api_request():
query = Query()
if query.version:
try:
query.version = int(query.version)
except ValueError:
info = "The version string is invalid: {0}".format(query.version)
return format_api_error("invalid_version", info)
else:
query.version = 1

if query.version == 1:
action = query.action.lower() if query.action else ""
return _HOOKS.get(action, _hook_default)(query)

info = "The API version is unsupported: {0}".format(query.version)
return format_api_error("unsupported_version", info)

正在加载...
取消
保存