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

Move v1-only APIs into their own module & isolate deprecated ones #3460

Merged
merged 21 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from 11 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
Empty file added changelog.d/3460.misc
Empty file.
39 changes: 32 additions & 7 deletions synapse/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from six import PY3

from synapse.http.server import JsonResource
from synapse.rest.client import versions
from synapse.rest.client.v1 import admin, directory, events, initial_sync
from synapse.rest.client.v1 import admin, directory
from synapse.rest.client.v1 import login as v1_login
from synapse.rest.client.v1 import logout, presence, profile, push_rule, pusher
from synapse.rest.client.v1 import register as v1_register
from synapse.rest.client.v1 import room, voip
from synapse.rest.client.v1 import (
logout,
presence,
profile,
push_rule,
pusher,
room,
voip,
)
from synapse.rest.client.v2_alpha import (
account,
account_data,
Expand All @@ -42,6 +50,16 @@
user_directory,
)

if not PY3:
from synapse.rest.client.v1_only import (
register as v1_register,
)

from synapse.rest.client.v1 import (
events,
initial_sync,
)


class ClientRestResource(JsonResource):
"""A resource for version 1 of the matrix client API."""
Expand All @@ -54,14 +72,21 @@ def __init__(self, hs):
def register_servlets(client_resource, hs):
versions.register_servlets(client_resource)

# "v1"
if not PY3:
# "v1" (Python 2 only)
v1_register.register_servlets(hs, client_resource)

# Deprecated in r0
events.register_servlets(hs, client_resource)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is duplicated below, and probably can't actually be made PY2-only, because it provides https://matrix.org/docs/spec/client_server/unstable.html#id95, which isn't deprecated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yeah. Fixed.

initial_sync.register_servlets(hs, client_resource)
room.register_deprecated_servlets(hs, client_resource)

# "v1" + "r0"
room.register_servlets(hs, client_resource)
events.register_servlets(hs, client_resource)
v1_register.register_servlets(hs, client_resource)
v1_login.register_servlets(hs, client_resource)
profile.register_servlets(hs, client_resource)
presence.register_servlets(hs, client_resource)
initial_sync.register_servlets(hs, client_resource)
directory.register_servlets(hs, client_resource)
voip.register_servlets(hs, client_resource)
admin.register_servlets(hs, client_resource)
Expand Down
5 changes: 4 additions & 1 deletion synapse/rest/client/v1/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,10 +832,13 @@ def register_servlets(hs, http_server):
RoomSendEventRestServlet(hs).register(http_server)
PublicRoomListRestServlet(hs).register(http_server)
RoomStateRestServlet(hs).register(http_server)
RoomInitialSyncRestServlet(hs).register(http_server)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, this can't be removed because it's used for peeking.

RoomRedactEventRestServlet(hs).register(http_server)
RoomTypingRestServlet(hs).register(http_server)
SearchRestServlet(hs).register(http_server)
JoinedRoomsRestServlet(hs).register(http_server)
RoomEventServlet(hs).register(http_server)
RoomEventContextServlet(hs).register(http_server)


def register_deprecated_servlets(hs, http_server):
RoomInitialSyncRestServlet(hs).register(http_server)
Empty file.
38 changes: 38 additions & 0 deletions synapse/rest/client/v1_only/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
# Copyright 2014-2016 OpenMarket Ltd
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could do with updating

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This module contains base REST classes for constructing client v1 servlets.
"""

import re

from synapse.api.urls import CLIENT_PREFIX


def v1_only_client_path_patterns(path_regex, include_in_unstable=True):
"""Creates a regex compiled client path with the correct client path
prefix.

Args:
path_regex (str): The regex string to match. This should NOT have a ^
as this will be prefixed.
Returns:
SRE_Pattern
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like it returns a list to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

"""
patterns = [re.compile("^" + CLIENT_PREFIX + path_regex)]
if include_in_unstable:
unstable_prefix = CLIENT_PREFIX.replace("/api/v1", "/unstable")
patterns.append(re.compile("^" + unstable_prefix + path_regex))
return patterns
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
from synapse.api.constants import LoginType
from synapse.api.errors import Codes, SynapseError
from synapse.http.servlet import parse_json_object_from_request
from synapse.rest.client.v1.base import ClientV1RestServlet
from synapse.types import create_requester

from .base import ClientV1RestServlet, client_path_patterns
from .base import v1_only_client_path_patterns

logger = logging.getLogger(__name__)

Expand All @@ -52,7 +53,7 @@ class RegisterRestServlet(ClientV1RestServlet):
handler doesn't have a concept of multi-stages or sessions.
"""

PATTERNS = client_path_patterns("/register$", releases=(), include_in_unstable=False)
PATTERNS = v1_only_client_path_patterns("/register$", include_in_unstable=False)

