Ejemplo n.º 1
0
async def begin_webauthn(
    user_uuid: uuid.UUID = Depends(get_current_user),
):
    # get the current user from the database
    user: User = await User.get(user_uuid)
    assert user, "User not found!"

    # calls the library which provides the credential options and a state
    registration_data, state = fido2server.register_begin(
        {
            # id is a byte sequence as described in
            # https://w3c.github.io/webauthn/#dom-publickeycredentialuserentity-id
            "id": str(user.user_uuid).encode("utf-8"),
            # we use the email address here for passwordless login later
            "name": user.user_email,
            # we don't have any 'real' name to display and therefore this example uses
            # the part before the @ in the email address.
            "displayName": user.user_email.split("@")[0],
        },
        # A list of already registered credentials is passed to the library and to the
        # client to avoid adding the same webauthn credential twice.
        User.get_webauthn_credentials(user.webauthn_credentials),
        # We want to use some kind of cross platform authenticator like a Yubikey not
        # something like Windows Hello on PCs or TouchID on some macs.
        authenticator_attachment="cross-platform",
        # We want to require the pin of the authenticator as a second factor.
        user_verification="required",
        # We want to store a credential on the client side
        resident_key=True,
    )

    # create a custom response with the ``Content-Type`` ``application/cbor``. Most
    # browsers and applications won't be able to display the body of the requests.
    response = Response(
        content=cbor.encode(registration_data), media_type="application/cbor"
    )
    state.update({"exp": datetime.utcnow() + timedelta(minutes=5)})
    # set the state parameter as a signed cookie
    response.set_cookie(
        "_state",
        jwt.encode(state, config.SESSION_SECRET.get_secret_value(), algorithm="HS256"),
    )

    # return the response.
    return response
Ejemplo n.º 2
0
async def download_jao_data_v1(
    horizon: str,
    from_date: str,
    to_date: str,
    token: str = Security(access_token_header)
) -> typing.Union[JSONResponse, Response]:
    """
    Download JAO data from from_date to to_date (time period).

    The from_date and to_data need to be same format: {year}, {year}-{month}, ..., {year}-{month}-{day}T{hour}:{minute}

    The horizon parameter must be set to Yearly or Monthly depending on what dataset you want data from.

    Args:
        horizon: YEARLY or MONTHLY
        from_date: format {year}, {year}-{month}, ..., {year}-{month}-{day}T{hour}:{minute}
        to_date: format {year}, {year}-{month}, ..., {year}-{month}-{day}T{hour}:{minute}
        token: The security access token given in the header.

    Returns:
        JSON response with data.
    """
    logger.info('download jao data requested')
    if horizon.lower() == "yearly":
        guid = config['JAO']['yearly_guid']
    elif horizon.lower() == "monthly":
        guid = config['JAO']['monthly_guid']
    else:
        message = '(ValueError) The horizon value can only be Yearly or Monthly.'
        raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=message)

    index, time_resolution = get_guid_config(guid, from_date, to_date)
    result, status_code = await download_parquet_data_v1(
        guid,
        token,
        index,
        time_resolution,
        from_date,
        to_date,
        convert_response=dataframe_to_json)

    if result is not None:
        return JSONResponse(result, status_code=status_code)

    return Response(status_code=status_code)  # No data
Ejemplo n.º 3
0
def delete_idea(
    *,
    db: Session = Depends(deps.get_db),
    id: UUID,
    current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
    """
    Delete an idea.
    """
    idea = crud.idea.get(db=db, id=id)
    if not idea:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                            detail="Idea not found")
    if idea.owner_id != current_user.id:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                            detail="Not enough permissions")
    crud.idea.remove(db=db, id=id)
    return Response(status_code=status.HTTP_204_NO_CONTENT)
Ejemplo n.º 4
0
        async def logout(
                request: fastapi.Request,
                db: Session = fastapi.Depends(self.get_session)
            ):

            user = editor.auth.get_login(request, db)

            if request.method == "POST":
                resp = Response(content="")
            elif user:
                resp = RedirectResponse(request.url.path)
            else:
                resp = HTMLResponse(editor.render_logout(request=request))

            if user:
                editor.logout(resp, user)

            return resp
