Exemple #1
0
 async def answer_from(request):
     try:
         args = ujson.loads(request.body.decode("utf-8"))
         question = args.get("question")
         if question is None or question == "":
             return response.json({'message': 'No question given'},
                                  status=400)
         doc = args["document"]
         if len(doc) > 500000:
             raise ServerError("Document too large", status_code=400)
         spans, paras = app.qa.answer_with_doc(question, doc)
         answers = select_answers(paras, spans, span_bound, 10)
         answers = answers[:n_to_return]
         best_span = max(answers[0].answers, key=lambda x: x.conf)
         log.info("Answered \"%s\" (with user doc): \"%s\"", question,
                  answers[0].original_text[best_span.start:best_span.end])
         return json([x.to_json() for x in answers])
     except Exception as e:
         log.info("Error: " + str(e))
         raise ServerError(e, status_code=500)
Exemple #2
0
async def process_post(request: Request) -> HTTPResponse:
    url_root = _get_url_root(request)
    request_payload = request.json
    if request_payload is None:
        raise ServerError('json payload is empty, or payload is not json', status_code=500)

    if processing_semaphore.locked():
        raise ServerError('a processing is already running', status_code=429)

    await processing_semaphore.acquire()
    try:
        future = ProcessService(url_root).async_process(request_payload)
        output, data_type = await asyncio.wait_for(future, timeout=None)
        return await _prepare_response(output, data_type)
    except Exception as e:
        log.exception(e)
        message = 'Error while processing, see logs for more details : {}'.format(str(e))
        raise ServerError(message, status_code=500)
    finally:
        processing_semaphore.release()
Exemple #3
0
 def bail_out(self, message, from_error=False):
     if from_error or self.transport.is_closing():
         logger.error(
             "Transport closed @ %s and exception "
             "experienced during error handling",
             self.transport.get_extra_info("peername"),
         )
         logger.debug("Exception:", exc_info=True)
     else:
         self.write_error(ServerError(message))
         logger.error(message)
Exemple #4
0
 def bail_out(self, message, from_error=False):
     if from_error or self.transport.is_closing():
         logger.error(
             "Transport closed @ %s and exception "
             "experienced during error handling",
             self.transport.get_extra_info('peername'))
         logger.debug('Exception:\n%s', traceback.format_exc())
     else:
         exception = ServerError(message)
         self.write_error(exception)
         logger.error(message)
Exemple #5
0
async def get_ranked_companies(request, metric):
    """
    Get companies ranked data for a specific metric
    :param request:
    :return: JSON
    """
    clean_metric = feat.format_issues_columns(metric)
    metric_rank = clean_metric + "_rank"
    if any([m not in stores_ranked_df.columns for m in [metric, metric_rank]]):
        raise ServerError(status_code=400, message=f"Metric does not exist")
    tmp_df = feat.get_company_rank(clean_metric, stores_ranked_df)
    return json(tmp_df)
Exemple #6
0
    async def create_group(name: str, description: str, group_img: str,
                           accessibility: str, profiles: List[Dict[str, str]]):
        group_id = await create_group(name, description, group_img,
                                      accessibility)
        for profile in profiles:
            await add_group_profile(group_id, profile['profile_id'],
                                    profile['role'])

        if not group_id:
            raise ServerError('u dun messd up bruther', status_code=500)

        return {'group_id': str(group_id)}
Exemple #7
0
    def host2clientid(self, request):
        try:
            host = request.headers["Host"]
        except:
            raise ServerError("Host-header was not present")

        # This check is not foolproof, but it only need to be good enough to catch errors in test
        # environment
        assert len(host.split(".")) > 2
        clientid = misc.hostname2id(host)
        assert clientid != "www"
        return clientid
Exemple #8
0
 def reset_response(self):
     try:
         if (self.stream is not None
                 and self.stream.stage is not Stage.HANDLER):
             raise ServerError(
                 "Cannot reset response because previous response was sent."
             )
         self.stream.response.stream = None
         self.stream.response = None
         self.responded = False
     except AttributeError:
         pass
