Skip to content

Commit

Permalink
Merge fe71d4c into f20db6f
Browse files Browse the repository at this point in the history
  • Loading branch information
Relrin committed Nov 25, 2016
2 parents f20db6f + fe71d4c commit 93e843b
Show file tree
Hide file tree
Showing 66 changed files with 725 additions and 508 deletions.
2 changes: 1 addition & 1 deletion aiorest_ws/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
__version__ = '1.1.0'
__author__ = 'Valeryi Savich'
__license__ = 'BSD'
__copyright__ = 'Copyright (c) 2015 by Valeryi Savich'
__copyright__ = 'Copyright (c) 2016 by Valeryi Savich'

VERSION = __version__
43 changes: 28 additions & 15 deletions aiorest_ws/abstract.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""
Abstract classes for future implementation.
Abstract classes for future implementation.
"""
from abc import ABCMeta, abstractmethod

Expand All @@ -11,13 +11,14 @@


class AbstractEndpoint(metaclass=ABCMeta):
"""Base class for endpoints."""

"""
Base class for endpoints.
"""
path = None # URL, used for get access to API
handler = None # class/function for processing request
methods = [] # list of supported methods (GET, POST, etc.)
name = None # short name for route
_pattern = None # pattern, which using for checking path on compatible
handler = None # Class/function for processing request
methods = [] # List of supported methods (GET, POST, etc.)
name = None # Short name for route
_pattern = None # Pattern, which using for checking path on compatible

def __init__(self, path, handler, methods, name):
self.path = path
Expand All @@ -30,15 +31,18 @@ def __init__(self, path, handler, methods, name):

@abstractmethod
def match(self, path):
"""Checking path on compatible.
"""
Checking path on compatible.
:param path: URL, which used for get access to API.
"""
pass


class AbstractRouter(metaclass=ABCMeta):
"""Base class for routers."""
"""
Base class for routers.
"""
_middlewares = []

def __init__(self, *args, **kwargs):
Expand All @@ -47,23 +51,29 @@ def __init__(self, *args, **kwargs):

@property
def middlewares(self):
"""Get list of used middlewares."""
"""
Get list of used middlewares.
"""
return self._middlewares

@abstractmethod
def process_request(self, request):
"""Handling received request from user.
"""
Handling received request from user.
:param request: request from user.
"""
pass


class AbstractMiddleware(metaclass=ABCMeta):
"""Base class for middlewares."""
"""
Base class for middlewares.
"""
@abstractmethod
def process_request(self, request, handler):
"""Processing request before calling handler.
"""
Processing request before calling handler.
:param request: instance of Request class.
:param handler: view, invoked later for the request.
Expand All @@ -72,10 +82,13 @@ def process_request(self, request, handler):


class AbstractPermission(metaclass=ABCMeta):
"""Base class for permissions."""
"""
Base class for permissions.
"""
@staticmethod
def check(request, handler):
"""Check permission method.
"""
Check permission method.
:param request: instance of Request class.
:param handler: view, invoked later for the request.
Expand Down
87 changes: 60 additions & 27 deletions aiorest_ws/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""
This module implements the central application object.
This module implements the central application object.
"""
import asyncio
import ssl
Expand All @@ -16,7 +16,9 @@


class Application(object):
"""Main application of aiorest-ws framework."""
"""
Main application of aiorest-ws framework.
"""

_factory = RequestHandlerFactory
_protocol = RequestHandlerProtocol
Expand All @@ -25,7 +27,9 @@ class Application(object):
_middlewares = []

def __init__(self, *args, **options):
"""Initialization of Application instance."""
"""
Initialization of Application instance.
"""
super(Application, self).__init__()
self.factory = options.get('factory')
self.protocol = options.get('protocol')
Expand All @@ -38,12 +42,15 @@ def __init__(self, *args, **options):

@property
def factory(self):
"""Get factory class."""
"""
Get factory class.
"""
return self._factory

@factory.setter
def factory(self, factory):
"""Set factory class.
"""
Set factory class.
:param factory: subclass of RequestHandlerFactory.
"""
Expand All @@ -53,17 +60,22 @@ def factory(self, factory):

@property
def middlewares(self):
"""Get list of used middlewares."""
"""
Get list of used middlewares.
"""
return self._middlewares

@property
def protocol(self):
"""Get protocol class."""
"""
Get protocol class.
"""
return self._protocol

@protocol.setter
def protocol(self, protocol):
"""Set protocol class.
"""
Set protocol class.
:param factory: subclass of RequestHandlerProtocol.
"""
Expand All @@ -73,46 +85,54 @@ def protocol(self, protocol):