Ejemplo n.º 5
0
    async def delete_job(self, jobid: str, userid: str = ""):
        """
        Delete a single k8s capture job.
        """
        jobid = str(jobid)
        api_response = await self.k8s.get_job(self.get_job_name(jobid))
        if not api_response:
            return JSONResponse(status_code=404, content={"message": "Capture job not found."})
        if userid and api_response.metadata.labels.get("userid") != userid:
            return JSONResponse(status_code=403, content={"message": f"Capture job {jobid} does not belong to userid {userid}."})

        storage_url = api_response.metadata.annotations.get("storageUrl")
        if storage_url:
            await self.storage.delete_object(storage_url)

        upstream_response = await super().remove_browser_job(jobid)
        assert upstream_response == {"deleted": True}
        return Response(status_code=204)
Ejemplo n.º 6
0
async def reply_handler(
    msg_signature: str,
    timestamp: str,
    nonce: str,
    request: Request,
):
    xml_body = await request.body()
    decrypted = crypto.decrypt_message(xml_body.decode(), msg_signature,
                                       timestamp, nonce)
    msg = parse_message(decrypted)

    dispatcher = MsgDispatcher(BaseReplyLoader())

    answer = await dispatcher.dispatch(msg)

    reply = create_reply(answer, message=msg, render=True)
    encrypted = crypto.encrypt_message(reply, nonce)
    return Response(encrypted, media_type='application/xml')
Ejemplo n.º 7
0
async def handle_get_featuretypes(dataset: str):
    """
    Handles GET requests to "/datasets/{dataset}/featuretypes.

    Parameters
    ----------
    dataset : str

    Returns
    -------
    str
    """
    featuretypes = get_featuretypes(dataset)
    headers = {
        "Content-Type": "text/plain",
        "Content-Disposition": "attachment; filename=featuretypes.txt",
    }
    return Response(content=featuretypes, headers=headers)
Ejemplo n.º 8
0
    async def _handle_http(self,
                           adapter: str,
                           data: dict = Body(...),
                           x_self_id: Optional[str] = Header(None),
                           x_signature: Optional[str] = Header(None)):
        # 检查self_id
        if not x_self_id:
            logger.warning("Missing X-Self-ID Header")
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                                detail="Missing X-Self-ID Header")

        # 检查签名
        secret = self.config.secret
        if secret:
            if not x_signature:
                logger.warning("Missing Signature Header")
                raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
                                    detail="Missing Signature")
            sig = hmac.new(secret.encode("utf-8"),
                           json.dumps(data).encode(), "sha1").hexdigest()
            if x_signature != "sha1=" + sig:
                logger.warning("Signature Header is invalid")
                raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                                    detail="Signature is invalid")

        if not isinstance(data, dict):
            logger.warning("Data received is invalid")
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)

        if x_self_id in self._clients:
            logger.warning("There's already a reverse websocket api connection,"
                           "so the event may be handled twice.")

        # 创建 Bot 对象
        if adapter in self._adapters:
            BotClass = self._adapters[adapter]
            bot = BotClass(self, "http", self.config, x_self_id)
        else:
            logger.warning("Unknown adapter")
            raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                                detail="adapter not found")

        await bot.handle_message(data)
        return Response("", 204)
Ejemplo n.º 9
0
async def exchange_currency(
    date: str = datetime.datetime.now().date(),
    quantity: decimal.Decimal = 100.0,
    from_: str = "USD",
    to_: str = "HRK",
):
    from_ = from_.upper()
    to_ = to_.upper()
    resp = requests.get(f"https://api.exchangeratesapi.io/{date}?base={from_}")
    if not resp.json().get("rates"):
        return Response(status_code=400)

    result = quantity * decimal.Decimal(resp.json()["rates"][to_])
    resp = {
        "date": date,
        from_.lower(): quantity,
        to_.lower(): result,
    }
    return resp
