starlette/README.md

318 lines
7.9 KiB
Markdown
Raw Normal View History

2018-06-26 09:09:35 +00:00
<h1 align="center">
Starlette
</h1>
2018-06-25 13:19:28 +00:00
<p align="center">
2018-06-25 15:05:31 +00:00
<em>✨ The little ASGI library that shines. ✨</em>
2018-06-25 13:19:28 +00:00
</p>
<p align="center">
<a href="https://travis-ci.org/encode/starlette">
<img src="https://travis-ci.org/encode/starlette.svg?branch=master" alt="Build Status">
</a>
<a href="https://codecov.io/gh/encode/starlette">
<img src="https://codecov.io/gh/encode/starlette/branch/master/graph/badge.svg" alt="Coverage">
</a>
2018-06-25 13:19:28 +00:00
<a href="https://pypi.org/project/starlette/">
<img src="https://badge.fury.io/py/starlette.svg" alt="Package version">
</a>
</p>
2018-06-25 13:15:32 +00:00
2018-06-25 13:35:26 +00:00
---
2018-06-25 14:35:22 +00:00
Starlette is a small library for working with [ASGI](https://asgi.readthedocs.io/en/latest/).
2018-06-25 13:35:26 +00:00
2018-06-26 09:09:35 +00:00
It gives you `Request` and `Response` classes, routing, a test client, and a
2018-06-25 13:35:26 +00:00
decorator for writing super-minimal applications.
2018-06-25 13:58:51 +00:00
**Requirements:**
Python 3.6+
2018-06-25 13:35:26 +00:00
**Installation:**
```shell
pip3 install starlette
```
2018-06-25 13:24:50 +00:00
2018-06-25 13:35:26 +00:00
**Example:**
2018-06-25 13:24:50 +00:00
2018-06-25 13:15:32 +00:00
```python
from starlette import Response
class App:
def __init__(self, scope):
self.scope = scope
async def __call__(self, receive, send):
response = Response('Hello, world!', media_type='text/plain')
await response(receive, send)
```
2018-06-25 13:40:56 +00:00
You can run the application with any ASGI server, including [uvicorn](http://www.uvicorn.org/), [daphne](https://github.com/django/daphne/), or [hypercorn](https://pgjones.gitlab.io/hypercorn/),
2018-06-25 13:21:24 +00:00
<p align="center">&mdash; ⭐️ &mdash;</p>
2018-06-25 13:15:32 +00:00
## Responses
Starlette includes a few response classes that handle sending back the
appropriate ASGI messages on the `send` channel.
### Response
Signature: `Response(content=b'', status_code=200, headers=None, media_type=None)`
* `content` - A string or bytestring.
* `status_code` - An integer HTTP status code.
* `headers` - A dictionary of strings or list of pairs of strings.
* `media_type` - A string giving the content type.
2018-06-25 13:15:32 +00:00
Starlette will automatically include a content-length header. It will also
set the content-type header, including a charset for text types.
2018-06-25 14:35:22 +00:00
Once you've instantiated a response, you can send it by calling it as an
ASGI application instance.
2018-06-25 13:15:32 +00:00
```python
class App:
def __init__(self, scope):
self.scope = scope
async def __call__(self, receive, send):
response = Response('Hello, world!', media_type='text/plain')
await response(receive, send)
```
### HTMLResponse
Takes some text or bytes and returns an HTML response.
```python
from starlette import HTMLResponse
class App:
def __init__(self, scope):
self.scope = scope
async def __call__(self, receive, send):
response = HTMLResponse('<html><body><h1>Hello, world!</h1></body></html')
await response(receive, send)
```
### JSONResponse
Takes some data and returns an `application/json` encoded response.
```python
from starlette import JSONResponse
class App:
def __init__(self, scope):
self.scope = scope
async def __call__(self, receive, send):
response = JSONResponse({'hello': 'world'})
await response(receive, send)
```
### StreamingResponse
Takes an async generator and streams the response body.
```python
from starlette import Request, StreamingResponse
import asyncio
async def slow_numbers(minimum, maximum):
yield('<html><body><ul>')
for number in range(minimum, maximum + 1):
yield '<li>%d</li>' % number
await asyncio.sleep(0.5)
yield('</ul></body></html>')
class App:
def __init__(self, scope):
self.scope = scope
async def __call__(self, receive, send):
generator = slow_numbers(1, 10)
response = StreamingResponse(generator, media_type='text/html')
await response(receive, send)
```
2018-06-25 13:35:26 +00:00
---
2018-06-25 13:24:50 +00:00
2018-06-25 13:15:32 +00:00
## Requests
Starlette includes a `Request` class that gives you a nicer interface onto
the incoming request, rather than accessing the ASGI scope and receive channel directly.
### Request
2018-06-26 09:51:51 +00:00
Signature: `Request(scope, receive=None)`
2018-06-25 13:15:32 +00:00
```python
class App:
def __init__(self, scope):
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(receive, send)
```
2018-06-26 09:51:51 +00:00
Requests present a mapping interface, so you can use them in the same
way as a `scope`.
For instance: `request['path']` will return the ASGI path.
2018-06-26 10:10:54 +00:00
If you don't need to access the request body you can instantiate a request
without providing an argument to `receive`.
#### Method
2018-06-25 13:15:32 +00:00
The request method is accessed as `request.method`.
2018-06-25 13:24:50 +00:00
#### URL
2018-06-25 13:15:32 +00:00
The request URL is accessed as `request.url`.
The property is actually a subclass of `str`, and also exposes all the
components that can be parsed out of the URL.
For example: `request.url.path`, `request.url.port`, `request.url.scheme`.
2018-06-25 13:24:50 +00:00
#### Headers
2018-06-25 13:15:32 +00:00
Headers are exposed as an immutable, case-insensitive, multi-dict.
For example: `request.headers['content-type']`
2018-06-25 13:24:50 +00:00
#### Query Parameters
2018-06-25 13:15:32 +00:00
Headers are exposed as an immutable multi-dict.
For example: `request.query_params['abc']`
2018-06-25 13:24:50 +00:00
#### Body
2018-06-25 13:15:32 +00:00
2018-06-25 15:02:59 +00:00
There are a few different interfaces for returning the body of the request:
2018-06-25 13:15:32 +00:00
The request body as bytes: `await request.body()`
The request body, parsed as JSON: `await request.json()`
You can also access the request body as a stream, using the `async for` syntax:
```python
class App:
def __init__(self, scope):
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(content, media_type='text/plain')
await response(receive, send)
```
If you access `.stream()` then the byte chunks are provided without storing
the entire body to memory. Any subsequent calls to `.body()` and `.json()` will
raise an error.
2018-06-25 13:35:26 +00:00
---
2018-06-25 13:24:50 +00:00
2018-06-26 09:09:35 +00:00
## Routing
Starlette includes a `Router` class which is an ASGI application that
dispatches to other ASGI applications.
```python
from starlette import Router, Path, PathPrefix
from myproject import Homepage, StaticFiles
app = Router([
Path('/', app=Homepage, methods=['GET']),
PathPrefix('/static', app=StaticFiles, methods=['GET'])
])
```
Paths can use URI templating style to capture path components.
```python
Path('/users/{username}', app=User, methods=['GET'])
```
Path components are made available in the scope, as `scope["kwargs"]`.
Because each target of the router is an ASGI instance itself, routers
allow for easy composition. For example:
```python
app = Router([
Path('/', app=Homepage, methods=['GET']),
PathPrefix('/users', app=Router([
Path('/', app=Users, methods=['GET', 'POST']),
Path('/{username}', app=User, methods=['GET']),
]))
])
```
The router will respond with "404 Not found" or "406 Method not allowed"
responses for requests which do not match.
---
2018-06-25 13:15:32 +00:00
## Test Client
The test client allows you to make requests against your ASGI application,
using the `requests` library.
```python
from starlette import HTMLResponse, TestClient
class App:
def __init__(self, scope):
self.scope = scope
async def __call__(self, receive, send):
response = HTMLResponse('<html><body>Hello, world!</body></html>')
await response(receive, send)
def test_app():
client = TestClient(App)
response = client.get('/')
assert response.status_code == 200
```
2018-06-25 13:35:26 +00:00
---
2018-06-25 13:24:50 +00:00
2018-06-25 13:15:32 +00:00
## Decorators
The `asgi_application` decorator turns an `async` function into an ASGI application.
The function must take a single `request` argument, and return a response.
```python
from starlette import asgi_application, HTMLResponse
@asgi_application
async def app(request):
return HTMLResponse('<html><body>Hello, world!</body></html>')
```
---
2018-06-26 09:09:35 +00:00
<p align="center"><i>Starlette is <a href="https://github.com/tomchristie/starlette/blob/master/LICENSE.md">BSD licensed</a> code.<br/>Designed & built in Brighton, England.</i><br/>&mdash; ⭐️ &mdash;</p>