Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #400 from matrix-org/daniel/versioning
Browse files Browse the repository at this point in the history
Merge pull request # 400 from matrix-org/daniel/versioning
  • Loading branch information
illicitonion committed Dec 1, 2015
2 parents af96c6f + 14d7acf commit 6e70979
Show file tree
Hide file tree
Showing 27 changed files with 133 additions and 117 deletions.
2 changes: 1 addition & 1 deletion synapse/federation/transport/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def register(self, server):
if code is None:
continue

server.register_path(method, pattern, self._wrap(code))
server.register_paths(method, (pattern,), self._wrap(code))


class FederationSendServlet(BaseFederationServlet):
Expand Down
13 changes: 7 additions & 6 deletions synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class HttpServer(object):
""" Interface for registering callbacks on a HTTP server
"""

def register_path(self, method, path_pattern, callback):
def register_paths(self, method, path_patterns, callback):
""" Register a callback that gets fired if we receive a http request
with the given method for a path that matches the given regex.
Expand All @@ -129,7 +129,7 @@ def register_path(self, method, path_pattern, callback):
Args:
method (str): The method to listen to.
path_pattern (str): The regex used to match requests.
path_patterns (list<SRE_Pattern>): The regex used to match requests.
callback (function): The function to fire if we receive a matched
request. The first argument will be the request object and
subsequent arguments will be any matched groups from the regex.
Expand Down Expand Up @@ -165,10 +165,11 @@ def __init__(self, hs, canonical_json=True):
self.version_string = hs.version_string
self.hs = hs

def register_path(self, method, path_pattern, callback):
self.path_regexs.setdefault(method, []).append(
self._PathEntry(path_pattern, callback)
)
def register_paths(self, method, path_patterns, callback):
for path_pattern in path_patterns:
self.path_regexs.setdefault(method, []).append(
self._PathEntry(path_pattern, callback)
)

def render(self, request):
""" This gets called by twisted every time someone sends us a request.
Expand Down
8 changes: 4 additions & 4 deletions synapse/http/servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import logging


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -102,12 +101,13 @@ class attribute containing a pre-compiled regular expression. The automatic

def register(self, http_server):
""" Register this servlet with the given HTTP server. """
if hasattr(self, "PATTERN"):
pattern = self.PATTERN
if hasattr(self, "PATTERNS"):
patterns = self.PATTERNS

for method in ("GET", "PUT", "POST", "OPTIONS", "DELETE"):
if hasattr(self, "on_%s" % (method,)):
method_handler = getattr(self, "on_%s" % (method,))
http_server.register_path(method, pattern, method_handler)
http_server.register_paths(method, patterns, method_handler)

else:
raise NotImplementedError("RestServlet must register something.")
4 changes: 2 additions & 2 deletions synapse/rest/client/v1/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
from synapse.api.errors import AuthError, SynapseError
from synapse.types import UserID

from base import ClientV1RestServlet, client_path_pattern
from base import ClientV1RestServlet, client_path_patterns

import logging

logger = logging.getLogger(__name__)


class WhoisRestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/admin/whois/(?P<user_id>[^/]*)")
PATTERNS = client_path_patterns("/admin/whois/(?P<user_id>[^/]*)", releases=())

@defer.inlineCallbacks
def on_GET(self, request, user_id):
Expand Down
10 changes: 8 additions & 2 deletions synapse/rest/client/v1/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
logger = logging.getLogger(__name__)


def client_path_pattern(path_regex):
def client_path_patterns(path_regex, releases=(0,)):
"""Creates a regex compiled client path with the correct client path
prefix.
Expand All @@ -37,7 +37,13 @@ def client_path_pattern(path_regex):
Returns:
SRE_Pattern
"""
return re.compile("^" + CLIENT_PREFIX + path_regex)
patterns = [re.compile("^" + CLIENT_PREFIX + path_regex)]
unstable_prefix = CLIENT_PREFIX.replace("/api/v1", "/unstable")
patterns.append(re.compile("^" + unstable_prefix + path_regex))
for release in releases:
new_prefix = CLIENT_PREFIX.replace("/api/v1", "/r%d" % release)
patterns.append(re.compile("^" + new_prefix + path_regex))
return patterns


