This commit is contained in:
@ -29,6 +29,7 @@ dependencies = [
|
|||||||
"pydantic-extra-types==2.10.5",
|
"pydantic-extra-types==2.10.5",
|
||||||
"semver==3.0.4",
|
"semver==3.0.4",
|
||||||
"pyjwt==2.10.1",
|
"pyjwt==2.10.1",
|
||||||
|
"xmltodict==1.0.2",
|
||||||
"python-multipart==0.0.20",
|
"python-multipart==0.0.20",
|
||||||
# CLI
|
# CLI
|
||||||
"typer-slim==0.16.1",
|
"typer-slim==0.16.1",
|
||||||
|
|||||||
@ -1,6 +1,13 @@
|
|||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
|
|
||||||
|
import xmltodict
|
||||||
from fastapi import APIRouter, HTTPException, Request
|
from fastapi import APIRouter, HTTPException, Request
|
||||||
|
from orjson import dumps, loads
|
||||||
|
|
||||||
|
from apps.users.v1.schema import AEMDDemandContent
|
||||||
|
from shared.redis import client
|
||||||
|
|
||||||
|
from . import schema as s
|
||||||
|
|
||||||
logger = getLogger(__name__)
|
logger = getLogger(__name__)
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
@ -13,18 +20,26 @@ router = APIRouter(
|
|||||||
|
|
||||||
@router.post('/callback')
|
@router.post('/callback')
|
||||||
async def callback(request: Request):
|
async def callback(request: Request):
|
||||||
logger.info(request.headers.get('content-type', ''))
|
|
||||||
# if not request.headers.get('content-type', '').startswith(
|
|
||||||
# 'application/xml'
|
|
||||||
# ):
|
|
||||||
# logger.warning('Content-Type must be application/xml')
|
|
||||||
# raise HTTPException(
|
|
||||||
# status_code=400, detail='Content-Type must be application/xml'
|
|
||||||
# )
|
|
||||||
|
|
||||||
body_bytes = await request.body()
|
body_bytes = await request.body()
|
||||||
if not body_bytes:
|
if not body_bytes:
|
||||||
logger.warning('Empty body')
|
logger.warning('Empty body')
|
||||||
raise HTTPException(status_code=400, detail='Empty body')
|
raise HTTPException(status_code=400, detail='Empty body')
|
||||||
|
|
||||||
print(body_bytes)
|
body = body_bytes.decode('utf-8').replace('\r', '').replace('\n', '')
|
||||||
|
body_dict = xmltodict.parse(body)
|
||||||
|
body = s.Remd.model_validate(body_dict)
|
||||||
|
file_request = body.Envelope.body.sendDocumentFileRequest
|
||||||
|
|
||||||
|
data = await client.get(f'aemd_messages:{file_request.relatesToMessage}')
|
||||||
|
|
||||||
|
if not data:
|
||||||
|
raise HTTPException(status_code=400, detail='Message not found')
|
||||||
|
|
||||||
|
messageData = AEMDDemandContent(**loads(data))
|
||||||
|
await client.delete(f'aemd_messages:{file_request.relatesToMessage}')
|
||||||
|
await client.set(
|
||||||
|
f'aemd{messageData["vitaId"]}:{messageData["emdrId"]}',
|
||||||
|
dumps(s.AEMDFileData(data=file_request.file.data)),
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
|||||||
28
src/apps/remd/v1/schema.py
Normal file
28
src/apps/remd/v1/schema.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
from typing import TypedDict
|
||||||
|
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
|
||||||
|
class FileModel(BaseModel):
|
||||||
|
data: str
|
||||||
|
|
||||||
|
|
||||||
|
class RequestModel(BaseModel):
|
||||||
|
relatesToMessage: str
|
||||||
|
file: FileModel
|
||||||
|
|
||||||
|
|
||||||
|
class BodyModel(BaseModel):
|
||||||
|
sendDocumentFileRequest: RequestModel
|
||||||
|
|
||||||
|
|
||||||
|
class EnvelopeModel(BaseModel):
|
||||||
|
body: BodyModel = Field(..., alias='s:Body')
|
||||||
|
|
||||||
|
|
||||||
|
class Remd(BaseModel):
|
||||||
|
Envelope: EnvelopeModel = Field(..., alias='s:Envelope')
|
||||||
|
|
||||||
|
|
||||||
|
class AEMDFileData(TypedDict):
|
||||||
|
data: str
|
||||||
@ -206,7 +206,7 @@ async def get_aemd(user: Annotated[User, Depends(login)]):
|
|||||||
return_items: list[s.AEMDReturnFile] = []
|
return_items: list[s.AEMDReturnFile] = []
|
||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
is_cached = await cache.get(f'aemd:{item["localUid"]}')
|
is_cached = await cache.get(f'aemd:{user.vita_id}:{item["emdrId"]}')
|
||||||
|
|
||||||
return_items.append(
|
return_items.append(
|
||||||
s.AEMDReturnFile(
|
s.AEMDReturnFile(
|
||||||
@ -233,7 +233,12 @@ async def post_aemd(user: Annotated[User, Depends(login)], emdrId: str):
|
|||||||
|
|
||||||
@router.get('/aemd/{emdrId}')
|
@router.get('/aemd/{emdrId}')
|
||||||
async def get_aemd_file(user: Annotated[User, Depends(login)], emdrId: str):
|
async def get_aemd_file(user: Annotated[User, Depends(login)], emdrId: str):
|
||||||
return
|
data = await cache.get(f'aemd:{user.vita_id}:{emdrId}')
|
||||||
|
|
||||||
|
if not data:
|
||||||
|
raise e.NotFoundException(status_code=404, detail='File not found')
|
||||||
|
|
||||||
|
return loads(data)
|
||||||
|
|
||||||
|
|
||||||
@router.post('/measurement', status_code=status.HTTP_202_ACCEPTED)
|
@router.post('/measurement', status_code=status.HTTP_202_ACCEPTED)
|
||||||
|
|||||||
Reference in New Issue
Block a user