Ejemplo n.º 10
0
async def accept_path(name: str = "bob",
                      filename=Path(None),
                      accept=Header(None)):

    content_types = {
        "xml": "text/xml",
        "json": "application/json",
        "csv": "text/csv"
    }
    extension = filename.split(".")[-1]
    content_types.get(extension, "application/json")
    if "xml" in accept or "xml" in extension:
        # this matches default browser requests because they have "xml" in them
        media_type = content_types.get("xml")
    else:
        media_type = content_types.get(extension, "application/json")

    data = {"hello": name, "accept": accept}
    return Response(content=json.dumps(data), media_type=media_type)
Ejemplo n.º 11
0
async def download_delfin_data(
    horizon: str,
    from_date: Optional[str] = None,
    to_date: Optional[str] = None,
    table_indices: str = '',
    token: str = Security(access_token_header)
) -> typing.Union[JSONResponse, Response]:
    """
    Download Delfin data from from_date to to_date (time period).
    If from_date is left out, current UTC time is used.
    If to_date is left out, only one data point is retrieved.

    The horizon parameter must be set to Daily, Hourly or Minutely depending on what dataset you want data from:

    If Horizon is set to Daily the date(s) must be of the form {year}-{month}-{day}
    If Horizon is set to Hourly the date(s) must be of the form {year}-{month}-{day}T{hour}
    If Horizon is set to Minutely the date(s) must be of the form {year}-{month}-{day}T{hour}:{minutes}

    The tags parameter is a list of tags (comma-separated string) which can be used to filter the data based on
    the Tag column.
    """
    logger.debug('download delfin data requested')

    if horizon.lower() == 'daily':
        guid = config['Delfin']['daily_guid']
    elif horizon.lower() == 'hourly':
        guid = config['Delfin']['hourly_guid']
    elif horizon.lower() == 'minutely':
        guid = config['Delfin']['minutely_guid']
    else:
        message = '(ValueError) The horizon parameter must be Daily, Hourly or Minutely.'
        raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=message)

    # create filter. The outer list is OR and the inner list is AND.
    table_indices_filters = [[('TABLE_INDEX', '=', int(index))] for index in table_indices.split(',')] if \
        table_indices else None

    events, status_code = await __download_parquet_data(
        guid, token, from_date, to_date, table_indices_filters)

    if events:
        return JSONResponse(events, status_code=status_code)
    return Response(status_code=status_code)  # No data
Ejemplo n.º 12
0
async def catch_exceptions_middleware(request: Request, call_next):
    """Middleware that wraps HTTP requests and catches exceptions.

    These need to be caught here in order to ensure that the
    CORS middleware is used for the response, otherwise the client
    gets CORS related errors instead of the actual error.

    Parameters
    ----------
    request : Request
    call_next : func
        next func in the chain to call
    """
    try:
        return await call_next(request)

    except Exception as ex:
        log.error(f"Error processing request: {ex}")
        return Response("Internal server error", status_code=500)
Ejemplo n.º 13
0
async def run():
    """
    Run all task available.
    """
    id = time.time_ns()
    result: int = _start_all_robot_tasks(id)
    if result == 0:
        result_page = 'PASS'
        status_code = 200
    elif 250 >= result >= 1:
        result_page = f'FAIL: {result} tasks failed'
        status_code = 400
    else:
        result_page = f'FAIL: Errorcode {result}'
        status_code = 500
    result_page += f'<p><a href="/logs/{id}/log.html">Go to log</a></p>'
    return Response(content=result_page,
                    media_type="text/html",
                    status_code=status_code)
