Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kitakamiooi committed Dec 12, 2015
1 parent ea2aabf commit ef6992d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions handlers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def world_image(self, request):
舰娘游戏中客户端FLASH请求的镇守府图片是根据FLASH本身的URL生成的,需要根据用户所在的镇守府IP为其显示正确的图片。
:param request: aiohttp.web.Request
:return: aiohttp.web.HTTPFound
:return: aiohttp.web.HTTPFound or aiohttp.web.HTTPBadRequest
"""
size = request.match_info['size']
session = await get_session(request)
Expand All @@ -44,7 +44,7 @@ async def api(self, request):
""" 转发客户端和游戏服务器之间的API通信。
:param request: aiohttp.web.Request
:return: aiohttp.web.Response
:return: aiohttp.web.Response or aiohttp.web.HTTPBadRequest
"""
action = request.match_info['action']
session = await get_session(request)
Expand Down
18 changes: 9 additions & 9 deletions handlers/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
包含了登录表单、登录后的跳转、不同的游戏运行模式和注销页面。
"""

import aiohttp.web
import aiohttp_jinja2
from aiohttp.web import HTTPFound
from aiohttp_session import get_session

from auth.kancolle import KancolleAuth, OOIAuthException
Expand All @@ -16,7 +16,7 @@ class FrontEndHandler:
async def form(self, request):
"""展示登录表单。
:param request: web.Request
:param request: aiohttp.web.Request
:return: dict
"""
session = await get_session(request)
Expand All @@ -31,8 +31,8 @@ async def form(self, request):
async def login(self, request):
"""接受登录表单提交的数据,登录后跳转或登录失败后展示错误信息。
:param request: web.Request
:return: web.HTTPFound or web.Response
:param request: aiohttp.web.Request
:return: aiohttp.web.HTTPFound or aiohttp.web.Response
"""
post = await request.post()
session = await get_session(request)
Expand All @@ -52,11 +52,11 @@ async def login(self, request):
session['api_starttime'] = kancolle.api_starttime
session['world_ip'] = kancolle.world_ip
if mode == 2:
return HTTPFound('/kcv')
return aiohttp.web.HTTPFound('/kcv')
elif mode == 3:
return HTTPFound('/poi')
return aiohttp.web.HTTPFound('/poi')
else:
return HTTPFound('/kancolle')
return aiohttp.web.HTTPFound('/kancolle')

except OOIAuthException as e:
context = {'errmsg': e.message, 'mode': mode}
Expand Down Expand Up @@ -88,7 +88,7 @@ async def normal(self, request):
del session['api_token']
del session['api_starttime']
del session['world_ip']
return HTTPFound('/')
return aiohttp.web.HTTPFound('/')

def kcv(self):
pass
Expand All @@ -115,7 +115,7 @@ async def poi(self, request):
del session['api_token']
del session['api_starttime']
del session['world_ip']
return HTTPFound('/')
return aiohttp.web.HTTPFound('/')

def logout(self):
pass
24 changes: 12 additions & 12 deletions handlers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
只接受POST请求,包括login_id和password两个参数,返回用户的内嵌游戏网页地址或游戏FLASH地址。请求缺少参数时返回400错误。
"""

from aiohttp import web
from aiohttp import MultiDict
import aiohttp
import aiohttp.web
import json

from auth.exceptions import OOIAuthException
Expand All @@ -17,14 +17,14 @@ async def get_osapi(self, request):
"""获取用户的内嵌游戏网页地址,返回一个JSON格式的字典。
结果中`status`键值为1时获取成功,`osapi_url`键值为内嵌网页地址;`status`为0时获取失败,`message`键值提供了错误信息。
:param request: web.Request
:return: web.Response
:param request: aiohttp.web.Request
:return: aiohttp.web.Response or aiohttp.web.HTTPBadRequest
"""
data = await request.post()
login_id = data.get('login_id', None)
password = data.get('password', None)
if login_id and password:
headers = MultiDict({'Content-Type': 'application/json'})
headers = aiohttp.MultiDict({'Content-Type': 'application/json'})
kancolle = KancolleAuth(login_id, password)
try:
osapi_url = await kancolle.get_osapi()
Expand All @@ -33,22 +33,22 @@ async def get_osapi(self, request):
except OOIAuthException as e:
result = {'status': 0,
'message': e.message}
return web.Response(body=json.dumps(result).encode(), headers=headers)
return aiohttp.web.Response(body=json.dumps(result).encode(), headers=headers)
else:
return web.HTTPBadRequest()
return aiohttp.web.HTTPBadRequest()

async def get_flash(self, request):
"""获取用户的游戏FLASH地址,返回一个JSON格式的字典。
结果中`status`键值为1时获取成功,`flash_url`键值为游戏FLASH地址;`status`为0时获取失败,`message`键值提供了错误信息。
:param request: web.Request
:return: web.Response
:param request: aiohttp.web.Request
:return: aiohttp.web.Response or aiohttp.web.HTTPBadRequest
"""
data = await request.post()
login_id = data.get('login_id', None)
password = data.get('password', None)
if login_id and password:
headers = MultiDict({'Content-Type': 'application/json'})
headers = aiohttp.MultiDict({'Content-Type': 'application/json'})
kancolle = KancolleAuth(login_id, password)
try:
flash_url = await kancolle.get_flash()
Expand All @@ -57,6 +57,6 @@ async def get_flash(self, request):
except OOIAuthException as e:
result = {'status': 0,
'message': e.message}
return web.Response(body=json.dumps(result).encode(), headers=headers)
return aiohttp.web.Response(body=json.dumps(result).encode(), headers=headers)
else:
return web.HTTPBadRequest()
return aiohttp.web.HTTPBadRequest()
4 changes: 2 additions & 2 deletions ooi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import asyncio

import jinja2
from aiohttp import web
import aiohttp.web
import aiohttp_jinja2
from aiohttp_session import session_middleware
from aiohttp_session.cookie_storage import EncryptedCookieStorage
Expand Down Expand Up @@ -44,7 +44,7 @@ def main():
middlewares = [session_middleware(EncryptedCookieStorage(config.secret_key)), ]

# 初始化应用
app = web.Application(middlewares=middlewares, loop=loop)
app = aiohttp.web.Application(middlewares=middlewares, loop=loop)

# 定义Jinja2模板位置
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(config.template_dir))
Expand Down

0 comments on commit ef6992d

Please sign in to comment.