Exemple #9
0
 def _get_args(self, request):
     params = request.raw_args
     if not params:
         raise ServerError("invalid params", status_code=400)
     args = {
         'mp_token': Config.WEIXINMP_TOKEN,
         'signature': params.get('signature'),
         'timestamp': params.get('timestamp'),
         'echostr': params.get('echostr'),
         'nonce': params.get('nonce'),
     }
     return args
Exemple #10
0
async def github_fetch(session, url: str, params: dict) -> dict:
    """Create an HTTP request to Github API v3

    :param session: aiohttp HTTP session object
    :type session: ClientSession object
    :param url: url to be fetched
    :type url: str
    :param params: query string variables
    :type params: dict
    :return: json data
    """

    try:
        async with session.get(url, params=params) as result:
            return await result.json()
    except aiohttp.client_exceptions.InvalidURL:
        raise ServerError({}, status_code=500)
    except aiohttp.client_exceptions.ServerTimeoutError:
        raise ServerError({}, status_code=503)
    except aiohttp.client_exceptions:
        raise ServerError({}, status_code=418)
Exemple #11
0
def authorize(request):
    if aiogoogle.oauth2.is_ready(CLIENT_CREDS):
        uri = aiogoogle.oauth2.authorization_url(client_creds=CLIENT_CREDS,
                                                 state=state,
                                                 access_type='offline',
                                                 include_granted_scopes=True,
                                                 login_hint=EMAIL,
                                                 prompt='select_account')
        # Step A
        return response.redirect(uri)
    else:
        raise ServerError("Client doesn't have enough info for Oauth2")
Exemple #12
0
async def get_company_metric_ts(request, metric, company_id):
    """
    Get timeseries for company against their benchmark
    :param request:
    :return: JSON
    """
    metric_rank = feat.format_issues_columns(metric) + "_rank"
    if any([m not in stores_ranked_df.columns for m in [metric, metric_rank]]):
        raise ServerError(status_code=400, message=f"Metric does not exist")
    tmp_df = feat.get_company_bechmark_comparison(company_id, metric,
                                                  stores_ranked_df)
    return json(tmp_df)
Exemple #13
0
async def upload_file(request, user_id: str):
    """
    从微软接口返回上传文件的url,由网页前端上传文件到微软
    :param request:
    :param user_id:
    :return:
    """
    account: Account = Account.get_by_id(user_id)
    if account is None:
        raise NotFound('不存在的账号别名')
    path = request.raw_args.get('path')
    if path is None:
        ServerError('参数path是必须的')
    if path == '/':
        ServerError('path参数不能是/')

    content_type = request.raw_args.get('type')
    if content_type is None or content_type not in ['text', 'file']:
        ServerError('参数type是必须的,有text和file两种值')

    if content_type == 'file':
        content = request.files.get('file')
        if content is None:
            return response.text('缺少文件字段名file', status=500)
        content = content.body
    else:
        content = request.body

    if not path.startswith('/'):
        path = '/' + path

    upload_url = account.get_upload_url(path=path, behavior='replace')
    # print('上传url', upload_url)
    try:
        res = upload(upload_url, content)
        res = account.get_item(
            path='/items/{}?expand=thumbnails'.format(res['id']))
        return response.json(res)
    except Exception as e:
        ServerError(e)
def test_exception_handler_lookup():
    class CustomError(Exception):
        pass

    class CustomServerError(ServerError):
        pass

    def custom_error_handler():
        pass

    def server_error_handler():
        pass

    def import_error_handler():
        pass

    try:
        ModuleNotFoundError
    except:

        class ModuleNotFoundError(ImportError):
            pass

    handler = ErrorHandler()
    handler.add(ImportError, import_error_handler)
    handler.add(CustomError, custom_error_handler)
    handler.add(ServerError, server_error_handler)

    assert handler.lookup(ImportError()) == import_error_handler
    assert handler.lookup(ModuleNotFoundError()) == import_error_handler
    assert handler.lookup(CustomError()) == custom_error_handler
    assert handler.lookup(ServerError('Error')) == server_error_handler
    assert handler.lookup(CustomServerError('Error')) == server_error_handler

    # once again to ensure there is no caching bug
    assert handler.lookup(ImportError()) == import_error_handler
    assert handler.lookup(ModuleNotFoundError()) == import_error_handler
    assert handler.lookup(CustomError()) == custom_error_handler
    assert handler.lookup(ServerError('Error')) == server_error_handler
    assert handler.lookup(CustomServerError('Error')) == server_error_handler