def __init__(self, hs):
"""
Expand Down Expand Up @@ -389,7 +390,7 @@ class CreateUserRestServlet(ClientV1RestServlet):
"""Handles user creation via a server-to-server interface
"""

PATTERNS = client_path_patterns("/createUser$", releases=())
PATTERNS = v1_only_client_path_patterns("/createUser$")

def __init__(self, hs):
super(CreateUserRestServlet, self).__init__(hs)
Expand Down
90 changes: 10 additions & 80 deletions tests/rest/client/v1/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,100 +14,30 @@
# limitations under the License.

""" Tests REST events for /events paths."""

from mock import Mock, NonCallableMock
from six import PY3

# twisted imports
from twisted.internet import defer

import synapse.rest.client.v1.events
import synapse.rest.client.v1.register
import synapse.rest.client.v1.room

from tests import unittest

from ....utils import MockHttpResource, setup_test_homeserver
from .utils import RestTestCase

PATH_PREFIX = "/_matrix/client/api/v1"


class EventStreamPaginationApiTestCase(unittest.TestCase):
""" Tests event streaming query parameters and start/end keys used in the
Pagination stream API. """
user_id = "sid1"

def setUp(self):
# configure stream and inject items
pass

def tearDown(self):
pass

def TODO_test_long_poll(self):
# stream from 'end' key, send (self+other) message, expect message.

# stream from 'END', send (self+other) message, expect message.

# stream from 'end' key, send (self+other) topic, expect topic.

# stream from 'END', send (self+other) topic, expect topic.

# stream from 'end' key, send (self+other) invite, expect invite.

# stream from 'END', send (self+other) invite, expect invite.

pass

def TODO_test_stream_forward(self):
# stream from START, expect injected items

# stream from 'start' key, expect same content

# stream from 'end' key, expect nothing

# stream from 'END', expect nothing

# The following is needed for cases where content is removed e.g. you
# left a room, so the token you're streaming from is > the one that
# would be returned naturally from START>END.
# stream from very new token (higher than end key), expect same token
# returned as end key
pass

def TODO_test_limits(self):
# stream from a key, expect limit_num items

# stream from START, expect limit_num items

pass

def TODO_test_range(self):
# stream from key to key, expect X items

# stream from key to END, expect X items

# stream from START to key, expect X items

# stream from START to END, expect all items
pass

def TODO_test_direction(self):
# stream from END to START and fwds, expect newest first

# stream from END to START and bwds, expect oldest first

# stream from START to END and fwds, expect oldest first

# stream from START to END and bwds, expect newest first

pass


class EventStreamPermissionsTestCase(RestTestCase):
""" Tests event streaming (GET /events). """

if PY3:
skip = "Deprecated APIs are not being ported to Python 3."

@defer.inlineCallbacks
def setUp(self):
import synapse.rest.client.v1.events
import synapse.rest.client.v1_only.register
import synapse.rest.client.v1.room

self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)

hs = yield setup_test_homeserver(
Expand All @@ -125,7 +55,7 @@ def setUp(self):

hs.get_handlers().federation_handler = Mock()

synapse.rest.client.v1.register.register_servlets(hs, self.mock_resource)
synapse.rest.client.v1_only.register.register_servlets(hs, self.mock_resource)
synapse.rest.client.v1.events.register_servlets(hs, self.mock_resource)
synapse.rest.client.v1.room.register_servlets(hs, self.mock_resource)

Expand Down
6 changes: 4 additions & 2 deletions tests/rest/client/v1/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@
import json

from mock import Mock
from six import PY3

from twisted.internet import defer

from synapse.rest.client.v1.register import CreateUserRestServlet

from tests import unittest
from tests.utils import mock_getRawHeaders


class CreateUserServletTestCase(unittest.TestCase):
if PY3:
skip = "v1-only APIs not ported to Python 3"

def setUp(self):
from synapse.rest.client.v1_only.register import CreateUserRestServlet
# do the dance to hook up request data to self.request_data
self.request_data = ""
self.request = Mock(
Expand Down
9 changes: 8 additions & 1 deletion tests/rest/client/v1/test_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import json

from mock import Mock, NonCallableMock
from six import PY3
from six.moves.urllib import parse as urlparse

# twisted imports
from twisted.internet import defer

import synapse.rest.client.v1.room
Expand Down Expand Up @@ -918,6 +918,10 @@ def test_rooms_messages_sent(self):

class RoomInitialSyncTestCase(RestTestCase):
""" Tests /rooms/$room_id/initialSync. """

if PY3:
skip = "Deprecated APIs are not being ported to Python 3"

user_id = "@sid1:red"

@defer.inlineCallbacks
Expand Down Expand Up @@ -951,6 +955,9 @@ def _insert_client_ip(*args, **kwargs):
hs.get_datastore().insert_client_ip = _insert_client_ip

synapse.rest.client.v1.room.register_servlets(hs, self.mock_resource)
synapse.rest.client.v1.room.register_deprecated_servlets(
hs, self.mock_resource
)

# create the room
self.room_id = yield self.create_room_as(self.user_id)
Expand Down