Ejemplo n.º 14
0
 async def dispatch(self, request: Request,
                    call_next: RequestResponseEndpoint):
     start_time = time.time()
     response = Response("Internal server error", status_code=500)
     new_log_context()
     add_log_context(env=config.environment,
                     site=request.headers.get('host', '-'),
                     method=request.method,
                     uri_path=request.url.path,
                     uri_query=dict(request.query_params.items()),
                     src_ip=request.client.host,
                     src_port=request.client.port,
                     pid=os.getpid(),
                     user_agent=request.headers.get('user-agent', '-'),
                     bytes_in=request.headers.get('content-length', '-'))
     try:
         response: Response = await call_next(request)
     finally:
         duration = time.time() - start_time
         LOG.info(bytes_out=response.headers.get('content-length', '-'),
                  status=response.status_code,
                  duration=duration,
                  request_id=response.headers.get('X-Request-Id', '-'))
         if 'discovery' in str(request.url):
             tags = {
                 'path':
                 request.url.path,
                 'xds_type':
                 response.headers.get("X-Sovereign-Requested-Type"),
                 'client_version':
                 response.headers.get("X-Sovereign-Client-Build"),
                 'response_code':
                 response.status_code,
             }
             tags = [
                 ':'.join(map(str, [k, v])) for k, v in tags.items()
                 if v is not None
             ]
             stats.increment('discovery.rq_total', tags=tags)
             stats.timing('discovery.rq_ms',
                          value=duration * 1000,
                          tags=tags)
     return response
Ejemplo n.º 15
0
    async def http_middleware(
        request: Request,
        call_next: RequestResponseEndpoint,
    ) -> Response:
        # if an osu! client is waiting on leaderboard data
        # and switches to another leaderboard, it will cancel
        # the previous request mid-way, resulting in a large
        # error in the console. this is to catch that :)

        try:
            return await call_next(request)
        except RuntimeError as exc:
            if exc.args[0] == "No response returned.":
                # client disconnected from the server
                # while we were sending the response.
                return Response("Client is stupppod")

            # unrelated issue, raise normally
            raise exc
Ejemplo n.º 16
0
async def upload(
        enable_discovery: bool = Form(
            ...,
            alias="enable-discovery",
            description="Allow search engines to index this notebook",
        ),
        enable_annotations:
    bool = Form(
        ...,
        alias="enable-annotations",
        description=
        "Enable hypothes.is based annotations UI when this notebook is rendered",
    ),
        notebook:
    UploadFile = File(
        ...,
        description=
        "Notebook data, in any format supported by Jupytext (.ipynb, .md, .Rmd, .py, etc)",
    ),
        host: Optional[str] = Header(None),
        x_forwarded_proto: str = Header("http"),
        accept: str = Header("text/plain"),
):
    """
    Upload a new notebook
    """

    data = await notebook.read()

    metadata = Metadata(
        filename=notebook.filename,
        enable_discovery=enable_discovery,
        enable_annotations=enable_annotations,
    )
    notebook_id = await backend.put(data, metadata)

    # FIXME: is this really the best way?
    url = f"{x_forwarded_proto}://{host}{app.root_path}view/{notebook_id}"
    if accept == "application/json":
        return {"url": url, "notebookId": notebook_id}
    else:
        return Response(url + "\n", media_type="text/plain")
Ejemplo n.º 17
0
async def solar_panel_defect_detection(
        dataset_uuid: str, image_urls: InspectionViewImages,
        background_tasks: BackgroundTasks) -> Response:
    """
    Runs solar panel defect detection on the supplied dataset's inspection view.
    :param dataset_uuid: dataset in Smartdata that solar panel defect detection is being applied to
    :param image_urls: URLs of images to perform detection on
    :param background_tasks: background thread worker to process the images after response
    :return: Http response OK
    @author Conor Brosnan <*****@*****.**>
    """

    select_server('solar')

    logging.info(
        f"Detecting solar panel defects for dataset_uuid {dataset_uuid}")
    background_tasks.add_task(process_solar_panel_defect_detection,
                              dataset_uuid=dataset_uuid,
                              inspection_view_images=image_urls)
    return Response()
Ejemplo n.º 18
0
def csv_response(df, bounds=None):
    """Write data frame to CSV and return Response with proper headers

    Parameters
    ----------
    df : DataFrame
    bounds : list-like of [xmin, ymin, xmax, ymax], optional (default: None)

    Returns
    -------
    fastapi Response
    """

    csv = df.to_csv(index_label="id", header=[c.lower() for c in df.columns])
    response = Response(content=csv, media_type="text/csv")

    if bounds is not None:
        response.headers["X-BOUNDS"] = ",".join(str(b) for b in bounds)

    return response