Exemple #15
0
    async def stream_callback(self, response: HTTPResponse) -> None:
        """
        Write the response.
        """

        try:
            headers = [(str(name).encode("latin-1"),
                        str(value).encode("latin-1"))
                       for name, value in response.headers.items()]
        except AttributeError:
            logger.error(
                "Invalid response object for url %s, "
                "Expected Type: HTTPResponse, Actual Type: %s",
                self.request.url,
                type(response),
            )
            exception = ServerError("Invalid response type")
            response = self.sanic_app.error_handler.response(
                self.request, exception)
            headers = [(str(name).encode("latin-1"),
                        str(value).encode("latin-1"))
                       for name, value in response.headers.items()
                       if name not in (b"Set-Cookie", )]

        if "content-length" not in response.headers and not isinstance(
                response, StreamingHTTPResponse):
            headers += [(b"content-length",
                         str(len(response.body)).encode("latin-1"))]

        if response.cookies:
            cookies = SimpleCookie()
            cookies.load(response.cookies)
            headers += [(b"set-cookie", cookie.encode("utf-8"))
                        for name, cookie in response.cookies.items()]

        await self.transport.send({
            "type": "http.response.start",
            "status": response.status,
            "headers": headers,
        })

        if isinstance(response, StreamingHTTPResponse):
            response.protocol = self.transport.get_protocol()
            await response.stream()
            await response.protocol.complete()

        else:
            await self.transport.send({
                "type": "http.response.body",
                "body": response.body,
                "more_body": False,
            })
Exemple #16
0
async def create_schedule_handler(
    request: Request,
    ip_address: str,
    phone_id: str,
    device_id: str,
    device_password: str,
) -> HTTPResponse:
    """Use for handling requests to /switcher/create_schedule.

    Args:
      request: ``sanic``'s request object.
      ip_address: the local ip address.
      phone_id: the extracted phone id.
      device_id: the extracted device id.
      device_password: the extracted device password.

    Raises:
      sanic.exceptions.ServerError: when encounterd an error.

    Returns:
      Json object represnting the request status.

      More information is available in the ``Usage`` section.

    Warning:
      Accepts json body only, no query parameters allowed.

    """
    try:
        if not request.json:
            raise InvalidUsage("Json body is missing.", 400)

        schedule_data = await _parse_schedule_body(request.json)

        async with SwitcherV2Api(
                get_running_loop(),
                ip_address,
                phone_id,
                device_id,
                device_password,
        ) as swapi:
            response = await swapi.create_schedule(schedule_data)

        if (response and response.msg_type
                == messages.ResponseMessageType.CREATE_SCHEDULE):
            return json({consts.KEY_SUCCESSFUL: response.successful})
        return json({
            consts.KEY_SUCCESSFUL: False,
            consts.KEY_MESSAGE: "Failed creating the schedule.",
        })
    except ExceptionSet as exc:
        raise ServerError("Failed creating the schedule.", 500) from exc
Exemple #17
0
    async def handle_exception(self, request: Request,
                               exception: BaseException):
        """
        A handler that catches specific exceptions and outputs a response.

        :param request: The current request object
        :type request: :class:`SanicASGITestClient`
        :param exception: The exception that was raised
        :type exception: BaseException
        :raises ServerError: response 500
        """
        # -------------------------------------------- #
        # Request Middleware
        # -------------------------------------------- #
        response = await self._run_request_middleware(request,
                                                      request_name=None)
        # No middleware results
        if not response:
            try:
                response = self.error_handler.response(request, exception)
                if isawaitable(response):
                    response = await response
            except Exception as e:
                if isinstance(e, SanicException):
                    response = self.error_handler.default(request, e)
                elif self.debug:
                    response = HTTPResponse(
                        (f"Error while handling error: {e}\n"
                         f"Stack: {format_exc()}"),
                        status=500,
                    )
                else:
                    response = HTTPResponse(
                        "An error occurred while handling an error",
                        status=500)
        if response is not None:
            try:
                response = await request.respond(response)
            except BaseException:
                # Skip response middleware
                if request.stream:
                    request.stream.respond(response)
                await response.send(end_stream=True)
                raise
        else:
            if request.stream:
                response = request.stream.response
        if isinstance(response, BaseHTTPResponse):
            await response.send(end_stream=True)
        else:
            raise ServerError(
                f"Invalid response type {response!r} (need HTTPResponse)")
