Skip to content

Commit

Permalink
feature: add root_path settings to the default fastapi application (#769
Browse files Browse the repository at this point in the history
)
  • Loading branch information
vincentsarago authored Jan 6, 2025
1 parent 3a9aae6 commit 47eac1f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

* Add `numberMatched` and `numberReturned` properties in `types.stac.ItemCollection` model
* Add `numberMatched` and `numberReturned` properties in `types.stac.Collections` model
* Add `root_path` to `stac_fastapi.types.config.ApiSettings` and use it in the default FastAPI application
* Add `python3.13` support

## Changed
Expand Down
1 change: 1 addition & 0 deletions stac_fastapi/api/stac_fastapi/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class StacApi:
openapi_url=self.settings.openapi_url,
docs_url=self.settings.docs_url,
redoc_url=None,
root_path=self.settings.root_path,
),
takes_self=True,
),
Expand Down
64 changes: 64 additions & 0 deletions stac_fastapi/api/tests/test_app_prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,67 @@ def test_async_api_prefix(AsyncTestCoreClient, prefix):

resp = client.get(prefix + expected_path)
assert resp.status_code == 200


@pytest.mark.parametrize("prefix", ["", "/a_prefix"])
def test_api_prefix_with_root_path(TestCoreClient, prefix):
api_settings = ApiSettings(
openapi_url=f"{prefix}/api", docs_url=f"{prefix}/api.html", root_path="/api/v1"
)

api = StacApi(
settings=api_settings,
client=TestCoreClient(),
router=APIRouter(prefix=prefix),
)

prefix = "/api/v1" + prefix
with TestClient(api.app, base_url="http://stac.io", root_path="/api/v1") as client:
landing = client.get(f"{prefix}/")
assert landing.status_code == 200, landing.json()
print(landing.text)
service_doc = client.get(f"{prefix}/api.html")
assert service_doc.status_code == 200, service_doc.text

service_desc = client.get(f"{prefix}/api")
assert service_desc.status_code == 200, service_desc.json()

conformance = client.get(f"{prefix}/conformance")
assert conformance.status_code == 200, conformance.json()

# NOTE: The collections/collection/items/item links do not have the prefix
# because they are created in the fixtures
collections = client.get(f"{prefix}/collections")
assert collections.status_code == 200, collections.json()
collection_id = collections.json()["collections"][0]["id"]

collection = client.get(f"{prefix}/collections/{collection_id}")
assert collection.status_code == 200, collection.json()

items = client.get(f"{prefix}/collections/{collection_id}/items")
assert items.status_code == 200, items.json()

item_id = items.json()["features"][0]["id"]
item = client.get(f"{prefix}/collections/{collection_id}/items/{item_id}")
assert item.status_code == 200, item.json()

link_tests = [
("root", "application/json", "/"),
("conformance", "application/json", "/conformance"),
("data", "application/json", "/collections"),
("search", "application/geo+json", "/search"),
("service-doc", "text/html", "/api.html"),
("service-desc", "application/vnd.oai.openapi+json;version=3.0", "/api"),
]

for rel_type, expected_media_type, expected_path in link_tests:
link = get_link(landing.json(), rel_type)

assert link is not None, f"Missing {rel_type} link in landing page"
assert link.get("type") == expected_media_type

link_path = urllib.parse.urlsplit(link.get("href")).path
assert link_path == prefix + expected_path

resp = client.get(prefix + expected_path)
assert resp.status_code == 200
1 change: 1 addition & 0 deletions stac_fastapi/types/stac_fastapi/types/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ApiSettings(BaseSettings):

openapi_url: str = "/api"
docs_url: str = "/api.html"
root_path: str = ""

model_config = SettingsConfigDict(env_file=".env", extra="allow")

Expand Down

0 comments on commit 47eac1f

Please sign in to comment.