async def __preprocess__(payload: None, context: EventContext,
                         request: PreprocessHook) -> MockData:
    args = await request.parsed_args()
    data = Payload.parse_form_field(args['field2'], MockData)
    return MockData(
        value=f"field1:{args['field1']} field2:{data.value} file:{args['file']}"
    )
async def __preprocess__(payload: None, context: EventContext,
                         request: PreprocessHook, *,
                         something_id: str) -> Union[str, FileUploadInfo]:
    uploaded_files = []
    save_path = Path(str(context.env['upload_something']['save_path']))
    chunk_size = int(context.env['upload_something']['chunk_size'])
    async for file_hook in request.files():
        file_name = f"{file_hook.name}-{file_hook.file_name}"
        path = save_path / file_name
        logger.info(context, f"Saving {path}...")
        await save_multipart_attachment(file_hook, path, chunk_size=chunk_size)
        uploaded_file = UploadedFile(file_hook.name,
                                     file_name,
                                     save_path.as_posix(),
                                     size=file_hook.size)
        uploaded_files.append(uploaded_file)
    args = await request.parsed_args()
    if not all(x in args for x in ('id', 'user', 'attachment', 'object')):
        request.status = 400
        return "Missing required fields"
    something_obj = Payload.parse_form_field(args['object'], Something)
    return FileUploadInfo(id=args['id'],
                          user=args['user'],
                          object=something_obj,
                          uploaded_files=uploaded_files)
Example #3
0
async def __preprocess__(payload: None, context: EventContext,
                         request: PreprocessHook, *,
                         query_arg1: str) -> Union[str, MockData]:
    fields = await request.parsed_args()
    if any(x not in fields for x in ('field1', 'field2', 'attachment')):
        request.set_status(400)
        return "Missing required fields"
    data = Payload.parse_form_field(fields['field2'], MockData)
    return MockData(
        value=
        f"field1={fields['field1']} field2={data.value} attachment={fields['attachment']}"
    )