-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement API endpoints for collections (#625)
* Implement API endpoint for collections index * Implement API endpoint for collection page * Implement visibility conditions * Extend github utility and code cleanup * Update feature flags * Update naming from slug to collection_name Co-authored-by: Richa Agarwal <[email protected]>
- Loading branch information
1 parent
f447a73
commit d8ef930
Showing
4 changed files
with
98 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import yaml | ||
from api.model import get_plugin | ||
from utils.github import get_file | ||
|
||
COLLECTIONS_CONTENTS = "https://api.github.com/repos/chanzuckerberg/napari-hub-collections/contents/collections" | ||
COLLECTIONS_REPO = "https://github.com/chanzuckerberg/napari-hub-collections" | ||
IMAGES_BASE_URL = "https://raw.githubusercontent.com/chanzuckerberg/napari-hub-collections/main/images/" | ||
|
||
|
||
def get_collections(): | ||
json_file = get_file(download_url=COLLECTIONS_CONTENTS, file_format="json") | ||
if not json_file: | ||
return None | ||
collections = [] | ||
for item in json_file: | ||
collection_name = item.get("name").replace(".yml", "") | ||
data = get_collection_preview(collection_name) | ||
if data: | ||
collections.append(data) | ||
return collections | ||
|
||
|
||
def get_yaml_data(collection_name, visibility_requirements): | ||
"""Return collection's yaml data if it meets visibility requirements.""" | ||
filename = "collections/{collection_name}.yml".format(collection_name=collection_name) | ||
yaml_file = get_file(download_url=COLLECTIONS_REPO, file=filename, branch="main") | ||
if yaml_file: | ||
data = yaml.safe_load(yaml_file) | ||
if data and data.get("visibility", "public") in visibility_requirements: | ||
data["cover_image"] = IMAGES_BASE_URL + data.get("cover_image") | ||
return data | ||
return None | ||
|
||
|
||
def get_collection_preview(collection_name): | ||
"""Return a subset of collection data for /collections.""" | ||
data = get_yaml_data(collection_name=collection_name, visibility_requirements=["public"]) | ||
if not data: | ||
return None | ||
return { | ||
"title": data.get("title"), | ||
"summary": data.get("summary"), | ||
"cover_image": data.get("cover_image"), | ||
"curator": data.get("curator"), | ||
"symbol": collection_name, | ||
} | ||
|
||
|
||
def get_collection(collection_name): | ||
"""Return full collection data for /collections/{collection}.""" | ||
data = get_yaml_data(collection_name=collection_name, visibility_requirements=["public", "hidden"]) | ||
if not data: | ||
return None | ||
# Get plugin-specific data | ||
plugins = get_plugin_data(data["plugins"]) | ||
data["plugins"] = list(plugins) | ||
return data | ||
|
||
|
||
def get_plugin_data(collection_plugins): | ||
"""Return plugin-specific data for each plugin specified in a collection.""" | ||
for collection_plugin in collection_plugins: | ||
plugin_name = collection_plugin["name"] | ||
plugin = get_plugin(plugin_name) | ||
# Only include plugins that are set to public | ||
if plugin and plugin.get("visibility", "public") == "public": | ||
collection_plugin["summary"] = plugin.get("summary", "") | ||
collection_plugin["authors"] = plugin.get("authors", []) | ||
collection_plugin["display_name"] = plugin.get("display_name", "") | ||
yield collection_plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters