Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name changes #55

Merged
merged 6 commits into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $ pip3 install starlette
## Example

```python
from starlette.response import Response
from starlette.responses import Response


class App:
Expand Down
18 changes: 9 additions & 9 deletions docs/applications.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Starlette also includes an `App` class that nicely ties together all of
Starlette also includes an app class `Starlette` that nicely ties together all of
its other functionality.

```python
from starlette.app import App
from starlette.response import PlainTextResponse
from starlette.app import Starlette
from starlette.responses import PlainTextResponse
from starlette.staticfiles import StaticFiles


app = App()
app = Starlette()
app.debug = True
app.mount("/static", StaticFiles(directory="static"))

Expand All @@ -23,15 +23,15 @@ def user(request, username):


@app.websocket_route('/ws')
async def websocket_endpoint(session):
await session.accept()
await session.send_text('Hello, websocket!')
await session.close()
async def websocket_endpoint(websocket):
await websocket.accept()
await websocket.send_text('Hello, websocket!')
await websocket.close()
```

### Instantiating the application

* `App(debug=False)` - Create a new Starlette application.
* `Starlette(debug=False)` - Create a new Starlette application.

### Adding routes to the application

Expand Down
24 changes: 12 additions & 12 deletions docs/views.md → docs/endpoints.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@

Starlette includes a `View` class that provides a class-based view pattern which
Starlette includes an `HTTPEndpoint` class that provides a class-based view pattern which
handles HTTP method dispatching.

The `View` class can be used as an other ASGI application:
The `HTTPEndpoint` class can be used as an ASGI application:

```python
from starlette.response import PlainTextResponse
from starlette.views import View
from starlette.responses import PlainTextResponse
from starlette.endpoints import HTTPEndpoint


class App(View):
class App(HTTPEndpoint):
async def get(self, request):
return PlainTextResponse(f"Hello, world!")
```

If you're using a Starlette application instance to handle routing, you can
dispatch to a View class by using the `@app.route()` decorator, or the
dispatch to an `HTTPEndpoint` class by using the `@app.route()` decorator, or the
`app.add_route()` function. Make sure to dispatch to the class itself, rather
than to an instance of the class:

```python
from starlette.app import App
from starlette.app import Starlette
from starlette.response import PlainTextResponse
from starlette.views import View
from starlette.endpoints import HTTPEndpoint


app = App()
app = Starlette()


@app.route("/")
class Homepage(View):
class Homepage(HTTPEndpoint):
async def get(self, request):
return PlainTextResponse(f"Hello, world!")


@app.route("/{username}")
class User(View):
class User(HTTPEndpoint):
async def get(self, request, username):
return PlainTextResponse(f"Hello, {username}")
```

Class-based views will respond with "405 Method not allowed" responses for any
HTTP endpoint classes will respond with "405 Method not allowed" responses for any
request methods which do not map to a corresponding handler.
2 changes: 1 addition & 1 deletion docs/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ instead:

```python
from starlette.exceptions import ExceptionMiddleware, HTTPException
from starlette.response import JSONResponse
from starlette.responses import JSONResponse


class App:
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ $ pip3 install starlette
## Example

```python
from starlette.response import Response
from starlette.responses import Response


class App:
Expand Down
6 changes: 3 additions & 3 deletions docs/requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ the incoming request, rather than accessing the ASGI scope and receive channel d
Signature: `Request(scope, receive=None)`

```python
from starlette.request import Request
from starlette.requests import Request
from starlette.response import Response


Expand Down Expand Up @@ -66,8 +66,8 @@ 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
from starlette.request import Request
from starlette.response import Response
from starlette.requests import Request
from starlette.responses import Response


class App:
Expand Down
14 changes: 7 additions & 7 deletions docs/responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Once you've instantiated a response, you can send it by calling it as an
ASGI application instance.

```python
from starlette.response import Response
from starlette.responses import Response


class App:
Expand All @@ -36,7 +36,7 @@ class App:
Takes some text or bytes and returns an HTML response.

```python
from starlette.response import HTMLResponse
from starlette.responses import HTMLResponse


class App:
Expand All @@ -53,7 +53,7 @@ class App:
Takes some text or bytes and returns an plain text response.

```python
from starlette.response import PlainTextResponse
from starlette.responses import PlainTextResponse


class App:
Expand All @@ -70,7 +70,7 @@ class App:
Takes some data and returns an `application/json` encoded response.

```python
from starlette.response import JSONResponse
from starlette.responses import JSONResponse


class App:
Expand All @@ -87,7 +87,7 @@ class App:
Returns an HTTP redirect. Uses a 302 status code by default.

```python
from starlette.response import PlainTextResponse, RedirectResponse
from starlette.responses import PlainTextResponse, RedirectResponse


class App:
Expand All @@ -107,7 +107,7 @@ class App:
Takes an async generator and streams the response body.

```python
from starlette.response import StreamingResponse
from starlette.responses import StreamingResponse
import asyncio


Expand Down Expand Up @@ -143,7 +143,7 @@ Takes a different set of arguments to instantiate than the other response types:
File responses will include appropriate `Content-Length`, `Last-Modified` and `ETag` headers.