class ClientV1RestServlet(RestServlet):
Expand Down
4 changes: 2 additions & 2 deletions synapse/rest/client/v1/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from synapse.api.errors import AuthError, SynapseError, Codes
from synapse.types import RoomAlias
from .base import ClientV1RestServlet, client_path_pattern
from .base import ClientV1RestServlet, client_path_patterns

import simplejson as json
import logging
Expand All @@ -32,7 +32,7 @@ def register_servlets(hs, http_server):


class ClientDirectoryServer(ClientV1RestServlet):
PATTERN = client_path_pattern("/directory/room/(?P<room_alias>[^/]*)$")
PATTERNS = client_path_patterns("/directory/room/(?P<room_alias>[^/]*)$")

@defer.inlineCallbacks
def on_GET(self, request, room_alias):
Expand Down
6 changes: 3 additions & 3 deletions synapse/rest/client/v1/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from synapse.api.errors import SynapseError
from synapse.streams.config import PaginationConfig
from .base import ClientV1RestServlet, client_path_pattern
from .base import ClientV1RestServlet, client_path_patterns
from synapse.events.utils import serialize_event

import logging
Expand All @@ -28,7 +28,7 @@


class EventStreamRestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/events$")
PATTERNS = client_path_patterns("/events$")

DEFAULT_LONGPOLL_TIME_MS = 30000

Expand Down Expand Up @@ -72,7 +72,7 @@ def on_OPTIONS(self, request):

# TODO: Unit test gets, with and without auth, with different kinds of events.
class EventRestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/events/(?P<event_id>[^/]*)$")
PATTERNS = client_path_patterns("/events/(?P<event_id>[^/]*)$")

def __init__(self, hs):
super(EventRestServlet, self).__init__(hs)
Expand Down
4 changes: 2 additions & 2 deletions synapse/rest/client/v1/initial_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
from twisted.internet import defer

from synapse.streams.config import PaginationConfig
from base import ClientV1RestServlet, client_path_pattern
from base import ClientV1RestServlet, client_path_patterns


# TODO: Needs unit testing
class InitialSyncRestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/initialSync$")
PATTERNS = client_path_patterns("/initialSync$")

@defer.inlineCallbacks
def on_GET(self, request):
Expand Down
12 changes: 6 additions & 6 deletions synapse/rest/client/v1/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from synapse.api.errors import SynapseError, LoginError, Codes
from synapse.http.client import SimpleHttpClient
from synapse.types import UserID
from base import ClientV1RestServlet, client_path_pattern
from base import ClientV1RestServlet, client_path_patterns

import simplejson as json
import urllib
Expand All @@ -36,7 +36,7 @@


class LoginRestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/login$")
PATTERNS = client_path_patterns("/login$", releases=())
PASS_TYPE = "m.login.password"
SAML2_TYPE = "m.login.saml2"
CAS_TYPE = "m.login.cas"
Expand Down Expand Up @@ -238,7 +238,7 @@ def parse_cas_response(self, cas_response_body):


class SAML2RestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/login/saml2")
PATTERNS = client_path_patterns("/login/saml2", releases=())

def __init__(self, hs):
super(SAML2RestServlet, self).__init__(hs)
Expand Down Expand Up @@ -282,7 +282,7 @@ def on_POST(self, request):

# TODO Delete this after all CAS clients switch to token login instead
class CasRestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/login/cas")
PATTERNS = client_path_patterns("/login/cas", releases=())

def __init__(self, hs):
super(CasRestServlet, self).__init__(hs)
Expand All @@ -293,7 +293,7 @@ def on_GET(self, request):


class CasRedirectServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/login/cas/redirect")
PATTERNS = client_path_patterns("/login/cas/redirect", releases=())

def __init__(self, hs):
super(CasRedirectServlet, self).__init__(hs)
Expand All @@ -316,7 +316,7 @@ def on_GET(self, request):


class CasTicketServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/login/cas/ticket")
PATTERNS = client_path_patterns("/login/cas/ticket", releases=())

def __init__(self, hs):
super(CasTicketServlet, self).__init__(hs)
Expand Down
6 changes: 3 additions & 3 deletions synapse/rest/client/v1/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from synapse.api.errors import SynapseError
from synapse.types import UserID
from .base import ClientV1RestServlet, client_path_pattern
from .base import ClientV1RestServlet, client_path_patterns

