Beispiel #1
0
 def _call(self, request):
     response = request.response
     data = {}
     exc_info = None
     callable = None
     try:
         try:
             data = yield from as_coroutine(request.body_data())
         except ValueError:
             raise InvalidRequest(
                 status=415, msg='Content-Type must be application/json')
         if data.get('jsonrpc') != self.version:
             raise InvalidRequest(
                 'jsonrpc must be supplied and equal to "%s"' %
                 self.version)
         params = data.get('params')
         if isinstance(params, dict):
             args, kwargs = (), params
         else:
             args, kwargs = tuple(params or ()), {}
         #
         callable = self.get_handler(data.get('method'))
         result = yield from as_coroutine(callable(request, *args,
                                                   **kwargs))
     except Exception as exc:
         result = exc
         exc_info = sys.exc_info()
     else:
         try:
             json.dumps(result)
         except Exception as exc:
             result = exc
             exc_info = sys.exc_info()
     #
     res = {'id': data.get('id'), "jsonrpc": self.version}
     if exc_info:
         msg = None
         code = getattr(result, 'fault_code', None)
         if not code:
             if isinstance(result, TypeError) and callable:
                 msg = checkarity(callable, args, kwargs, discount=1)
             code = -32602 if msg else -32603
         msg = msg or str(result) or 'JSON RPC exception'
         code = getattr(result, 'fault_code', code)
         if code == -32603:
             logger.error(msg, exc_info=exc_info)
         else:
             logger.warning(msg)
         error = {
             'code': code,
             'message': msg,
             'data': getattr(result, 'data', '')
         }
         response.status_code = getattr(result, 'status', 400)
         res['error'] = error
     else:
         res['result'] = result
     return res
Beispiel #2
0
 def _call(self, request):
     response = request.response
     data = {}
     exc_info = None
     callable = None
     try:
         try:
             data = yield from as_coroutine(request.body_data())
         except ValueError:
             raise InvalidRequest(
                 status=415, msg='Content-Type must be application/json')
         if data.get('jsonrpc') != self.version:
             raise InvalidRequest(
                 'jsonrpc must be supplied and equal to "%s"' %
                 self.version)
         params = data.get('params')
         if isinstance(params, dict):
             args, kwargs = (), params
         else:
             args, kwargs = tuple(params or ()), {}
         #
         callable = self.get_handler(data.get('method'))
         result = yield from as_coroutine(
             callable(request, *args, **kwargs))
     except Exception as exc:
         result = exc
         exc_info = sys.exc_info()
     else:
         try:
             json.dumps(result)
         except Exception as exc:
             result = exc
             exc_info = sys.exc_info()
     #
     res = {'id': data.get('id'), "jsonrpc": self.version}
     if exc_info:
         msg = None
         code = getattr(result, 'fault_code', None)
         if not code:
             if isinstance(result, TypeError) and callable:
                 msg = checkarity(callable, args, kwargs, discount=1)
             code = -32602 if msg else -32603
         msg = msg or str(result) or 'JSON RPC exception'
         code = getattr(result, 'fault_code', code)
         if code == -32603:
             logger.error(msg, exc_info=exc_info)
         else:
             logger.warning(msg)
         error = {'code': code,
                  'message': msg,
                  'data': getattr(result, 'data', '')}
         response.status_code = getattr(result, 'status', 400)
         res['error'] = error
     else:
         res['result'] = result
     return res
Beispiel #3
0
    def _release(self):
        self.git = git = yield from Git.create()
        self.gitapi = gitapi = git.api()

        path = yield from git.toplevel()
        self.logger.info('Repository directory %s', path)
        # Read the release note file
        note_file = os.path.join(path, self.cfg.note_file)
        if not os.path.isfile(note_file):
            raise ImproperlyConfigured('%s file not available' % note_file)

        self.releases_path = os.path.dirname(note_file)
        release_json = os.path.join(self.releases_path, 'release.json')

        if not os.path.isfile(release_json):
            raise ImproperlyConfigured('%s file not available' % release_json)

        # Load release config and notes
        with open(release_json, 'r') as file:
            release = json.load(file)

        with open(note_file, 'r') as file:
            release['body'] = file.read().strip()

        yield from as_coroutine(self.cfg.before_commit(self, release))

        # Validate new tag and write the new version
        tag_name = release['tag_name']
        repo = gitapi.repo(git.repo_path)
        version = yield from repo.validate_tag(tag_name)
        self.logger.info('Bump to version %s', version)
        self.cfg.change_version(self, tuple(version))
        #
        if self.cfg.commit or self.cfg.push:
            #
            # Add release note to the changelog
            yield from as_coroutine(self.cfg.write_notes(self, release))
            self.logger.info('Commit changes')
            result = yield from git.commit(msg='Release %s' % tag_name)
            self.logger.info(result)
            if self.cfg.push:
                self.logger.info('Push changes changes')
                result = yield from git.push()
                self.logger.info(result)

                self.logger.info('Creating a new tag %s' % tag_name)
                tag = yield from repo.create_tag(release)
                self.logger.info('Congratulation, the new release %s is out',
                                 tag)
Beispiel #4
0
 def _call(self, request, path, start_response):
     data, files = yield from as_coroutine(request.data_and_files())
     response = yield from self.http_client.request(
         request.method, path, data=data, files=files,
         headers=self.request_headers(request.environ),
         version=request.get('SERVER_PROTOCOL'))
     response.raise_for_status()
     start_response(response.get_status(), list(response.headers))
     return [response.get_content()]
