Skip to content

Commit

Permalink
Fix routing for VEP (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamaldodiya authored Jul 3, 2024
1 parent 24c9d07 commit 3e760de
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions APISpecification.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ info:
title: Tools api
version: 0.0.1
paths:
/vep/config:
/api/tools/vep/config:
get:
parameters:
- name: genome_id
Expand All @@ -18,7 +18,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/VepConfigResponse'
/vep/submission:
/api/tools/vep/submissions:
post:
requestBody:
content:
Expand Down
7 changes: 3 additions & 4 deletions app/blast/blast.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from contextlib import asynccontextmanager
from urllib import response
from fastapi import FastAPI, Response
from fastapi import FastAPI, Response, APIRouter
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
Expand Down Expand Up @@ -30,7 +30,6 @@

app = FastAPI()


# Override response for input payload validation error
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
Expand Down Expand Up @@ -74,8 +73,8 @@ class DbType(str, Enum):


class BlastParams(BaseModel, extra='allow'):
email: str | None = "[email protected]"
title: str | None = ""
email: str = "[email protected]"
title: str = ""
program: Program
database: DbType

Expand Down
8 changes: 4 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from starlette.middleware.cors import CORSMiddleware

from blast.blast import app as blast_app
from vep.routes import router
from vep.routes import router as vep_router
from core.config import API_PREFIX, ALLOWED_HOSTS, VERSION, PROJECT_NAME, DEBUG

def get_application() -> FastAPI:
Expand All @@ -29,14 +29,14 @@ def get_application() -> FastAPI:
CORSMiddleware,
allow_origins=ALLOWED_HOSTS or ["*"],
allow_credentials=True,
allow_methods=["GET"],
allow_methods=["POST", "GET"],
allow_headers=["*"],
)

application.include_router(router, prefix=API_PREFIX)
application.include_router(vep_router, prefix=API_PREFIX)

return application


app = get_application()
app.mount("/blast", blast_app)
app.mount("/api/tools/blast", blast_app)
19 changes: 10 additions & 9 deletions app/tests/test_blast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import pytest
from app.blast.blast import get_db_path
from app.main import app
from core.config import API_PREFIX

# Test config endpoint
def test_read_config():
with TestClient(app) as client: #include @startup hook
with open('/data/blast_config.json') as f:
config = json.load(f)
response = client.get('/blast/config')
response = client.get(API_PREFIX + '/blast/config')
assert response.status_code == 200
assert response.json() == config

Expand All @@ -30,7 +31,7 @@ def test_get_db_path(blast_payload):
# Test single BLAST job submission with a valid payload
def test_blast_job(blast_payload):
with TestClient(app) as client:
response = client.post('/blast/job', json=blast_payload)
response = client.post(API_PREFIX + '/blast/job', json=blast_payload)
assert response.status_code == 200
resp = response.json()
assert 'submission_id' in resp
Expand All @@ -48,7 +49,7 @@ def test_blast_jobs(blast_payload):
with TestClient(app) as client:
blast_payload['genome_ids'].append(blast_payload['genome_ids'][0])
blast_payload['query_sequences'].append({'id': 2, 'value': 'MPIGSKERPTFKTRCNKADLGPI'})
response = client.post('/blast/job', json=blast_payload)
response = client.post(API_PREFIX + '/blast/job', json=blast_payload)
assert response.status_code == 200
resp = response.json()
assert 'submission_id' in resp
Expand All @@ -63,7 +64,7 @@ def test_blast_jobs(blast_payload):
def test_blast_job_data_error(blast_payload):
with TestClient(app) as client:
del blast_payload['query_sequences']
response = client.post('/blast/job', json=blast_payload)
response = client.post(API_PREFIX + '/blast/job', json=blast_payload)
assert response.status_code == 422
resp = response.json()
assert 'error' in resp
Expand All @@ -74,7 +75,7 @@ def test_blast_job_jd_error(blast_payload):
with TestClient(app) as client:
# 'stype'='dna' is incompatible with 'program'='blastp'
blast_payload['parameters']['stype'] = 'dna'
response = client.post('/blast/job', json=blast_payload)
response = client.post(API_PREFIX + '/blast/job', json=blast_payload)
assert response.status_code == 200
resp = response.json()
assert 'jobs' in resp
Expand All @@ -87,7 +88,7 @@ def test_blast_job_jd_error(blast_payload):
# Test BLAST job status endpoint
def test_blast_job_status():
with TestClient(app) as client:
response = client.get('/blast/jobs/status/ncbiblast_ensembl-12345')
response = client.get(API_PREFIX + '/blast/jobs/status/ncbiblast_ensembl-12345')
assert response.status_code == 200
assert 'status' in response.json()

Expand All @@ -96,7 +97,7 @@ def test_blast_job_status():
def test_blast_job_statuses():
with TestClient(app) as client:
job_ids = {'job_ids': ['ncbiblast-1234', 'ncbiblast-5678']}
response = client.post('/blast/jobs/status', json=job_ids)
response = client.post(API_PREFIX + '/blast/jobs/status', json=job_ids)
assert response.status_code == 200
resp = response.json()
assert 'statuses' in resp
Expand All @@ -107,13 +108,13 @@ def test_blast_job_statuses():
# Test JD proxy error response
def test_blast_proxy_error():
with TestClient(app) as client:
response = client.get('/blast/jobs/na/na')
response = client.get(API_PREFIX + '/blast/jobs/na/na')
assert response.status_code == 404
assert 'error' in response.json()

# Test invalid endpoint error response
def test_404_error():
with TestClient(app) as client:
response = client.get('/blast/invalid_path')
response = client.get(API_PREFIX + '/blast/invalid_path')
assert response.status_code == 404
assert response.json() == {'error': 'Invalid endpoint'}

0 comments on commit 3e760de

Please sign in to comment.