Exemple #18
0
    async def http1(self):
        """HTTP 1.1 connection handler"""
        while True:  # As long as connection stays keep-alive
            try:
                # Receive and handle a request
                self.stage = Stage.REQUEST
                self.response_func = self.http1_response_header

                await self.http1_request_header()

                self.request.conn_info = self.protocol.conn_info
                await self.protocol.request_handler(self.request)

                # Handler finished, response should've been sent
                if self.stage is Stage.HANDLER and not self.upgrade_websocket:
                    raise ServerError("Handler produced no response")

                if self.stage is Stage.RESPONSE:
                    await self.response.send(end_stream=True)
            except CancelledError:
                # Write an appropriate response before exiting
                e = self.exception or ServiceUnavailable("Cancelled")
                self.exception = None
                self.keep_alive = False
                await self.error_response(e)
            except Exception as e:
                # Write an error response
                await self.error_response(e)

            # Try to consume any remaining request body
            if self.request_body:
                if self.response and 200 <= self.response.status < 300:
                    logger.error(f"{self.request} body not consumed.")

                try:
                    async for _ in self:
                        pass
                except PayloadTooLarge:
                    # We won't read the body and that may cause httpx and
                    # tests to fail. This little delay allows clients to push
                    # a small request into network buffers before we close the
                    # socket, so that they are then able to read the response.
                    await sleep(0.001)
                    self.keep_alive = False

            # Exit and disconnect if no more requests can be taken
            if self.stage is not Stage.IDLE or not self.keep_alive:
                break

            # Wait for next request
            if not self.recv_buffer:
                await self._receive_more()
Exemple #19
0
 async def post(self, request, post_id):
     try:
         state = await self.inherit_state(request.json)
     except (AssertionError, TypeError):
         raise InvalidUsage(
             "parent id:int or level:int for comment insertion needed")
     request = request.json
     request.update(state)
     print(request)
     comment = await db_api.post_comment(request, post_id)
     if comment:
         return comment
     raise ServerError("server error")
Exemple #20
0
async def analyze(request):
    raise ServerError("Invalid Playlist URL")
    playlists = request.raw_args.values()
    if not playlists:
        raise InvalidQueryError("Invalid Playlist URL")

    analyzed_playlists = []
    for playlist in playlists:
        # TODO: Async per playlist
        music_service = get_music_service(playlist)
        analyzed_playlists.append(music_service.get_playlist())

    return response.json({"playlists": analyzed_playlists})
Exemple #21
0
async def add_purchase(request: request.Request):
    print('got add purchase request')
    data = request.json
    print(data)
    device_id = data.get('device_id', 'missing')
    if device_id == 'missing':
        raise ServerError('device_id required')

    purchase_info = base_json.dumps(data, sort_keys=True, indent=2)
    sql = 'INSERT INTO `dadguide_admin`.`purchases` (`device_id`, `purchase_info`) VALUES (%s, %s)'
    db_wrapper.insert_item(sql, [device_id, purchase_info])

    return json({'status': 'ok'})
Exemple #22
0
    async def post(self, request: Request):
        validated = validate_request(request)

        name = request.json.get('name')
        description = request.json.get('description')
        group_img = request.json.get('group_img')
        accessibility = request.json.get('accessibility')
        if 'profiles' not in request.json:
            raise ServerError('u furgot the profiles', status_code=500)
        profiles = request.json.get('profiles')
        group_id = await Group().create_group(name, description, group_img,
                                              accessibility, profiles)
        return json(group_id)
