<commit_before># -*- coding: utf-8 -*-
# Copyright 2014 OpenMarket Ltd
#
# 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.

from twisted.internet import defer

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

import json


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

    @defer.inlineCallbacks
    def on_POST(self, request):
        user, _ = yield self.auth.get_user_by_req(request)

        content = _parse_json(request)

        reqd = ['instance_handle', 'kind', 'app_id', 'app_display_name',
                'device_display_name', 'pushkey', 'lang', 'data']
        missing = []
        for i in reqd:
            if i not in content:
                missing.append(i)
        if len(missing):
            raise SynapseError(400, "Missing parameters: "+','.join(missing),
                               errcode=Codes.MISSING_PARAM)

        pusher_pool = self.hs.get_pusherpool()
        try:
            yield pusher_pool.add_pusher(
                user_name=user.to_string(),
                instance_handle=content['instance_handle'],
                kind=content['kind'],
                app_id=content['app_id'],
                app_display_name=content['app_display_name'],
                device_display_name=content['device_display_name'],
                pushkey=content['pushkey'],
                lang=content['lang'],
                data=content['data']
            )
        except PusherConfigException as pce:
            raise SynapseError(400, "Config Error: "+pce.message,
                               errcode=Codes.MISSING_PARAM)

        defer.returnValue((200, {}))<commit_msg>Allow kind to be set to null to delete a pusher.<commit_after># -*- coding: utf-8 -*-
# Copyright 2014 OpenMarket Ltd
#
# 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.

from twisted.internet import defer

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

import json


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

    @defer.inlineCallbacks
    def on_POST(self, request):
        user, _ = yield self.auth.get_user_by_req(request)

        content = _parse_json(request)

        pusher_pool = self.hs.get_pusherpool()

        if ('pushkey' in content and 'app_id' in content
                    and 'kind' in content and
                    content['kind'] is None):
            yield pusher_pool.remove_pusher(
                content['app_id'], content['pushkey']
            )
            defer.returnValue((200, {}))

        reqd = ['instance_handle', 'kind', 'app_id', 'app_display_name',
                'device_display_name', 'pushkey', 'lang', 'data']
        missing = []
        for i in reqd:
            if i not in content:
                missing.append(i)
        if len(missing):
            raise SynapseError(400, "Missing parameters: "+','.join(missing),
                               errcode=Codes.MISSING_PARAM)

        try:
            yield pusher_pool.add_pusher(
                user_name=user.to_string(),
                instance_handle=content['instance_handle'],
                kind=content['kind'],
                app_id=content['app_id'],
                app_display_name=content['app_display_name'],
                device_display_name=content['device_display_name'],
                pushkey=content['pushkey'],
                lang=content['lang'],
                data=content['data']
            )
        except PusherConfigException as pce:
            raise SynapseError(400, "Config Error: "+pce.message,
                               errcode=Codes.MISSING_PARAM)

        defer.returnValue((200, {}))
{'fraction_non_alphanumeric': 0.09564860426929392, 'fraction_numerical': 0.008825944170771757, 'mean_word_length': 2.453579021970234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5374232', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1303, 'n_words': 373}