Ejemplo n.º 19
0
async def index(request: Request):

    data = {
        'method': request.method,
        'url': request.url._url,
        'query_params': {key: value
                         for key, value in
                         request.query_params.items()},
        'headers': {key: value for key, value in request.headers.items()},
        'body': (await request.body()).decode(),
        'cookies': {key: value for key, value in request.cookies.items()}
    }

    response = Response(json.dumps(data),
                        200,
                        {'Content-Type': 'application/json'})

    response.set_cookie('datetime-now',
                        datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S'),
                        expires=60*60*48)
    return response
Ejemplo n.º 20
0
 async def cookie_request(expiry: int):
     key = 'test'
     value = 'value'
     domain = 'localhost.org'
     secure = True
     http_only = False
     path = '/'
     expires = expiry
     same_site = 'none'
     response = Response()
     await set_cookie(
         response,
         key,
         value,
         expires,
         path,
         domain,
         secure,
         http_only,
         same_site)
     return response
Ejemplo n.º 21
0
async def download_json_file(
    guid: str,
    from_date: Optional[str] = None,
    to_date: Optional[str] = None,
    token: str = Security(access_token_header)
) -> typing.Union[JSONResponse, Response]:
    """
    Deprecated: Use /v1/{guid}/json

    Download JSON endpoint with data from from_date to to_date (time period).
    If form_date is left out, current UTC time is used.
    If to_date is left out, only one data point is retrieved.
    """
    logger.info('download json data requested')

    result, status_code = await __download_parquet_data(
        guid, token, from_date, to_date)

    if result:
        return JSONResponse(result, status_code=status_code)
    return Response(status_code=status_code)  # No data
Ejemplo n.º 22
0
async def custom_logging(request: Request, call_next):
    if "/embedded" in str(request.url):
        return await call_next(request)
    reqId = uuid.uuid4()
    request.state.requestId = reqId
    #if(request.method == "POST"):
    #    logging.info(",reqId:"+str(reqId)+","+str(request.query_params)+",url="+str(request.url)+",body="+str(await request.json())) 
    #if(request.method == "GET"):
    #    logging.info(",reqId:"+str(reqId)+","+str(request.query_params)+",url="+str(request.url))
    response = await call_next(request)
    body = b""
    async for chunk in response.body_iterator:
        body += chunk
    logging.info(",reqId:"+str(reqId)+","+str(request.query_params)+",url="+str(request.url)+",body="+str(body))
    return Response(
        content=body,
        status_code=response.status_code,
        headers=dict(response.headers),
        media_type=response.media_type
    )
    return response
Ejemplo n.º 23
0
    async def _handle_http(
        self,
        request: Request,
        setup: HTTPServerSetup,
    ) -> Response:
        json: Any = None
        try:
            json = await request.json()
        except Exception:
            pass

        data: Optional[dict] = None
        files: Optional[List[Tuple[str, FileTypes]]] = None
        try:
            form = await request.form()
            data = {}
            files = []
            for key, value in form.multi_items():
                if isinstance(value, UploadFile):
                    files.append((key, (value.filename, value.file,
                                        value.content_type)))
                else:
                    data[key] = value
        except Exception:
            pass
        http_request = BaseRequest(
            request.method,
            str(request.url),
            headers=request.headers.items(),
            cookies=request.cookies,
            content=await request.body(),
            data=data,
            json=json,
            files=files,
            version=request.scope["http_version"],
        )

        response = await setup.handle_func(http_request)
        return Response(response.content, response.status_code,
                        dict(response.headers))