@property
def certificate(self):
"""Get filepath to certificate."""
"""
Get filepath to certificate.
"""
return self._certificate

@certificate.setter
def certificate(self, certificate):
"""Setter for certificate.
"""
Setter for certificate.
:param certificate: path to certificate file.
"""
self._certificate = certificate

@property
def key(self):
"""Get private key for certificate."""
"""
Get private key for certificate.
"""
return self._key

@key.setter
def key(self, key):
"""Set private key for certificate.
"""
Set private key for certificate.
:param key: private key for certificate.
"""
self._key = key

@property
def url(self):
"""Get url to WebSocket REST API."""
if self.isSecure:
url = "wss://{0}:{1}/{2}"
else:
url = "ws://{0}:{1}/{2}"
return url
"""
Get url to WebSocket REST API.
"""
return "wss://{0}:{1}/{2}" if self.isSecure else "ws://{0}:{1}/{2}"

@property
def isSecure(self):
"""Property, which help us to understand, use SSL or not."""
"""
Property, which help us to understand, use SSL or not.
"""
return self.certificate and self.key

def _get_ssl_context(self):
"""Generating SSL context for asyncio loop."""
"""
Generating SSL context for asyncio loop.
"""
if self.isSecure:
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ssl_context.load_cert_chain(self.certificate, self.key)
Expand All @@ -121,30 +141,38 @@ def _get_ssl_context(self):
return ssl_context

def _init_factory(self, url, **options):
"""Create a factory instance."""
"""
Create a factory instance.
"""
debug = options.get('debug', False)

factory = self.factory(url, debug=debug)
factory.protocol = self.protocol
return factory

def _enable_compressing(self, factory, **options):
"""Set compression message for factory, if defined."""
"""
Set compression message for factory, if defined.
"""
compress = options.get('compress', False)

if compress:
factory.setProtocolOptions(perMessageCompressionAccept=accept)

def _set_factory_router(self, factory, **options):
"""Set users router for factory, if defined."""
"""
Set users router for factory, if defined.
"""
router = options.get('router', None)
assert router, "Argument `router` must be defined for Application."

factory.router = router
factory.router._middlewares = self.middlewares

def _init_urlconf(self, factory, url, **options):
"""Initialize urlconf thread variable."""
"""
Initialize urlconf thread variable.
"""
data = {
'path': url,
'urls': factory.router._urls,
Expand All @@ -153,19 +181,24 @@ def _init_urlconf(self, factory, url, **options):
set_urlconf(data)

def generate_factory(self, url, **options):
"""Create and initialize factory instance."""
"""
Create and initialize factory instance.
"""
factory = self._init_factory(url, **options)
self._enable_compressing(factory, **options)
self._set_factory_router(factory, **options)
self._init_urlconf(factory, url, **options)
return factory

def generate_url(self, host, port, path=''):
"""Generate URL to application."""
"""
Generate URL to application.
"""
return self.url.format(host, port, path)

def run(self, **options):
"""Create and start web server with some IP and PORT.
"""
Create and start web server with some IP and PORT.
:param options: parameters, which can be used for configuration
of the Application.
Expand Down
2 changes: 1 addition & 1 deletion aiorest_ws/auth/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""
Exception classes for authentication.
Exception classes for authentication.
"""
from aiorest_ws.exceptions import BaseAPIException

Expand Down
9 changes: 6 additions & 3 deletions aiorest_ws/auth/permissions.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# -*- coding: utf-8 -*-
"""
Permission classes for authentication.
Permission classes for authentication.
"""
from aiorest_ws.abstract import AbstractPermission

__all__ = ('IsAuthenticated', )


class IsAuthenticated(AbstractPermission):
"""Permissions used for checking authenticated users."""
"""
Permissions used for checking authenticated users.
"""
@staticmethod
def check(request, handler):
"""Check permission method.
"""
Check permission method.
:param request: instance of Request class.
:param handler: view, invoked later for the request.
Expand Down
Loading

0 comments on commit 93e843b

Please sign in to comment.