```python
from starlette.response import FileResponse
from starlette.responses import FileResponse


class App:
Expand Down
12 changes: 6 additions & 6 deletions docs/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ You can also route based on the incoming protocol, using the `ProtocolRouter`
class.

```python
from starlette.response import Response
from starlette.responses import Response
from starlette.routing import ProtocolRouter
from starlette.websockets import WebSocketSession
from starlette.websockets import WebSocket


def http_endpoint(scope):
Expand All @@ -62,10 +62,10 @@ def http_endpoint(scope):

def websocket_endpoint(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
await session.accept()
await session.send_json({"hello": "world"})
await session.close()
websocket = WebSocket(scope, receive, send)
await websocket.accept()
await websocket.send_json({"hello": "world"})
await websocket.close()
return asgi


Expand Down
16 changes: 8 additions & 8 deletions docs/test_client.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ The test client allows you to make requests against your ASGI application,
using the `requests` library.

```python
from starlette.response import HTMLResponse
from starlette.responses import HTMLResponse
from starlette.testclient import TestClient


Expand Down Expand Up @@ -44,24 +44,24 @@ websocket testing.

```python
from starlette.testclient import TestClient
from starlette.websockets import WebSocketSession
from starlette.websockets import WebSocket


class App:
def __init__(self, scope):
self.scope = scope

async def __call__(self, receive, send):
session = WebSocketSession(self.scope, receive=receive, send=send)
await session.accept()
await session.send_text('Hello, world!')
await session.close()
websocket = WebSocket(self.scope, receive=receive, send=send)
await websocket.accept()
await websocket.send_text('Hello, world!')
await websocket.close()


def test_app():
client = TestClient(App)
with client.websocket_connect('/') as session:
data = session.receive_text()
with client.websocket_connect('/') as websocket:
data = websocket.receive_text()
assert data == 'Hello, world!'
```

Expand Down
55 changes: 27 additions & 28 deletions docs/websockets.md
Original file line number Diff line number Diff line change
@@ -1,81 +1,80 @@

Starlette includes a `WebSocketSessions` class that fulfils a similar role
to the HTTP request, but that allows sending and receiving data on a websocket
session.
Starlette includes a `WebSocket` class that fulfils a similar role
to the HTTP request, but that allows sending and receiving data on a websocket.

### WebSocketSession
### WebSocket

Signature: `WebSocketSession(scope, receive=None, send=None)`
Signature: `WebSocket(scope, receive=None, send=None)`

```python
from starlette.websockets import WebSocketSession
from starlette.websockets import WebSocket


class App:
def __init__(self, scope):
self.scope = scope

async def __call__(self, receive, send):
session = WebSocketSession(self.scope, receive=receive, send=send)
await session.accept()
await session.send_text('Hello, world!')
await session.close()
websocket = WebSocket(self.scope, receive=receive, send=send)
await websocket.accept()
await websocket.send_text('Hello, world!')
await websocket.close()
```

Sessions present a mapping interface, so you can use them in the same
WebSockets present a mapping interface, so you can use them in the same
way as a `scope`.

For instance: `session['path']` will return the ASGI path.
For instance: `websocket['path']` will return the ASGI path.

#### URL

The session URL is accessed as `session.url`.
The websocket URL is accessed as `websocket.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: `session.url.path`, `session.url.port`, `session.url.scheme`.
For example: `websocket.url.path`, `websocket.url.port`, `websocket.url.scheme`.

#### Headers

Headers are exposed as an immutable, case-insensitive, multi-dict.

For example: `session.headers['sec-websocket-version']`
For example: `websocket.headers['sec-websocket-version']`

#### Query Parameters

Headers are exposed as an immutable multi-dict.

For example: `session.query_params['abc']`
For example: `websocket.query_params['abc']`

### Accepting the connection

* `await session.accept(subprotocol=None)`
* `await websocket.accept(subprotocol=None)`

### Sending data

* `await session.send_text(data)`
* `await session.send_bytes(data)`
* `await session.send_json(data)`
* `await websocket.send_text(data)`
* `await websocket.send_bytes(data)`
* `await websocket.send_json(data)`

### Receiving data

* `await session.receive_text()`
* `await session.receive_bytes()`
* `await session.receive_json()`
* `await websocket.receive_text()`
* `await websocket.receive_bytes()`
* `await websocket.receive_json()`

May raise `starlette.websockets.Disconnect()`.

### Closing the connection

* `await session.close(code=1000)`
* `await websocket.close(code=1000)`

### Sending and receiving messages

If you need to send or receive raw ASGI messages then you should use
`session.send()` and `session.receive()` rather than using the raw `send` and
`receive` callables. This will ensure that the session's state is kept
`websocket.send()` and `websocket.receive()` rather than using the raw `send` and
`receive` callables. This will ensure that the websocket's state is kept
correctly updated.

* `await session.send(message)`
* `await session.receive()`
* `await websocket.send(message)`
* `await websocket.receive()`
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ nav:
- Applications: 'applications.md'
- Test Client: 'test_client.md'
- Debugging: 'debugging.md'
- Views: 'views.md'
- Endpoints: 'endpoints.md'

markdown_extensions:
- markdown.extensions.codehilite:
Expand Down
Loading