本文整理汇总了Python中pyramid.response.Response.headers['ETag']方法的典型用法代码示例。如果您正苦于以下问题:Python Response.headers['ETag']方法的具体用法?Python Response.headers['ETag']怎么用?Python Response.headers['ETag']使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyramid.response.Response
的用法示例。
在下文中一共展示了Response.headers['ETag']方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getBadges
# 需要导入模块: from pyramid.response import Response [as 别名]
# 或者: from pyramid.response.Response import headers['ETag'] [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: createProfile
# 需要导入模块: from pyramid.response import Response [as 别名]
# 或者: from pyramid.response.Response import headers['ETag'] [as 别名]
def createProfile(request):
'''Create a new learner profile'''
# get proposed key
uid = None
try:
uid = request.json['identity']['userid']
except ValueError:
return Response(status=400, body='Body is not JSON')
except KeyError:
return Response(status=400, body='Body does not include required field "identity.userid"')
if db.get(uid).exists:
return Response(status=409)
# create stub profile
template = copy.deepcopy(profileSkeleton)
data = util.mergeObjects(template, request.json, protectUid=False)
key = db.new(uid, data=data)
key.store()
res = Response(status=201, json=data)
res.headers['ETag'] = util.genETag(data)
return res
示例3: saveProfile
# 需要导入模块: from pyramid.response import Response [as 别名]
# 或者: from pyramid.response.Response import headers['ETag'] [as 别名]
def saveProfile(request):
'''If authorized, update learner profile with request body'''
key = db.get(request.matchdict['user'])
data = None
# get the post data, if available
try:
data = request.json
except ValueError:
return Response(status=400, body='Body is not JSON')
if not key.exists:
return Response(status=404)
# compute original hash for update comparison
oldHash = util.genETag(key.data)
if_match = request.headers['If-Match'] if 'If-Match' in request.headers.keys() else '*'
if_none_match = request.headers['If-None-Match'] if 'If-None-Match' in request.headers.keys() else None
# if the preconditions pass
if if_match in ['*', oldHash] and if_none_match not in ['*', oldHash]:
# merge objects on PUT
if request.method == 'PUT':
try:
key.data = util.mergeObjects(key.data, data, protectUid=True)
except:
return Response(status=500, body='Failed to merge objects')
# replace object on POST
else:
template = copy.deepcopy(profileSkeleton)
template['identity']['userid'] = request.matchdict['user']
key.data = util.mergeObjects(template, data, protectUid=True)
key.store()
# indicate creation or update
res = Response(json=key.data)
res.headers['ETag'] = util.genETag(key.data)
res.status = 200
return res
# if the precondition fails, return 412
else:
return Response(status=412)
示例4: addBadges
# 需要导入模块: from pyramid.response import Response [as 别名]
# 或者: from pyramid.response.Response import headers['ETag'] [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
示例5: getProfile
# 需要导入模块: from pyramid.response import Response [as 别名]
# 或者: from pyramid.response.Response import headers['ETag'] [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