Skip to content

Commit

Permalink
Using web.Request instead of importing Request from web_request
Browse files Browse the repository at this point in the history
  • Loading branch information
WisdomPill committed Nov 18, 2020
1 parent 96d525c commit 9f08ad1
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 33 deletions.
3 changes: 1 addition & 2 deletions examples/background_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import aioredis # type: ignore

from aiohttp import web
from aiohttp.web_request import Request


async def websocket_handler(request: Request) -> web.StreamResponse:
async def websocket_handler(request: web.Request) -> web.StreamResponse:
ws = web.WebSocketResponse()
await ws.prepare(request)
request.app["websockets"].append(ws)
Expand Down
3 changes: 1 addition & 2 deletions examples/cli_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
from typing import Optional, Sequence

from aiohttp import web
from aiohttp.web_request import Request


async def display_message(req: Request) -> web.StreamResponse:
async def display_message(req: web.Request) -> web.StreamResponse:
args = req.app["args"]
text = "\n".join([args.message] * args.repeat)
return web.Response(text=text)
Expand Down
5 changes: 2 additions & 3 deletions examples/fake_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from aiohttp.abc import AbstractResolver
from aiohttp.resolver import DefaultResolver
from aiohttp.test_utils import unused_port
from aiohttp.web_request import Request


class FakeResolver:
Expand Down Expand Up @@ -73,10 +72,10 @@ async def start(self) -> Dict[str, int]:
async def stop(self) -> None:
await self.runner.cleanup()

async def on_me(self, request: Request) -> web.StreamResponse:
async def on_me(self, request: web.Request) -> web.StreamResponse:
return web.json_response({"name": "John Doe", "id": "12345678901234567"})

