Skip to content

Commit

Permalink
fix: added flexible app redirect to docs page (#2428)
Browse files Browse the repository at this point in the history
# Description

I added more flexible app redirect to `api/docs`

Closes #2377 

**Type of change**

(Please delete options that are not relevant. Remember to title the PR
according to the type of change)

- [X] Bug fix (non-breaking change which fixes an issue)


**How Has This Been Tested**
N.A.

**Checklist**

N.A.

---------

Co-authored-by: frascuchon <[email protected]>
  • Loading branch information
davidberenstein1957 and frascuchon authored Mar 6, 2023
1 parent 7199780 commit 5600301
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/argilla/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse
from pydantic import ConfigError

from argilla import __version__ as argilla_version
Expand Down Expand Up @@ -202,7 +203,18 @@ async def check_telemetry():
version=str(argilla_version),
)

app = FastAPI()

@argilla_app.get("/docs", include_in_schema=False)
async def redirect_docs():
return RedirectResponse(url=f"{settings.base_url}api/docs")


@argilla_app.get("/api", include_in_schema=False)
async def redirect_api():
return RedirectResponse(url=f"{settings.base_url}api/docs")


app = FastAPI(docs_url=None)
app.mount(settings.base_url, argilla_app)

configure_app_logging(app)
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def telemetry_track_data(mocker):
return spy


@pytest.fixture(scope="session")
def test_client():
with TestClient(app) as client:
yield client


@pytest.fixture
def mocked_client(
monkeypatch,
Expand Down
11 changes: 11 additions & 0 deletions tests/server/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
TextClassificationRecord,
)
from argilla.server.commons.models import TaskStatus, TaskType
from starlette.testclient import TestClient


def create_some_data_for_text_classification(
Expand Down Expand Up @@ -109,3 +110,13 @@ def uri_2_path(uri: str):

p = urlparse(uri)
return os.path.abspath(os.path.join(p.netloc, p.path))


def test_docs_redirect(test_client: TestClient):
response = test_client.get("/docs", follow_redirects=False)
assert response.status_code == 307
assert response.next_request.url.path == "/api/docs"

response = test_client.get("/api", follow_redirects=False)
assert response.status_code == 307
assert response.next_request.url.path == "/api/docs"

0 comments on commit 5600301

Please sign in to comment.