from starlette.responses import Response
-class App:
- def __init__(self, scope):
- assert scope['type'] == 'http'
- self.scope = scope
-
- async def __call__(self, receive, send):
- request = Request(self.scope, receive)
- content = '%s %s' % (request.method, request.url.path)
- response = Response(content, media_type='text/plain')
- await response(self.scope, receive, send)
+async def app(scope, receive, send):
+ assert scope['type'] == 'http'
+ request = Request(scope, receive)
+ content = '%s %s' % (request.method, request.url.path)
+ response = Response(content, media_type='text/plain')
+ await response(scope, receive, send)
```
Requests present a mapping interface, so you can use them in the same
from starlette.requests import Request
from starlette.responses import Response
-
-class App:
- def __init__(self, scope):
- assert scope['type'] == 'http'
- self.scope = scope
-
- async def __call__(self, receive, send):
- request = Request(self.scope, receive)
- body = b''
- async for chunk in request.stream():
- body += chunk
- response = Response(body, media_type='text/plain')
- await response(self.scope, receive, send)
+
+async def app(scope, receive, send):
+ assert scope['type'] == 'http'
+ request = Request(scope, receive)
+ body = b''
+ async for chunk in request.stream():
+ body += chunk
+ response = Response(body, media_type='text/plain')
+ await response(scope, receive, send)
```
If you access `.stream()` then the byte chunks are provided without storing
from starlette.responses import Response
-class App:
- def __init__(self, scope):
- assert scope['type'] == 'http'
- self.scope = scope
-
- async def __call__(self, receive, send):
- response = Response('Hello, world!', media_type='text/plain')
- await response(self.scope, receive, send)
+async def app(scope, receive, send):
+ assert scope['type'] == 'http'
+ response = Response('Hello, world!', media_type='text/plain')
+ await response(scope, receive, send)
```
#### Set Cookie
from starlette.responses import HTMLResponse
-class App:
- def __init__(self, scope):
- assert scope['type'] == 'http'
- self.scope = scope
-
- async def __call__(self, receive, send):
- response = HTMLResponse('<html><body><h1>Hello, world!</h1></body></html>')
- await response(self.scope, receive, send)
+async def app(scope, receive, send):
+ assert scope['type'] == 'http'
+ response = HTMLResponse('<html><body><h1>Hello, world!</h1></body></html>')
+ await response(scope, receive, send)
```
### PlainTextResponse
from starlette.responses import PlainTextResponse
-class App:
- def __init__(self, scope):
- assert scope['type'] == 'http'
- self.scope = scope
-
- async def __call__(self, receive, send):
- response = PlainTextResponse('Hello, world!')
- await response(self.scope, receive, send)
+async def app(scope, receive, send):
+ assert scope['type'] == 'http'
+ response = PlainTextResponse('Hello, world!')
+ await response(scope, receive, send)
```
### JSONResponse
from starlette.responses import JSONResponse
-class App:
- def __init__(self, scope):
- assert scope['type'] == 'http'
- self.scope = scope
-
- async def __call__(self, receive, send):
- response = JSONResponse({'hello': 'world'})
- await response(self.scope, receive, send)
+async def app(scope, receive, send):
+ assert scope['type'] == 'http'
+ response = JSONResponse({'hello': 'world'})
+ await response(scope, receive, send)
```
### UJSONResponse
from starlette.responses import UJSONResponse
-class App:
- def __init__(self, scope):
- assert scope['type'] == 'http'
- self.scope = scope
-
- async def __call__(self, receive, send):
- response = UJSONResponse({'hello': 'world'})
- await response(self.scope, receive, send)
+async def app(scope, receive, send):
+ assert scope['type'] == 'http'
+ response = UJSONResponse({'hello': 'world'})
+ await response(scope, receive, send)
```
### RedirectResponse
from starlette.responses import PlainTextResponse, RedirectResponse
-class App:
- def __init__(self, scope):
- assert scope['type'] == 'http'
- self.scope = scope
-
- async def __call__(self, receive, send):
- if self.scope['path'] != '/':
- response = RedirectResponse(url='/')
- else:
- response = PlainTextResponse('Hello, world!')
- await response(self.scope, receive, send)
+async def app(scope, receive, send):
+ assert scope['type'] == 'http'
+ if scope['path'] != '/':
+ response = RedirectResponse(url='/')
+ else:
+ response = PlainTextResponse('Hello, world!')
+ await response(scope, receive, send)
```
### StreamingResponse
yield('</ul></body></html>')
-class App:
- def __init__(self, scope):
- assert scope['type'] == 'http'
- self.scope = scope
-
- async def __call__(self, receive, send):
- generator = slow_numbers(1, 10)
- response = StreamingResponse(generator, media_type='text/html')
- await response(self.scope, receive, send)
+async def app(scope, receive, send):
+ assert scope['type'] == 'http'
+ generator = slow_numbers(1, 10)
+ response = StreamingResponse(generator, media_type='text/html')
+ await response(scope, receive, send)
```
Have in mind that <a href="https://docs.python.org/3/glossary.html#term-file-like-object" target="_blank">file-like</a> objects (like those created by `open()`) are normal iterators. So, you can return them directly in a `StreamingResponse`.
from starlette.responses import FileResponse
-class App:
- def __init__(self, scope):
- assert scope['type'] == 'http'
- self.scope = scope
-
- async def __call__(self, receive, send):
- response = FileResponse('statics/favicon.ico')
- await response(self.scope, receive, send)
+async def app(scope, receive, send):
+ assert scope['type'] == 'http'
+ response = FileResponse('statics/favicon.ico')
+ await response(scope, receive, send)
```
from starlette.testclient import TestClient
-class App:
- def __init__(self, scope):
- assert scope['type'] == 'http'
- self.scope = scope
-
- async def __call__(self, receive, send):
- response = HTMLResponse('<html><body>Hello, world!</body></html>')
- await response(self.scope, receive, send)
+async def app(scope, receive, send):
+ assert scope['type'] == 'http'
+ response = HTMLResponse('<html><body>Hello, world!</body></html>')
+ await response(scope, receive, send)
def test_app():
- client = TestClient(App)
+ client = TestClient(app)
response = client.get('/')
assert response.status_code == 200
```
from starlette.websockets import WebSocket
-class App:
- def __init__(self, scope):
- assert scope['type'] == 'websocket'
- self.scope = scope
-
- async def __call__(self, receive, send):
- websocket = WebSocket(self.scope, receive=receive, send=send)
- await websocket.accept()
- await websocket.send_text('Hello, world!')
- await websocket.close()
+async def app(scope, receive, send):
+ assert scope['type'] == 'websocket'
+ websocket = WebSocket(scope, receive=receive, send=send)
+ await websocket.accept()
+ await websocket.send_text('Hello, world!')
+ await websocket.close()
def test_app():
- client = TestClient(App)
+ client = TestClient(app)
with client.websocket_connect('/') as websocket:
data = websocket.receive_text()
assert data == 'Hello, world!'