本文整理汇总了Python中pyramid.response.Response.json方法的典型用法代码示例。如果您正苦于以下问题:Python Response.json方法的具体用法?Python Response.json怎么用?Python Response.json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyramid.response.Response
的用法示例。
在下文中一共展示了Response.json方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getBadges
# 需要导入模块: from pyramid.response import Response [as 别名]
# 或者: from pyramid.response.Response import json [as 别名]
def getBadges(req):
'''Retrieve a user's badge information'''
key = db.get(req.matchdict['user'])
res = Response(status=404)
if key.exists:
# generate proper response body
body = None
if 'type' not in req.matchdict:
body = {'badges': key.data['badges']}
elif req.matchdict['type'] in ['achieved','inprogress','desired']:
badgeType = 'inProgress' if req.matchdict['type'] == 'inprogress' else req.matchdict['type']
body = {badgeType: key.data['badges'][badgeType]}
if body != None:
hash = util.genETag(body)
if_none_match = req.headers['If-None-Match'] if 'If-None-Match' in req.headers.keys() else None
if if_none_match not in ['*',hash]:
res.status = 200
res.content_type = 'application/json'
res.headers['ETag'] = hash
res.json = body
else:
res.status = 304
return res
示例2: addBadges
# 需要导入模块: from pyramid.response import Response [as 别名]
# 或者: from pyramid.response.Response import json [as 别名]
def addBadges(req):
'''Modify a list of badges'''
key = db.get(req.matchdict['user'])
# only take the given lists
if req.matchdict['type'] not in ['achieved','inprogress','desired']:
return Response(status=404)
badgeType = 'inProgress' if req.matchdict['type'] == 'inprogress' else req.matchdict['type']
# retrieve data
data = None
try:
data = req.json
if not isinstance(data, list):
raise IndexError
except ValueError:
return Response(status=400, body='Body is not JSON')
except IndexError:
return Response(status=400, body='Body must be an array')
oldHash = util.genETag({badgeType: key.data['badges'][badgeType]})
if_match = req.headers['If-Match'] if 'If-Match' in req.headers.keys() else '*'
if_none_match = req.headers['If-None-Match'] if 'If-None-Match' in req.headers.keys() else None
# if the preconditions pass
if if_match in ['*', oldHash] and if_none_match not in ['*', oldHash]:
if req.method == 'PUT':
key.data['badges'][badgeType] = key.data['badges'][badgeType] + data
else:
key.data['badges'][badgeType] = data
key.store()
res = Response(status=200)
res.json = {badgeType: key.data['badges'][badgeType]}
res.headers['ETag'] = util.genETag(res.json)
res.content_type = 'application/json'
else:
res.status = 412
return res
示例3: getProfile
# 需要导入模块: from pyramid.response import Response [as 别名]
# 或者: from pyramid.response.Response import json [as 别名]
def getProfile(request):
'''If authorized, respond with learner profile from db'''
key = db.get(request.matchdict['user'])
res = Response()
if key.exists:
hash = util.genETag(key.data)
if_none_match = request.headers['If-None-Match'] if 'If-None-Match' in request.headers.keys() else None
if if_none_match in ['*', hash]:
res.status = 304
else:
res.status = 200
res.headers['ETag'] = hash
if request.method == 'GET':
res.json = key.data
res.content_type = 'application/json'
else:
res.status = 404
return res