import simplejson as json
import logging
Expand All @@ -28,7 +28,7 @@


class PresenceStatusRestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/presence/(?P<user_id>[^/]*)/status")
PATTERNS = client_path_patterns("/presence/(?P<user_id>[^/]*)/status")

@defer.inlineCallbacks
def on_GET(self, request, user_id):
Expand Down Expand Up @@ -73,7 +73,7 @@ def on_OPTIONS(self, request):


class PresenceListRestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/presence/list/(?P<user_id>[^/]*)")
PATTERNS = client_path_patterns("/presence/list/(?P<user_id>[^/]*)")

@defer.inlineCallbacks
def on_GET(self, request, user_id):
Expand Down
8 changes: 4 additions & 4 deletions synapse/rest/client/v1/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
""" This module contains REST servlets to do with profile: /profile/<paths> """
from twisted.internet import defer

from .base import ClientV1RestServlet, client_path_pattern
from .base import ClientV1RestServlet, client_path_patterns
from synapse.types import UserID

import simplejson as json


class ProfileDisplaynameRestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/profile/(?P<user_id>[^/]*)/displayname")
PATTERNS = client_path_patterns("/profile/(?P<user_id>[^/]*)/displayname")

@defer.inlineCallbacks
def on_GET(self, request, user_id):
Expand Down Expand Up @@ -56,7 +56,7 @@ def on_OPTIONS(self, request, user_id):


class ProfileAvatarURLRestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/profile/(?P<user_id>[^/]*)/avatar_url")
PATTERNS = client_path_patterns("/profile/(?P<user_id>[^/]*)/avatar_url")

@defer.inlineCallbacks
def on_GET(self, request, user_id):
Expand Down Expand Up @@ -89,7 +89,7 @@ def on_OPTIONS(self, request, user_id):


class ProfileRestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/profile/(?P<user_id>[^/]*)")
PATTERNS = client_path_patterns("/profile/(?P<user_id>[^/]*)")

@defer.inlineCallbacks
def on_GET(self, request, user_id):
Expand Down
4 changes: 2 additions & 2 deletions synapse/rest/client/v1/push_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from synapse.api.errors import (
SynapseError, Codes, UnrecognizedRequestError, NotFoundError, StoreError
)
from .base import ClientV1RestServlet, client_path_pattern
from .base import ClientV1RestServlet, client_path_patterns
from synapse.storage.push_rule import (
InconsistentRuleException, RuleNotFoundException
)
Expand All @@ -31,7 +31,7 @@


class PushRuleRestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/pushrules/.*$")
PATTERNS = client_path_patterns("/pushrules/.*$")
SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR = (
"Unrecognised request: You probably wanted a trailing slash")

Expand Down
4 changes: 2 additions & 2 deletions synapse/rest/client/v1/pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

from synapse.api.errors import SynapseError, Codes
from synapse.push import PusherConfigException
from .base import ClientV1RestServlet, client_path_pattern
from .base import ClientV1RestServlet, client_path_patterns

import simplejson as json


class PusherRestServlet(ClientV1RestServlet):
PATTERN = client_path_pattern("/pushers/set$")
PATTERNS = client_path_patterns("/pushers/set$")

@defer.inlineCallbacks
def on_POST(self, request):
Expand Down
4 changes: 2 additions & 2 deletions synapse/rest/client/v1/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from synapse.api.errors import SynapseError, Codes
from synapse.api.constants import LoginType
from base import ClientV1RestServlet, client_path_pattern
from base import ClientV1RestServlet, client_path_patterns
import synapse.util.stringutils as stringutils

from synapse.util.async import run_on_reactor
Expand Down Expand Up @@ -48,7 +48,7 @@ class RegisterRestServlet(ClientV1RestServlet):
handler doesn't have a concept of multi-stages or sessions.
"""

PATTERN = client_path_pattern("/register$")
PATTERNS = client_path_patterns("/register$", releases=())

def __init__(self, hs):
super(RegisterRestServlet, self).__init__(hs)
Expand Down
Loading

0 comments on commit 6e70979

Please sign in to comment.