-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UvicornTestServer and local coverage script
- Loading branch information
1 parent
cf3f3a3
commit 7622826
Showing
11 changed files
with
113 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
__pycache__ | ||
/src/serie/version.txt | ||
/.test_data | ||
coverage.xml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,6 @@ | ||
from typing import Optional, Annotated | ||
from serie import router, __version__ | ||
from fastapi import FastAPI | ||
|
||
from aiochris.types import FeedUrl | ||
from fastapi import FastAPI, Response, status, Header | ||
|
||
from serie import ( | ||
DicomSeriesPayload, | ||
get_settings, | ||
BadAuthorizationError, | ||
ClientActions, | ||
__version__, | ||
) | ||
from serie.models import OxidicomCustomMetadata | ||
|
||
app = FastAPI( | ||
title="Specific Endpoints for Research Integration Events", | ||
|
@@ -19,30 +9,6 @@ | |
"url": "https://chrisproject.org", | ||
"email": "[email protected]", | ||
}, | ||
version=__version__, | ||
version=__version__ | ||
) | ||
|
||
|
||
@app.post("/dicom_series/") | ||
async def dicom_series( | ||
payload: DicomSeriesPayload, | ||
authorization: Annotated[str, Header()], | ||
response: Response, | ||
) -> Optional[FeedUrl]: | ||
""" | ||
Create *ChRIS* plugin instances and/or workflows on DICOM series data when an entire DICOM series is received. | ||
""" | ||
if (oxm_file := OxidicomCustomMetadata.from_pacsfile(payload.data)) is None: | ||
response.status_code = status.HTTP_204_NO_CONTENT | ||
return None | ||
|
||
settings = get_settings() | ||
actions = ClientActions(auth=authorization, url=settings.chris_url) | ||
try: | ||
feed = await actions.create_analysis( | ||
oxm_file, payload.jobs, payload.feed_name_template | ||
) | ||
except BadAuthorizationError as e: | ||
response.status_code = status.HTTP_401_UNAUTHORIZED | ||
return None | ||
return feed.url | ||
app.include_router(router) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
from serie.actions import ClientActions | ||
from serie.clients import BadAuthorizationError | ||
from serie.models import DicomSeriesPayload | ||
from serie.settings import get_settings | ||
from serie.router import router | ||
from serie.__version__ import __version__ | ||
|
||
__all__ = [ | ||
"ClientActions", | ||
"DicomSeriesPayload", | ||
"get_settings", | ||
"BadAuthorizationError", | ||
"router", | ||
"__version__", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import Optional, Annotated | ||
|
||
from aiochris.types import FeedUrl | ||
from fastapi import Response, status, Header, APIRouter | ||
|
||
from serie.clients import BadAuthorizationError | ||
from serie.models import DicomSeriesPayload, OxidicomCustomMetadata | ||
from serie.settings import get_settings | ||
from serie.actions import ClientActions | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/dicom_series/") | ||
async def dicom_series( | ||
payload: DicomSeriesPayload, | ||
authorization: Annotated[str, Header()], | ||
response: Response, | ||
) -> Optional[FeedUrl]: | ||
""" | ||
Create *ChRIS* plugin instances and/or workflows on DICOM series data when an entire DICOM series is received. | ||
""" | ||
if (oxm_file := OxidicomCustomMetadata.from_pacsfile(payload.data)) is None: | ||
response.status_code = status.HTTP_204_NO_CONTENT | ||
return None | ||
|
||
settings = get_settings() | ||
actions = ClientActions(auth=authorization, url=settings.chris_url) | ||
try: | ||
feed = await actions.create_analysis( | ||
oxm_file, payload.jobs, payload.feed_name_template | ||
) | ||
except BadAuthorizationError as e: | ||
response.status_code = status.HTTP_401_UNAUTHORIZED | ||
return None | ||
return feed.url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,30 @@ | ||
import asyncio | ||
|
||
import uvicorn | ||
import threading | ||
|
||
|
||
class UvicornTestServer: | ||
""" | ||
An uvicorn server which runs in a different thread, and can be shut down programmatically. | ||
See https://github.com/encode/uvicorn/discussions/1103 | ||
""" | ||
|
||
def __init__(self): | ||
... | ||
def __init__(self, config: uvicorn.Config): | ||
self._server = uvicorn.Server(config) | ||
self._thread = threading.Thread(daemon=True, target=self._server.run) | ||
|
||
async def start(self): | ||
self._thread.start() | ||
await self._wait_for_started() | ||
|
||
async def _wait_for_started(self): | ||
while not self._server.started: | ||
await asyncio.sleep(0.1) | ||
|
||
async def stop(self): | ||
if self._thread.is_alive(): | ||
self._server.should_exit = True | ||
while self._thread.is_alive(): | ||
await asyncio.sleep(0.1) |