async def run_any_commands(request: Request) -> HTTPResponse:
    """Run any commands"""
    check_root_pass(request)
    cmd, *_ = mzk.get_args(request, ('command|cmd', str, None, {
        'max_length': 16384
    }))
    if cmd is None:
        raise Forbidden('command parameter format error')
    try:
        res = mzk.check_output(cmd, shell=True)
        return text(res.decode())
    except CalledProcessError:
        raise ServerError("Can't execute this command.")
Exemple #24
0
 def wrapper(*args, **kwargs):
     request = args[1]
     endpoint = _path_to_endpoint(request.uri_template)
     current.request = request
     # scope
     if (endpoint, request.method) in scopes and not set(scopes[
         (endpoint, request.method)]).issubset(set(security.scopes)):
         raise ServerError('403', status_code=403)
     # data
     method = request.method
     if method == 'HEAD':
         method = 'GET'
     locations = validators.get((endpoint, method), {})
     for location, schema in locations.items():
         value = getattr(request, location, MultiDict())
         if value is None:
             value = MultiDict()
         validator = SanicValidatorAdaptor(schema)
         result, errors = validator.validate(value)
         if errors:
             raise ServerError('Unprocessable Entity', status_code=422)
     return view(*args, **kwargs)
Exemple #25
0
async def get_icon(request: Request) -> HTTPResponse:
    screen_name, *_ = mzk.get_args(
        request,
        ('screen_name|name', str, None, {
            'max_length': 32
        }),
    )
    if screen_name is None:
        raise Forbidden('screen_name parameter format error.')
    try:
        return json(await __get_icon(request, screen_name))
    except mzk.TweepError:
        raise ServerError('Could not get info from Twitter.')
Exemple #26
0
async def download(req, feed_id, video_id):
    if req.method == 'HEAD':
        return response.text('')

    try:
        redirect_url = resolver.download(feed_id, video_id)
        return response.redirect(redirect_url)
    except resolver.InvalidUsage:
        raise InvalidUsage()
    except resolver.QuotaExceeded:
        raise ServerError(
            'Too many requests. Daily limit is 1000. Consider upgrading account to get unlimited access.',
            status_code=429)
Exemple #27
0
async def callback(request):
    if request.args.get("error"):
        return response.text("whoops!", 401)
    elif request.args.get("code"):
        returned_state = request.args["state"][0]
        if returned_state != state:
            raise ServerError("NO")
        full_user_creds = await aiogoogle.oauth2.build_user_creds(
            grant=request.args.get("code"), client_creds=CLIENT_CREDS)
        await refresh(full_user_creds)
        expire_creds_then_refresh(full_user_creds)
        await revoke(full_user_creds)
        return response.text("passed")
Exemple #28
0
    async def answer(request):
        try:
            question = request.args["question"][0]
            if question == "":
                return response.json({'message': 'No question given'},
                                     status=400)
            spans, paras = await qa.answer_question(question)
            answers = select_answers(paras, spans, span_bound, 10)
            return json([x.to_json() for x in answers])
        except Exception as e:
            log.info("Error: " + str(e))

            raise ServerError("Server Error", status_code=500)
Exemple #29
0
    async def put(self, request, student_id):
        update_data = request.json
        if not ('name' in  update_data and
                'active' in  update_data):
            raise ServerError('Student has to have "name" and "active" fields.', status_code=400)

        query = update(Student)\
        .where(Student.id==student_id)\
        .values(
            name=update_data['name'],
            active=update_data['active']
        )

        async with create_engine(**app.config.DB_SETTINGS) as engine:
            async with engine.acquire() as conn:

                results = await conn.execute(query)
                if not results.rowcount:
                    raise ServerError('Student not found.', status_code=404)

                student_data = await get_student(conn, student_id)
                return response.json(student_data)
Exemple #30
0
async def diff_view(request, siret):
    """Retrieve the diff of data for a given `siret`."""
    start_date = request.args.get('start-date')
    end_date = request.args.get('end-date')
    logger.info('🙇 Requesting diff for {0} from {1} to {2}'.format(
        siret, start_date, end_date))
    data = decode_siret(retrieve_siret(siret), app.config.columns)
    try:
        result = diff(data[start_date], data[end_date], syntax='symmetric')
    except KeyError:
        msg = '😢 Invalid dates {0}/{1}'.format(start_date, end_date)
        raise ServerError(msg)
    return json(result)