Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/relrin/aiorest-ws
REST framework with WebSockets support [inactive]
https://github.com/relrin/aiorest-ws
Last synced: 30 days ago
JSON representation
REST framework with WebSockets support [inactive]
- Host: GitHub
- URL: https://github.com/relrin/aiorest-ws
- Owner: Relrin
- License: other
- Created: 2015-04-25T15:37:37.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-16T06:52:10.000Z (about 7 years ago)
- Last Synced: 2024-09-18T04:45:23.064Z (about 2 months ago)
- Language: Python
- Homepage:
- Size: 505 KB
- Stars: 36
- Watchers: 8
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# ![aiorest-ws logo](https://raw.githubusercontent.com/Relrin/aiorest-ws/master/docs/source/static/logo.png) [![Build Status](https://travis-ci.org/Relrin/aiorest-ws.svg)](https://travis-ci.org/Relrin/aiorest-ws) [![Coverage Status](https://coveralls.io/repos/Relrin/aiorest-ws/badge.svg?branch=master&service=github)](https://coveralls.io/github/Relrin/aiorest-ws?branch=master)
REST framework with WebSockets supportThis library represents as a flexible toolkit for building Web APIs, which based on the Autobahn.ws and asyncio packages. Use the opportunities of WebSockets for communication between clients and servers, build APIs like in popular frameworks (Django REST, Flask, etc.) with enough simplicity, flexibility and transparency. Develop with a pleasure!
Features
-----
- Routing
- Views (function and method-based)
- Authentication (using JSON Web Token)
- Customizing behaviour of your application through settings file
- Compressing messages for minimize of transmitted traffic (if your browser support)
- Model serializing for Django and SQLAlchemy ORM
- SSL supportRequirements
-----
- Python >= 3.4.2
- Autobahn.ws == 0.16.0Optional:
- SQLAlchemy ORM >= 1.0
- Django >= 1.9License
-----
The aiorest-ws published under BSD license. For more details read [LICENSE](https://github.com/Relrin/aiorest-ws/blob/master/LICENSE) file.Roadmap (by priority) to releases:
------
v1.2:
- Wrap ORM calls / serializers into coroutines, so that it won't slow down an event loop
- Notification support
v1.3:
- Web browsable API (similar on swagger?)
v1.4:
- Classes and functions for testing APIs
- Clients for Python, JavaScriptDocumentation
-----
The latest documentation for the project is available [there](http://aiorest-ws.readthedocs.org/).Contributing
-----
Read [CONTRIBUTING](https://github.com/Relrin/aiorest-ws/blob/master/CONTRIBUTING.md) file for more information.Getting started
---------------
#### Client (JavaScript)
```javascript
var ws = null;
var isopen = false;window.onload = function() {
ws = new WebSocket("ws://127.0.0.1:8080");
ws.onopen = function() {
console.log("Connected!");
isopen = true;
};ws.onmessage = function(e) {
console.log("Result: " + e.data);
};ws.onclose = function(e) {
console.log("Connection closed.");
ws = null;
isopen = false;
}
};function sendOnStaticURL() {
if (isopen) {
ws.send(JSON.stringify({'method': 'GET', 'url': '/hello'}));
} else {
console.log("Connection not opened.")
}
}
```#### Client (Python)
```python
import asyncio
import jsonfrom autobahn.asyncio.websocket import WebSocketClientProtocol, \
WebSocketClientFactoryclass HelloClientProtocol(WebSocketClientProtocol):
def onOpen(self):
request = {'method': 'GET', 'url': '/hello'}
self.sendMessage(json.dumps(request).encode('utf8'))def onMessage(self, payload, isBinary):
message = payload.decode('utf8')
print(message)if __name__ == '__main__':
factory = WebSocketClientFactory("ws://localhost:8080")
factory.protocol = HelloClientProtocolloop = asyncio.get_event_loop()
coro = loop.create_connection(factory, '127.0.0.1', 8080)
loop.run_until_complete(coro)
loop.run_forever()
loop.close()
```#### Server
```python
from aiorest_ws.app import Application
from aiorest_ws.routers import SimpleRouter
from aiorest_ws.views import MethodBasedViewclass HelloWorld(MethodBasedView):
def get(self, request, *args, **kwargs):
return "Hello, world!"router = SimpleRouter()
router.register('/hello', HelloWorld, 'GET')if __name__ == '__main__':
app = Application()
app.run(host='127.0.0.1', port=8080, router=router)
```Also you can look more examples [there](https://github.com/Relrin/aiorest-ws/tree/master/examples).
Running server with SSL
-----Aiorest-ws framework support running server with SSL. Just append ```certificate``` and ```key``` options to the ```Application``` constructor:
```python
# WebSockets will be available on wss://127.0.0.1:8080/api
app = Application(certificate='path/to/my.crt', key='path/to/my.key')
app.run(host='127.0.0.1', port=8080, path='api', router=router)
```If you don't have any certificates and keys, but want to run the server with SSL, then generate self-signed certificate via OpenSSL:
```bash
openssl genrsa -out server.key 2048
openssl req -new -key server.key -out server.csr
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
openssl x509 -in server.crt -out server.pem
```