Skip to content

Commit

Permalink
Rename WebSocketSession -> WebSocket, rename session -> websocket in …
Browse files Browse the repository at this point in the history
…docs
  • Loading branch information
erm committed Sep 4, 2018
1 parent 0578fd5 commit 8952bb3
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 67 deletions.
10 changes: 5 additions & 5 deletions docs/applications.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions docs/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class.
```python
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 @@ -22,7 +22,7 @@ def test_app():
assert response.status_code == 200
```

The test client exposes the same interface as any other `requests` session.
The test client exposes the same interface as any other `requests` websocket.
In particular, note that the calls to make a request are just standard
function calls, not awaitables.

Expand All @@ -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()`
4 changes: 2 additions & 2 deletions starlette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from starlette.requests import Request
from starlette.routing import Path, PathPrefix, Router
from starlette.types import ASGIApp, ASGIInstance, Receive, Scope, Send
from starlette.websockets import WebSocketSession
from starlette.websockets import WebSocket
import asyncio
import inspect

Expand Down Expand Up @@ -36,7 +36,7 @@ def websocket_session(func):

def app(scope: Scope) -> ASGIInstance:
async def awaitable(receive: Receive, send: Send) -> None:
session = WebSocketSession(scope, receive=receive, send=send)
session = WebSocket(scope, receive=receive, send=send)
kwargs = scope.get("kwargs", {})
await func(session, **kwargs)

Expand Down
2 changes: 1 addition & 1 deletion starlette/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, code=1000):
self.code = code


class WebSocketSession(Mapping):
class WebSocket(Mapping):
def __init__(self, scope, receive=None, send=None):
assert scope["type"] == "websocket"
self._scope = scope
Expand Down
4 changes: 2 additions & 2 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from starlette import Response, TestClient
from starlette.exceptions import ExceptionMiddleware
from starlette.routing import Path, PathPrefix, Router, ProtocolRouter
from starlette.websockets import WebSocketSession, WebSocketDisconnect
from starlette.websockets import WebSocket, WebSocketDisconnect
import pytest


Expand Down Expand Up @@ -71,7 +71,7 @@ def http_endpoint(scope):

def websocket_endpoint(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
await session.accept()
await session.send_json({"hello": "world"})
await session.close()
Expand Down
32 changes: 16 additions & 16 deletions tests/test_websockets.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pytest
from starlette.testclient import TestClient
from starlette.websockets import WebSocketSession, WebSocketDisconnect
from starlette.websockets import WebSocket, WebSocketDisconnect


def test_session_url():
def app(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
await session.accept()
await session.send_json({"url": session.url})
await session.close()
Expand All @@ -22,7 +22,7 @@ async def asgi(receive, send):
def test_session_query_params():
def app(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
query_params = dict(session.query_params)
await session.accept()
await session.send_json({"params": query_params})
Expand All @@ -39,7 +39,7 @@ async def asgi(receive, send):
def test_session_headers():
def app(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
headers = dict(session.headers)
await session.accept()
await session.send_json({"headers": headers})
Expand All @@ -65,7 +65,7 @@ async def asgi(receive, send):
def test_session_port():
def app(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
await session.accept()
await session.send_json({"port": session.url.port})
await session.close()
Expand All @@ -81,7 +81,7 @@ async def asgi(receive, send):
def test_session_send_and_receive_text():
def app(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
await session.accept()
data = await session.receive_text()
await session.send_text("Message was: " + data)
Expand All @@ -99,7 +99,7 @@ async def asgi(receive, send):
def test_session_send_and_receive_bytes():
def app(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
await session.accept()
data = await session.receive_bytes()
await session.send_bytes(b"Message was: " + data)
Expand All @@ -117,7 +117,7 @@ async def asgi(receive, send):
def test_session_send_and_receive_json():
def app(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
await session.accept()
data = await session.receive_json()
await session.send_json({"message": data})
Expand All @@ -138,7 +138,7 @@ def test_client_close():
def app(scope):
async def asgi(receive, send):
nonlocal close_code
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
await session.accept()
try:
data = await session.receive_text()
Expand All @@ -156,7 +156,7 @@ async def asgi(receive, send):
def test_application_close():
def app(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
await session.accept()
await session.close(1001)

Expand All @@ -172,7 +172,7 @@ async def asgi(receive, send):
def test_rejected_connection():
def app(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
await session.close(1001)

return asgi
Expand All @@ -186,7 +186,7 @@ async def asgi(receive, send):
def test_subprotocol():
def app(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
assert session["subprotocols"] == ["soap", "wamp"]
await session.accept(subprotocol="wamp")
await session.close()
Expand All @@ -213,7 +213,7 @@ async def asgi(receive, send):
def test_duplicate_close():
def app(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
await session.accept()
await session.close()
await session.close()
Expand All @@ -229,7 +229,7 @@ async def asgi(receive, send):
def test_duplicate_disconnect():
def app(scope):
async def asgi(receive, send):
session = WebSocketSession(scope, receive, send)
session = WebSocket(scope, receive, send)
await session.accept()
message = await session.receive()
assert message["type"] == "websocket.disconnect"
Expand All @@ -245,10 +245,10 @@ async def asgi(receive, send):

def test_session_scope_interface():
"""
A WebSocketSession can be instantiated with a scope, and presents a `Mapping`
A WebSocket can be instantiated with a scope, and presents a `Mapping`
interface.
"""
session = WebSocketSession({"type": "websocket", "path": "/abc/", "headers": []})
session = WebSocket({"type": "websocket", "path": "/abc/", "headers": []})
assert session["type"] == "websocket"
assert dict(session) == {"type": "websocket", "path": "/abc/", "headers": []}
assert len(session) == 3

0 comments on commit 8952bb3

Please sign in to comment.