Skip to content

Commit

Permalink
Remove api prefix (#106)
Browse files Browse the repository at this point in the history
* Change redoc/openapi paths to point to versioned api

* Remove /api prefix

* Regenerate client
  • Loading branch information
liviuba authored May 8, 2024
1 parent ebab0f4 commit 4ac3c0f
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 207 deletions.
8 changes: 4 additions & 4 deletions api/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Create a local-only test user with:
curl -H "Content-Type: application/json" \
--request POST \
--data '{"email": "[email protected]", "password_plain": "test", "secret_token": "00123456789==" }' \
http://localhost:8080/api/v1/auth/users/register
http://localhost:8080/v1/auth/users/register
```

The response should be just `null` and there should be no errors in the api container.
Expand Down Expand Up @@ -68,15 +68,15 @@ Build images and run contains:

Create user:

`curl -H "Content-Type: application/json" --request POST --data '{"email": "[email protected]", "password_plain": "test", "secret_token": "00123456789==" }' http://localhost:8080/api/v1/auth/users/register`
`curl -H "Content-Type: application/json" --request POST --data '{"email": "[email protected]", "password_plain": "test", "secret_token": "00123456789==" }' http://localhost:8080/v1/auth/users/register`

Get auth token

`curl -H "Content-Type: application/x-www-form-urlencoded" --request POST --data '[email protected]&password=test' http://localhost:8080/api/v1/auth/token`
`curl -H "Content-Type: application/x-www-form-urlencoded" --request POST --data '[email protected]&password=test' http://localhost:8080/v1/auth/token`

Copy auth token which you can then use to make calls to the api. E.g. create a study:

`curl -H "Content-Type: application/json" -H "Authorization: Bearer <auth token>" --request POST http://localhost:8080/api/v1/private/studies -d @study_input.json`
`curl -H "Content-Type: application/json" -H "Authorization: Bearer <auth token>" --request POST http://localhost:8080/v1/private/studies -d @study_input.json`

You should be able to make your changes & can rebuild the api image with the command:

Expand Down
24 changes: 19 additions & 5 deletions api/src/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from typing import AsyncGenerator
from dotenv import load_dotenv

load_dotenv(os.environ.get("DOTENV_PATH", None))
Expand All @@ -11,6 +12,7 @@

import uvicorn
from fastapi import FastAPI, Depends
from fastapi.openapi.docs import get_redoc_html
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR
Expand Down Expand Up @@ -42,7 +44,7 @@ async def log_exception_handler(request: Request, exc: Exception):
)


async def repository_dependency() -> Repository:
async def repository_dependency() -> AsyncGenerator[Repository, None]:
db = await repository_create(init=False)
try:
yield db
Expand All @@ -55,18 +57,30 @@ async def startup_event():
await repository_create(init=True)


# Same as /openapi.json and /redoc
# Duplicated here to make them have the common /vN prefix to make proxying easy
@app.get("/v1/openapi.json", include_in_schema=False)
async def get_custom_openapi():
return app.openapi()


@app.get("/v1/redoc", include_in_schema=False)
async def custom_redoc():
return get_redoc_html(openapi_url="/v1/openapi.json", title="BioImage Archive API")


app.include_router(
auth.router, prefix="/api/v1", dependencies=[Depends(repository_dependency)]
auth.router, prefix="/v1", dependencies=[Depends(repository_dependency)]
)
app.include_router(
private.router, prefix="/api/v1", dependencies=[Depends(repository_dependency)]
private.router, prefix="/v1", dependencies=[Depends(repository_dependency)]
)
app.include_router(
admin.router, prefix="/api/v1", dependencies=[Depends(repository_dependency)]
admin.router, prefix="/v1", dependencies=[Depends(repository_dependency)]
)
# routes applied in the order they are declared
app.include_router(
public.router, prefix="/api/v1", dependencies=[Depends(repository_dependency)]
public.router, prefix="/v1", dependencies=[Depends(repository_dependency)]
)

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion api/src/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def get_image_acquisition(
return rsp.json()


TEST_SERVER_BASE_URL = "http://localhost.com/api/v1"
TEST_SERVER_BASE_URL = "http://localhost.com/v1"


def get_client(**kwargs) -> TestClient:
Expand Down
3 changes: 1 addition & 2 deletions api/src/tests_load/common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def batch_response_status_all(arr_batch_op_result, expected_status_code):

def get_study_filerefs(host, study_uuid, n_filerefs=100):
url = (
urljoin(host, f"/api/studies/{study_uuid}/file_references")
+ f"?limit={n_filerefs}"
urljoin(host, f"/studies/{study_uuid}/file_references") + f"?limit={n_filerefs}"
)
rsp = requests.get(url, verify=False)

Expand Down
Loading

0 comments on commit 4ac3c0f

Please sign in to comment.