async def on_my_friends(self, request: Request) -> web.StreamResponse:
async def on_my_friends(self, request: web.Request) -> web.StreamResponse:
return web.json_response(
{
"data": [
Expand Down
5 changes: 2 additions & 3 deletions examples/server_simple.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# server_simple.py
from aiohttp import web
from aiohttp.web_request import Request


async def handle(request: Request) -> web.StreamResponse:
async def handle(request: web.Request) -> web.StreamResponse:
name = request.match_info.get("name", "Anonymous")
text = "Hello, " + name
return web.Response(text=text)


async def wshandle(request: Request) -> web.StreamResponse:
async def wshandle(request: web.Request) -> web.StreamResponse:
ws = web.WebSocketResponse()
await ws.prepare(request)

Expand Down
3 changes: 1 addition & 2 deletions examples/web_classview.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import json

from aiohttp import web
from aiohttp.web_request import Request


class MyView(web.View):
Expand All @@ -33,7 +32,7 @@ async def post(self) -> web.StreamResponse:
)


async def index(request: Request) -> web.StreamResponse:
async def index(request: web.Request) -> web.StreamResponse:
txt = """
<html>
<head>
Expand Down
7 changes: 3 additions & 4 deletions examples/web_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pprint import pformat

from aiohttp import web
from aiohttp.web_request import Request

tmpl = """\
<html>
Expand All @@ -17,19 +16,19 @@
</html>"""


async def root(request: Request) -> web.StreamResponse:
async def root(request: web.Request) -> web.StreamResponse:
resp = web.Response(content_type="text/html")
resp.text = tmpl.format(pformat(request.cookies))
return resp


async def login(request: Request) -> None:
async def login(request: web.Request) -> None:
exc = web.HTTPFound(location="/")
exc.set_cookie("AUTH", "secret")
raise exc


async def logout(request: Request) -> None:
async def logout(request: web.Request) -> None:
exc = web.HTTPFound(location="/")
exc.del_cookie("AUTH")
raise exc
Expand Down
9 changes: 4 additions & 5 deletions examples/web_srv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import textwrap

from aiohttp import web
from aiohttp.web_request import Request


async def intro(request: Request) -> web.StreamResponse:
async def intro(request: web.Request) -> web.StreamResponse:
txt = textwrap.dedent(
"""\
Type {url}/hello/John {url}/simple or {url}/change_body
Expand All @@ -24,18 +23,18 @@ async def intro(request: Request) -> web.StreamResponse:
return resp


async def simple(request: Request) -> web.StreamResponse:
async def simple(request: web.Request) -> web.StreamResponse:
return web.Response(text="Simple answer")


async def change_body(request: Request) -> web.StreamResponse:
async def change_body(request: web.Request) -> web.StreamResponse:
resp = web.Response()
resp.body = b"Body changed"
resp.content_type = "text/plain"
return resp


async def hello(request: Request) -> web.StreamResponse:
async def hello(request: web.Request) -> web.StreamResponse:
resp = web.StreamResponse()
name = request.match_info.get("name", "Anonymous")
answer = ("Hello, " + name).encode("utf8")
Expand Down
10 changes: 5 additions & 5 deletions examples/web_srv_route_deco.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import textwrap

from aiohttp import web
from aiohttp.web_request import Request

routes = web.RouteTableDef()


@routes.get("/")
async def intro(request: Request) -> web.StreamResponse:
async def intro(request: web.Request) -> web.StreamResponse:
txt = textwrap.dedent(
"""\
Type {url}/hello/John {url}/simple or {url}/change_body
Expand All @@ -29,20 +28,21 @@ async def intro(request: Request) -> web.StreamResponse:


@routes.get("/simple")
async def simple(request: Request) -> web.StreamResponse:
async def simple(request: web.Request) -> web.StreamResponse:
return web.Response(text="Simple answer")


@routes.get('/change_body')
@routes.get("/change_body")
async def change_body(request: Request) -> web.StreamResponse:
async def change_body(request: web.Request) -> web.StreamResponse:
resp = web.Response()
resp.body = b"Body changed"
resp.content_type = "text/plain"
return resp


@routes.get("/hello")
async def hello(request: Request) -> web.StreamResponse:
async def hello(request: web.Request) -> web.StreamResponse:
resp = web.StreamResponse()
name = request.match_info.get("name", "Anonymous")
answer = ("Hello, " + name).encode("utf8")
Expand Down
9 changes: 4 additions & 5 deletions examples/web_srv_route_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import textwrap

from aiohttp import web
from aiohttp.web_request import Request


async def intro(request: Request) -> web.StreamResponse:
async def intro(request: web.Request) -> web.StreamResponse:
txt = textwrap.dedent(
"""\
Type {url}/hello/John {url}/simple or {url}/change_body
Expand All @@ -25,18 +24,18 @@ async def intro(request: Request) -> web.StreamResponse:
return resp


async def simple(request: Request) -> web.StreamResponse:
async def simple(request: web.Request) -> web.StreamResponse:
return web.Response(text="Simple answer")


async def change_body(request: Request) -> web.StreamResponse:
async def change_body(request: web.Request) -> web.StreamResponse:
resp = web.Response()
resp.body = b"Body changed"
resp.content_type = "text/plain"
return resp


async def hello(request: Request) -> web.StreamResponse:
async def hello(request: web.Request) -> web.StreamResponse:
resp = web.StreamResponse()
name = request.match_info.get("name", "Anonymous")
answer = ("Hello, " + name).encode("utf8")
Expand Down
3 changes: 1 addition & 2 deletions examples/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
from typing import Union

from aiohttp import web
from aiohttp.web_request import Request

WS_FILE = os.path.join(os.path.dirname(__file__), "websocket.html")


async def wshandler(request: Request) -> Union[web.WebSocketResponse, web.Response]:
async def wshandler(request: web.Request) -> Union[web.WebSocketResponse, web.Response]:
resp = web.WebSocketResponse()
available = resp.can_prepare(request)
if not available:
Expand Down

0 comments on commit 9f08ad1

Please sign in to comment.