Ejemplo n.º 24
0
async def download_dmi_coord(
    lon: float,
    lat: float,
    from_date: str,
    to_date: str,
    token: str = Security(access_token_header)
) -> typing.Union[StreamingResponse, Response]:
    """
    Download DMI endpoint with data from from_date to to_date (time period) for given coordinates (lon and lat)
    The from_date and to_data need to be same format.

    Args:
        lon: <two digit float> (e.g. 15.19)
        lat: <two digit float> (e.g. 55.00)
        from_date: format {year}, {year}-{month}, ..., {year}-{month}-{day}T{hour}:{minute}
        to_date: format {year}, {year}-{month}, ..., {year}-{month}-{day}T{hour}:{minute}
        token: The security access token given in the header.

    Returns:
        Response in parquet
    """
    logger.info('download dmi data requested')

    guid = config['DMI']['guid']
    index, time_resolution = get_guid_config(guid, from_date, to_date)

    filters = [('lon', '=', lon), ('lat', '=', lat)]
    result, status_code = await download_parquet_data_v1(
        guid,
        token,
        index,
        time_resolution,
        from_date,
        to_date,
        filters,
        convert_response=dataframe_to_parquet)

    if result is not None:
        return StreamingResponse(result, status_code=status_code)
    return Response(status_code=status_code)  # No data
Ejemplo n.º 25
0
def get_image_code(req:Request):
    """
    获取图片验证码
    : params image_code_id:  图片验证码编号
    :return:  正常:验证码图片  异常:返回json
    """
    # 业务逻辑处理
    # 生成验证码图片
    # 名字,真实文本, 图片数据
    characters = string.digits + string.ascii_lowercase
    width, height, n_len, n_class = 170, 80, 4, len(characters)
    generator = ImageCaptcha(width=width, height=height)
    random_str = ''.join([random.choice(characters) for j in range(4)])
    img = generator.create_captcha_image(random_str, (0, 0, 153), (255, 255, 255))
    # 将验证码真实值与编号保存到redis中, 设置有效期
    # redis:  字符串   列表  哈希   set
    # "key": xxx
    # 使用哈希维护有效期的时候只能整体设置
    # "image_codes": {"id1":"abc", "":"", "":""} 哈希  hset("image_codes", "id1", "abc")  hget("image_codes", "id1")
    # 单条维护记录,选用字符串
    # "image_code_编号1": "真实值"
    # "image_code_编号2": "真实值"

    # redis_store.set("image_code_%s" % image_code_id, text)
    # redis_store.expire("image_code_%s" % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES)
    #                   记录名字                          有效期                              记录值
    try:
        ip = req.get("client")[0]
        redis_cli.setex(ip+"verify_code",600, random_str)
    except Exception as e:
        # 记录日志
        logger.error(e)
        # return jsonify(errno=RET.DBERR,  errmsg="save image code id failed")
        return JSONResponse(content="保存验证码失败", status_code=status.HTTP_406_NOT_ACCEPTABLE)
    # 返回图片
    out = BytesIO()
    img.save(out, format="JPEG")
    resp = Response(out.getvalue())
    resp.headers["Content-Type"] = "image/jpg"
    return resp
Ejemplo n.º 26
0
def laborder_delete(
        order_id: str,
        patient_record: PatientRecord = Depends(_get_patientrecord),
        ukrdc3: Session = Depends(get_ukrdc3),
        audit: Auditer = Depends(get_auditer),
) -> Response:
    """Mark a particular lab order for deletion"""
    order = patient_record.lab_orders.filter(LabOrder.id == order_id).first()
    if not order:
        raise HTTPException(404, detail="Lab Order not found")

    deletes = [
        PVDelete(
            pid=item.pid,
            observation_time=item.observation_time,
            service_id=item.service_id,
        ) for item in order.result_items
    ]

    # Audit the laborder delete and then each resulitem delete
    order_audit = audit.add_event(
        Resource.LABORDER,
        order_id,
        RecordOperation.DELETE,
        parent=audit.add_event(Resource.PATIENT_RECORD, patient_record.pid,
                               RecordOperation.UPDATE),
    )
    for item in order.result_items:
        audit.add_event(
            Resource.RESULTITEM,
            item.id,
            RecordOperation.DELETE,
            parent=order_audit,
        )

    ukrdc3.bulk_save_objects(deletes)
    ukrdc3.delete(order)
    ukrdc3.commit()

    return Response(status_code=204)
