我正在尝试使用 web.py 为 RESTful API 的 URL 末尾的 .{Type} 设置响应类型。
如何将 .JSON、.XML、.HTML、.(whatever) 传递给“Assignments”类或将其设置为某处的值,以便 ServerResponse 可以接收它并以正确的格式响应?
我试过了:
'/assignments(\.[:upper:]+)', 'Assignments'
我正在为我的网址使用以下代码:
urls = (
'/(.*)/', 'redirect',
'/', 'Homepage',
'/assignments', 'Assignments'
)
我有一个班级“作业”:
class Assignments:
def GET(self,responseType):
sentData = web.data()
query = JSON.decode(sentData,'unicode')
# \/ Replace With Code \/
data = query
# /\ Replace with Code /\
return ServerResponse.Send(data,responseType)
def POST(self,responseType):
sentData = web.data()
query = JSON.decode(sentData,'unicode')
# \/ Replace With Code \/
data = query
# /\ Replace with Code /\
return ServerResponse.Send(data,responseType)
还有我的 ServerResponse 类:
class ServerResponse:
@staticmethod
def Send(data, method):
return getattr(ServerResponse, method)(data)
@staticmethod
def JSON(data):
web.header('Content-Type', 'application/json')
response = JSON.encode(data)
return response
@staticmethod
def XML(data):
pass
@staticmethod
def HTML(data):
web.header('Content-Type', 'text/html')
response = "<html>"
response += "<head></head>"
response += "<body>"
response += "{"
for key in data:
response += "%s:%s,\n" % (str(key), str(data[key]))
response += "}"
response += "</body>"
response += "</html>"
return response