本文整理汇总了Python中apis.APIError方法的典型用法代码示例。如果您正苦于以下问题:Python apis.APIError方法的具体用法?Python apis.APIError怎么用?Python apis.APIError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apis
的用法示例。
在下文中一共展示了apis.APIError方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: api_register_user
# 需要导入模块: import apis [as 别名]
# 或者: from apis import APIError [as 别名]
def api_register_user(*,email,name,passwd):
if not name or not name.strip():
raise APIValueError('name')
if not email or not _RE_EMAIL.match(email):
raise APIValueError('email')
if not passwd or not _RE_SHA1.match(passwd):
raise APIValueError('passwd')
users = await User.findAll('email=?',[email])
if len(users) > 0:
raise APIError('register:failed','email','Email is already in use')
uid = next_id()
sha1_passwd = '%s:%s' % (uid,passwd)
encrypt_passwd = hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest()
image = 'https://1.gravatar.com/avatar/%s?s=200&r=pg&d=mm'
user = User(id=uid,name=name.strip(),email=email,passwd=encrypt_passwd,image=image % hashlib.md5(email.encode('utf-8')).hexdigest())
await user.save()
# make session cookie
r = web.Response()
r.set_cookie(COOKIE_NAME,user2cookie(user,86400),max_age=86400,httponly=True)
user.passwd = '******'
r.content_type = 'application/json'
r.body = json.dumps(user,ensure_ascii=False).encode('utf-8')
return r
示例2: api_register_user
# 需要导入模块: import apis [as 别名]
# 或者: from apis import APIError [as 别名]
def api_register_user(*, email, name, password):
if not name or not name.strip():
raise APIValueError('name')
if not email or not _RE_EMAIL.match(email):
raise APIValueError('email')
if not password or not _RE_SHA1.match(password):
raise APIValueError('password')
users = yield from User.find_all('email=?', [email])
if len(users) > 0:
raise APIError('Register failed', 'email', 'Email is already in use.')
uid = next_id()
sha1_password = '{}:{}'.format(uid, password)
logging.info('register password:{}, sha1_password:{}'.format(password, sha1_password))
user = User(id=uid, name= name.strip(), email= email, password = hashlib.sha1(sha1_password.encode('utf-8')).hexdigest(), image='http://www.gravatar.com/avatar/{}?d=mm&s=120'.format(hashlib.md5(email.encode('utf-8')).hexdigest()))
yield from user.save()
r = web.Response()
r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
user.password = '*' * 8
r.content_type = 'application/json'
r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
return r
示例3: authenticate
# 需要导入模块: import apis [as 别名]
# 或者: from apis import APIError [as 别名]
def authenticate():
i = ctx.request.input()
email = i.email.strip().lower()
password = i.password
remember = i.remember
user = User.find_first('where email=?', email)
if user is None:
raise APIError('auth:failed', 'email', 'Invalid email.')
elif user.password != password:
raise APIError('auth:failed', 'password', 'Invalid password.')
max_age = 604800 if remember == 'true' else None
cookie = make_signed_cookie(user.id, user.password, max_age)
ctx.response.set_cookie(_COOKIE_NAME, cookie)
user.password = '*******'
return user
示例4: register_uauser
# 需要导入模块: import apis [as 别名]
# 或者: from apis import APIError [as 别名]
def register_uauser():
i = ctx.request.input(name='', email='', password='')
name = i.name.strip()
email = i.email.strip().lower()
password = i.password
if not name:
raise APIValueError('name')
if not email or not _RE_EMAIL.match(email):
raise APIValueError('email')
if not password or not _RE_MD5.match(password):
raise APIValueError('password')
user = User.find_first('where email=?', email)
if user:
raise APIError('register:failed', 'email', 'Email is already in use.')
user = User(name=name, email=email, password=password,
image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email).hexdigest())
user.insert()
cookie = make_signed_cookie(user.id, user.password, None)
ctx.response.set_cookie(_COOKIE_NAME, cookie)
return user
示例5: api_register_user
# 需要导入模块: import apis [as 别名]
# 或者: from apis import APIError [as 别名]
def api_register_user(*, email, name, passwd):
'''
????,?????cookie
'''
if not name or not name.strip():
raise APIValueError('name')
if not email or not _RE_EMAIL.match(email):
raise APIValueError('email')
if not passwd or not _RE_SHA1.match(passwd):
raise APIValueError('passwd')
users = await User.findAll('email=?', [email])
if len(users) > 0:
raise APIError('register:failed', 'email', 'Email is already in use.')
uid = next_id()
sha1_passwd = '%s:%s' % (uid, passwd)
# image ????????gravatar??????
user = User(id = uid, name = name.strip(), email = email, passwd = hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image = 'http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest())
await user.save()
# make session cookie:
r = web.Response()
r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age = 86400, httponly = True)
user.passwd = '******'
r.content_type = 'application/json'
r.body = json.dumps(user, ensure_ascii = False).encode('utf-8')
return r
示例6: api_register_user
# 需要导入模块: import apis [as 别名]
# 或者: from apis import APIError [as 别名]
def api_register_user(*, email, name, passwd):
if not name or not name.strip():
raise APIValueError('name', 'Invalid name.')
if not email or not _RE_EMAIL.match(email):
raise APIValueError('email', 'Invalid email.')
if not passwd or not _RE_SHA1.match(passwd):
raise APIValueError('passwd', 'Invalid password.')
users = await User.findAll('email=?', [email])
if len(users) > 0:
raise APIError('register: failed', 'email', 'Email is already in use.')
uid = next_id()
sha1_passwd = '%s:%s' % (uid, passwd)
user = User(
id=uid,
name=name.strip(),
email=email,
passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(),
image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest()
)
await user.save()
# make session cookie:
r = web.Response()
r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
user.passwd = '******'
r.content_type = 'application/json'
r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
return r
示例7: __call__
# 需要导入模块: import apis [as 别名]
# 或者: from apis import APIError [as 别名]
def __call__(self,request):
kw = None
if self._has_var_kw_arg or self._has_named_kw_args or self._required_kw_args:
if request.method == 'POST':
if not request.content_type:
return web.HTTPBadRequest(text='Missing content_type')
ct = request.content_type.lower()
if ct.startswith('application/json'):
params = await request.json()
if not isinstance(params,dict):
return web.HTTPBadRequest(text='Json body mmust be object')
kw = params
elif ct.startswith('application/x-www-form-urlencoded') or ct.startswith('multipart/form-data'):
params = await request.post()
kw = dict(**params)
else:
return web.HTTPBadRequest(text = 'Unsupported content_type:%s' % request.content_type)
if request.method == 'GET':
qs = request.query_string
if qs:
kw = dict()
for k,v in parse.parse_qs(qs,True).items():
kw[k] = v[0]
if kw is None:
kw = dict(**request.match_info)
else:
# ????????????????request????????????????
if not self._has_var_kw_arg and self._named_kw_args:
#remove all unamed kw
copy = dict()
for name in self._named_kw_args:
if name in kw:
copy[name] = kw[name]
kw = copy
#check named arg
for k,v in request.match_info.items():
if k in kw:
logging.warning('Duplicate arg name in named arg and kw args:%s' % k)
kw[k] = v
if self._has_request_arg:
kw['request'] = request
#check required kw
if self._required_kw_args:
for name in self._required_kw_args:
if not name in kw:
return web.HTTPBadRequest(text='Missing argument : %s' % name)
logging.info('call with args:%s' % str(kw))
try:
r = await self._func(**kw)
return r
except APIError as e:
raise dict(error=e.error,data=e.data,message=e.message)
#??????????
示例8: __call__
# 需要导入模块: import apis [as 别名]
# 或者: from apis import APIError [as 别名]
def __call__(self, request):
kw = None
if self._has_var_kw_arg or self._has_named_kw_args or self._requested_kw_args:
if request.method == 'POST':
if not request.content_type:
return web.HTTPBadRequest('Missing Content-Type')
ct = request.content_type.lower()
if ct.startswith('application/json'):
params = yield from request.json()
if not isinstance(params, dict):
return web.HTTPBadRequest('JSON body must be object.')
kw = params
elif ct.startswith('application/x-www-form-urlencoded') or ct.startswith('multipart/form-data'):
params = yield from request.post()
kw = dict(**params)
else:
return web.HTTPBadRequest('Unsupported Content-Type: {}'.format(request.content_type))
if request.method == 'GET':
qs = request.query_string
if qs:
kw = dict()
for k, v in parse.parse_qs(qs, True).items():
kw[k] = v[0]
if kw is None:
kw = dict(**request.match_info)
else:
if not self._has_var_kw_arg and self._named_kw_args:
#remove all unamed kw:
copy = dict()
for name in self._named_kw_args:
if name in kw:
copy[name] = kw[name]
kw = copy
#check named arg:
for k, v in request.match_info.items():
if k in kw:
logging.warning('Duplicate arg name in named arg and args:{}'.format(k))
kw[k] = v
if self._has_request_arg:
kw['request'] = request
#check required kw:
if self._requested_kw_args:
for name in self._requested_kw_args:
if not name in kw:
return web.HTTPBadRequest('Missing argument:{}'.format(name))
logging.info('call with args: {}'.format(str(kw)))
try:
r = yield from self._func(**kw)
return r
except APIError as e:
return dict(error = e.error, data = e.data, message = e.message)
示例9: api_register_user
# 需要导入模块: import apis [as 别名]
# 或者: from apis import APIError [as 别名]
def api_register_user(*, email, name, passwd): # ??????????????
# ????????
# ?????name
if not name or not name.strip(): # s.strip(rm)??????s?????????????rm???????
# ??rm????????????
raise APIValueError('name')
# ??email?????????????
if not email or not _RE_EMAIL.match(email):
raise APIValueError('email')
# ??passwd???SHA1??????????
if not passwd or not _RE_SHA1.match(passwd):
raise APIValueError('passwd')
# ?????????????email
users = yield from User.findAll('email=?', [email])
# users??????????????????email???????
if len(users) > 0:
raise APIError('register:failed', 'email', 'Email is already in use.')
# ???????email???????????
uid = next_id() # next_id?models????????????????????id??????????????
sha1_passwd = '%s:%s' % (uid, passwd) # ???id?????
# ????????????????????
# unicode???????????????????utf8??
# hashlib.sha1()??????????sha1?
# hash.hexdigest()???hash?????16????????
# ?????sha1?????????md5??
# Gravatar???????????????????????????
user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest())
yield from user.save() # ????????????
# make session cookie:
r = web.Response()
# ?????????cookie(???????????????????????)
# http???????????,?????????????????.
# ??????????????Cookies?????,????????????????
# user2cookie????cookie?????????
# max_age?cookie???????,????.??????,???????cookie.????????
# ?????????24??
r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
user.passwd = '******' # ??????????*
# ??content_type???data_factory????????
r.content_type = 'application/json'
# json.dump?????????json??
r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
return r
# API?????
示例10: __call__
# 需要导入模块: import apis [as 别名]
# 或者: from apis import APIError [as 别名]
def __call__(self, request):
kw = None
if self._has_var_kw_arg or self._has_named_kw_args or self._required_kw_args:
if request.method == 'POST':
if not request.content_type:
return web.HTTPBadRequest('Missing Content-Type.')
ct = request.content_type.lower()
if ct.startswith('application/json'):
params = yield from request.json()
if not isinstance(params, dict):
return web.HTTPBadRequest('JSON body must be object.')
kw = params
elif ct.startswith('application/x-www-form-urlencoded') or ct.startswith('multipart/form-data'):
params = yield from request.post()
kw = dict(**params)
else:
return web.HTTPBadRequest('Unsupported Content-Type: %s' % request.content_type)
if request.method == 'GET':
qs = request.query_string
if qs:
kw = dict()
for k, v in parse.parse_qs(qs, True).items():
kw[k] = v[0]
if kw is None:
kw = dict(**request.match_info)
else:
if not self._has_var_kw_arg and self._named_kw_args:
# remove all unamed kw:
copy = dict()
for name in self._named_kw_args:
if name in kw:
copy[name] = kw[name]
kw = copy
# check named arg:
for k, v in request.match_info.items():
if k in kw:
logging.warning('Duplicate arg name in named arg and kw args: %s' % k)
kw[k] = v
if self._has_request_arg:
kw['request'] = request
# check required kw:
if self._required_kw_args:
for name in self._required_kw_args:
if not name in kw:
return web.HTTPBadRequest('Missing argument: %s' % name)
logging.info('call with args: %s' % str(kw))
try:
r = yield from self._func(**kw)
return r
except APIError as e:
return dict(error=e.error, data=e.data, message=e.message)
示例11: __call__
# 需要导入模块: import apis [as 别名]
# 或者: from apis import APIError [as 别名]
def __call__(self, request):
kw = None
if self._has_var_kw_arg or self._has_named_kw_args or self._required_kw_args:
if request.method == 'POST':
if not request.content_type:
return web.HTTPBadRequest('Missing Content Type.')
ct = request.content_type.lower()
if ct.startswith('application/json'):
params = await request.json()
if not isinstance(params, dict):
return web.HTTPBadRequest('JSON body must be object.')
kw = params
elif ct.startswith('application/x-www-form-urlencoded') or ct.startswith('multipart/form-data'):
params = await request.post()
kw = dict(**params)
else:
return web.HTTPBadRequest('Unsupported Content-Type: %s' % request.content_type)
if request.method == 'GET':
qs = request.query_string
if qs:
kw = dict()
for k, v in parse.parse_qs(qs, True).items():
kw[k] = v[0]
if kw is None:
kw = dict(**request.match_info)
else:
if not self._has_var_kw_arg and self._named_kw_args:
# remove all unamed kw:
copy = dict()
for name in self._named_kw_args:
if name in kw:
copy[name] = kw[name]
kw = copy
# check named arg:
for k, v in request.match_info.items():
if k in kw:
logging.warning('Duplicate arg name in named arg and kw args: %s' % k)
kw[k] = v
if self._has_request_arg:
kw['request'] = request
# check required kw:
if self._required_kw_args:
for name in self._required_kw_args:
if not name in kw:
return web.HTTPBadRequest('Missing argument: %s' % name)
logging.info('call with args: %s' % str(kw))
try:
r = await self._func(**kw)
return r
except APIError as e:
return dict(error=e.error, data=e.data, message=e.message)