Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public API endpoint for public collections #2174

Merged
merged 18 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions backend/btrixcloud/colls.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
UpdatedResponse,
SuccessResponse,
CollectionSearchValuesResponse,
OrgPublicCollections,
CollAccessType,
)
from .utils import dt_now

Expand Down Expand Up @@ -395,6 +397,23 @@ async def add_successful_crawl_to_collections(self, crawl_id: str, cid: UUID):
)
await self.update_crawl_collections(crawl_id)

async def get_org_public_collections(self, org_slug: str):
"""List public collections for org"""
try:
org = await self.orgs.get_org_by_slug(org_slug)
# pylint: disable=broad-exception-caught
except Exception:
# pylint: disable=raise-missing-from
raise HTTPException(status_code=404, detail="public_collections_not_found")

collections, _ = await self.list_collections(
org.id, access=CollAccessType.PUBLIC
)
if not collections:
raise HTTPException(status_code=404, detail="public_collections_not_found")

return OrgPublicCollections(orgName=org.name, collections=collections)


# ============================================================================
# pylint: disable=too-many-locals
Expand Down Expand Up @@ -582,4 +601,12 @@ async def download_collection(
):
return await colls.download_collection(coll_id, org)

@app.get(
"/public-collections/{org_slug}",
tags=["collections"],
response_model=OrgPublicCollections,
)
async def get_org_public_collections(org_slug: str):
return await colls.get_org_public_collections(org_slug)

return colls
9 changes: 9 additions & 0 deletions backend/btrixcloud/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,15 @@ class RenameOrg(BaseModel):
slug: Optional[str] = None


# ============================================================================
class OrgPublicCollections(BaseModel):
"""Model for listing public collections in org"""

orgName: str

collections: List[CollOut]


# ============================================================================
class OrgStorageRefs(BaseModel):
"""Input model for setting primary storage + optional replicas"""
Expand Down
8 changes: 8 additions & 0 deletions backend/btrixcloud/orgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ async def get_org_by_id(self, oid: UUID) -> Organization:

return Organization.from_dict(res)

async def get_org_by_slug(self, slug: str) -> Organization:
"""Get an org by id"""
res = await self.orgs.find_one({"slug": slug})
if not res:
raise HTTPException(status_code=400, detail="invalid_org_slug")

return Organization.from_dict(res)

async def get_default_org(self) -> Organization:
"""Get default organization"""
res = await self.orgs.find_one({"default": True})
Expand Down
48 changes: 48 additions & 0 deletions backend/test/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

_coll_id = None
_second_coll_id = None
_public_coll_id = None
upload_id = None
modified = None

Expand Down Expand Up @@ -66,6 +67,7 @@ def test_create_public_collection(
assert data["added"]
assert data["name"] == PUBLIC_COLLECTION_NAME

global _public_coll_id
_public_coll_id = data["id"]

# Verify that it is public
Expand Down Expand Up @@ -725,6 +727,52 @@ def test_filter_sort_collections(
assert r.json()["detail"] == "invalid_sort_direction"


def test_list_public_collections(
crawler_auth_headers, default_org_id, crawler_crawl_id, admin_crawl_id
):
# Create new public collection
r = requests.post(
f"{API_PREFIX}/orgs/{default_org_id}/collections",
headers=crawler_auth_headers,
json={
"crawlIds": [crawler_crawl_id],
"name": "Second public collection",
"access": "public",
},
)
assert r.status_code == 200
second_public_coll_id = r.json()["id"]

# Get default org slug
r = requests.get(
f"{API_PREFIX}/orgs/{default_org_id}",
headers=crawler_auth_headers,
)
assert r.status_code == 200
data = r.json()
org_slug = data["slug"]
org_name = data["name"]

# List public collections with no auth
r = requests.get(f"{API_PREFIX}/public-collections/{org_slug}")
assert r.status_code == 200
data = r.json()
assert data["orgName"] == org_name

collections = data["collections"]
assert len(collections) == 2
for collection in collections:
assert collection["id"] in (_public_coll_id, second_public_coll_id)
assert collection["access"] == "public"

# Test non-existing slug - it should return a 404 specifying that
# public collections weren't found but not reveal whether or not
# an org exists with that slug
r = requests.get(f"{API_PREFIX}/public-collections/nonexistentslug")
assert r.status_code == 404
assert r.json()["detail"] == "public_collections_not_found"


def test_delete_collection(crawler_auth_headers, default_org_id, crawler_crawl_id):
# Delete second collection
r = requests.delete(
Expand Down
Loading