Ejemplo n.º 27
0
def get_logs(logger_file, lines, html, text, refresh):
    if not os.path.isfile(logger_file):
        raise HTTPException(status_code=404,
                            detail=f"Log File {logger_file} NOT Found")

    refresh = max(refresh, 3) if refresh else 0
    if lines > 0:
        with open(logger_file) as fin:
            response_lines = list(deque(fin, lines))
            if html and not text:
                response = HTML_TEMPLATE.replace("LINES_T", str(lines))
                response = response.replace(
                    "REFRESH_T",
                    "setInterval(fetch, 1000*" + str(refresh) +
                    ");" if refresh else "",
                )
                response_type = "text/html"
            else:
                response = "".join(response_lines)
                response_type = "text/plain"
            return Response(content=response, media_type=response_type)
    return FileResponse(logger_file, media_type="text/plain")
Ejemplo n.º 28
0
async def download_xhr(
        payload: schemas.PathRequest,
        db_client: AsyncIOClient = Depends(deps.db_client),
        namespace: Namespace = Depends(deps.namespace),
):
    """
    Download a file or a folder.

    This endpoint is useful to download files with XHR. Folders will be downloaded as a
    ZIP archive."""
    try:
        file = await crud.file.get(db_client, namespace.path, payload.path)
    except errors.FileNotFound as exc:
        raise exceptions.PathNotFound(path=payload.path) from exc

    filename = file.name.encode("utf-8").decode("latin-1")
    if file.is_folder():
        headers = {
            "Content-Disposition": f'attachment; filename="{filename}.zip"',
            "Content-Type": "attachment/zip",
        }
    else:
        headers = {
            "Content-Disposition": f'attachment; filename="{filename}"',
            "Content-Length": str(file.size),
            "Content-Type": file.mediatype,
        }

    attachment = storage.download(namespace.path, payload.path)
    if file.is_folder(
    ) or file.size > config.APP_MAX_DOWNLOAD_WITHOUT_STREAMING:
        return StreamingResponse(attachment, headers=headers)

    buffer = BytesIO()
    for chunk in attachment:
        buffer.write(chunk)
    buffer.seek(0)

    return Response(buffer.read(), headers=headers)
Ejemplo n.º 29
0
async def download_parquet_files(
    guid: str,
    from_date: Optional[str] = None,
    to_date: Optional[str] = None,
    limit: Optional[int] = None,
    token: str = Security(access_token_header)
) -> typing.Union[StreamingResponse, Response]:
    """
    Download parquet endpoint with data from from_date to to_date (time period). The from_date and to_date need to
    have the same format (see formats below).

    Args:
        guid: Dataset guid
        from_date: format {year}, {year}-{month}, ..., {year}-{month}-{day}T{hour}:{minute}
        to_date: format {year}, {year}-{month}, ..., {year}-{month}-{day}T{hour}:{minute}
        limit: Used to limit the number of rows returned.
        token: The security access token given in the header.

    Returns:
        The response in parquet format.
    """
    logger.info('download parquet data requested')

    index, time_resolution = get_guid_config(guid, from_date, to_date)

    result, status_code = await download_parquet_data_v1(
        guid,
        token,
        index,
        time_resolution,
        from_date,
        to_date,
        limit=limit,
        convert_response=dataframe_to_parquet)

    if result is not None:
        return StreamingResponse(result, status_code=status_code)
    return Response(status_code=status_code)  # No data
Ejemplo n.º 30
0
async def twitch_webhook_follow_post(data: dict, request: Request):
    # [{'followed_at': '', 'from_id': '',
    # 'from_name': '', 'to_id': '', 'to_name': ''}]

    if await is_request_valid(request):
        data = data["data"][0]

        # Load the ignore list from the database on startup
        ignore_list_patterns = dict()
        query = db.session.query(IgnoreList).filter(
            IgnoreList.enabled == True).all()  # noqa E712
        for each in query:
            ignore_list_patterns[each.id] = each.pattern

        # Check the ignore list to see if the user matches any of the requirements to be ignored
        for pattern in ignore_list_patterns.values():
            if match(pattern, data["from_name"]):
                # Ignore the user, still return 204 so twitch doesn't keep resending.
                return Response(status_code=HTTP_204_NO_CONTENT)

        # Send data to the twitchbot
        ret = await send_command_to_bot("follow", [data["from_name"]])
        return ret