Beispiel #5
0
 def _call(self, websocket, body):
     # Process subprotocol
     (i, ), data = struct.unpack("I", body[:4]), body[4:]
     methods, blob = data[:i], data[i:]
     methods = methods.decode("utf-8").split(".")
     method = methods[0]
     proc = self.get_handler(method)
     if len(methods) > 1:
         method = methods[1]
     yield from as_coroutine(proc(websocket, method, blob))
Beispiel #6
0
 def _call(self, websocket, body):
     # Process subprotocol
     (i, ), data = struct.unpack("I", body[:4]), body[4:]
     methods, blob = data[:i], data[i:]
     methods = methods.decode("utf-8").split(".")
     method = methods[0]
     proc = self.get_handler(method)
     if len(methods) > 1:
         method = methods[1]
     yield from as_coroutine(proc(websocket, method, blob))
Beispiel #7
0
 def _call(self, request, path, start_response):
     data, files = yield from as_coroutine(request.data_and_files())
     response = yield from self.http_client.request(
         request.method,
         path,
         data=data,
         files=files,
         headers=self.request_headers(request.environ),
         version=request.get('SERVER_PROTOCOL'))
     response.raise_for_status()
     start_response(response.get_status(), list(response.headers))
     return [response.get_content()]
Beispiel #8
0
    def _release(self):
        self.git = git = yield from Git.create(self.cfg)
        path = yield from git.toplevel()
        self.logger.info('Repository directory %s', path)

        with open(os.path.join(path, 'release', 'release.json'), 'r') as file:
            release = json.load(file)

        # Read the release note file
        note_file = self.cfg.note_file
        with open(os.path.join(path, 'release', note_file), 'r') as file:
            release['body'] = file.read().strip()

        yield from as_coroutine(self.cfg.before_commit(self, release))

        # Validate new tag and write the new version
        tag_name = release['tag_name']
        version = yield from git.validate_tag(tag_name)
        self.logger.info('Bump to version %s', version)
        self.cfg.change_version(self, tuple(version))
        #
        if self.cfg.commit or self.cfg.push:
            #
            # Add release note to the changelog
            yield from as_coroutine(self.cfg.write_notes(self, path,
                                                         version, release))
            self.logger.info('Commit changes')
            result = yield from git.commit(msg='Release %s' % tag_name)
            self.logger.info(result)
            if self.cfg.push:
                self.logger.info('Push changes changes')
                result = yield from git.push()
                self.logger.info(result)

                self.logger.info('Creating a new tag %s' % tag_name)
                tag = yield from git.create_tag(release)
                self.logger.info('Congratulation, the new release %s is out',
                                 tag)
Beispiel #9
0
    def _call(self, request, data):
        exc_info = None
        procedure = None
        try:
            if (not isinstance(data, dict) or
                    data.get('jsonrpc') != self.version or
                    'id' not in data):
                raise InvalidRequest(
                    'jsonrpc must be supplied and equal to "%s"' %
                    self.version
                )
            params = data.get('params')
            if isinstance(params, dict):
                args, kwargs = (), params
            else:
                args, kwargs = tuple(params or ()), {}
            #
            procedure = self.get_handler(data.get('method'))
            result = yield from as_coroutine(
                procedure(request, *args, **kwargs))
        except Exception as exc:
            result = exc
            exc_info = sys.exc_info()
        else:
            try:
                json.dumps(result)
            except Exception as exc:
                result = exc
                exc_info = sys.exc_info()
        #
        if exc_info:
            if isinstance(result, TypeError) and procedure:
                msg = checkarity(procedure, args, kwargs, discount=1)
            else:
                msg = None

            rpc_id = data.get('id') if isinstance(data, dict) else None

            res, status = self._get_error_and_status(
                result, msg=msg, rpc_id=rpc_id, exc_info=exc_info)
        else:
            res = {
                'id': data.get('id'),
                'jsonrpc': self.version,
                'result': result
            }
            status = 200

        return res, status
Beispiel #10
0
    def _execute_request(self, request):
        response = request.response

        try:
            data = yield from as_coroutine(request.body_data())
        except ValueError:
            res, status = self._get_error_and_status(InvalidRequest(
                status=415, msg='Content-Type must be application/json'))
        else:
            # if it's batch request
            if isinstance(data, list):
                status = 200

                tasks = [self._call(request, each) for each in data]
                result = yield from asyncio.gather(*tasks)
                res = [r[0] for r in result]
            else:
                res, status = yield from self._call(request, data)

        response.status_code = status
        return Json(res).http_response(request)
Beispiel #11
0
    def _execute_request(self, request):
        response = request.response

        try:
            data = yield from as_coroutine(request.body_data())
        except ValueError:
            res, status = self._get_error_and_status(InvalidRequest(
                status=415, msg='Content-Type must be application/json'))
        else:
            # if it's batch request
            if isinstance(data, list):
                status = 200

                tasks = [self._call(request, each) for each in data]
                yield from multi_async(tasks, raise_on_error=False)
                res = [each.result()[0] for each in tasks]
            else:
                res, status = yield from self._call(request, data)

        response.status_code = status
        return res
Beispiel #12
0
 def __call__(self, request, name):
     data = yield from as_coroutine(request.body_data())
     assert(data['method'] == name)
     return True
Beispiel #13
0
 def __call__(self, request, name):
     data = yield from as_coroutine(request.body_data())
     assert (data['method'] == name)
     return True