Skip to content

Update get resources by batch API #3

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ GET /api/resources/find-resources-in-batch?id=riscv-ubuntu-20.04-boot&resource_v

- Each `id` parameter must have a corresponding `resource_version` parameter
- Use `resource_version=None` to retrieve all versions of a resource
- Returns 404 if any requested resource is missing
- Returns a list of resources that were found even if not all requested resources were found.
- Returns an empty list is no resources were found.

### 2. Advanced Resource Search

Expand Down
17 changes: 0 additions & 17 deletions functions/get_resources_by_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,6 @@ def find_resources_in_batch(req: func.HttpRequest) -> func.HttpResponse:
collection.find({"$or": queries}, RESOURCE_FIELDS)
)

# Check if any resources were found
if not resources:
return create_error_response(
404, "No requested resources were found"
)

# Check if at least one instance of each requested ID is present
found_ids = {resource.get("id") for resource in resources}
missing_ids = set(ids) - found_ids

if missing_ids:
return create_error_response(
404,
"The following requested resources were not found: "
f"{', '.join(missing_ids)}",
)

return func.HttpResponse(
body=json.dumps(resources),
status_code=200,
Expand Down
25 changes: 18 additions & 7 deletions tests/resources_api_unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,14 @@ def test_get_resources_by_batch_not_found_partial(self):
f"{self.base_url}/resources/find-resources-in-batch?{query_string}"
)
response = requests.get(url)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertIn("error", data)
self.assertIn("non-existent", data["error"])
self.assertIsInstance(data, list)
self.assertEqual(len(data), 1)

found_ids = [r["id"] for r in data]
self.assertEqual(len(found_ids), 1)
self.assertEqual("arm-hello64-static", found_ids[0])

def test_get_resources_by_batch_not_found_all(self):
"""Test batch retrieval where all resources are missing."""
Expand All @@ -128,7 +132,10 @@ def test_get_resources_by_batch_not_found_all(self):
f"{self.base_url}/resources/find-resources-in-batch?{query_string}"
)
response = requests.get(url)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertIsInstance(data, list)
self.assertEqual(len(data), 0)

def test_get_resources_by_batch_mismatched_parameters(self):
"""Test batch retrieval with mismatched number of id and version
Expand Down Expand Up @@ -173,10 +180,14 @@ def test_get_resources_by_batch_valid_id_invalid_version(self):
f"{self.base_url}/resources/find-resources-in-batch?{query_string}"
)
response = requests.get(url)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertIn("error", data)
self.assertIn("riscv-ubuntu-20.04-boot", data["error"])
self.assertIsInstance(data, list)
self.assertEqual(len(data), 1)

found_ids = [r["id"] for r in data]
self.assertEqual(len(found_ids), 1)
self.assertEqual("arm-hello64-static", found_ids[0])

# FILTER ENDPOINT TESTS
def test_get_filters(self):
Expand Down
Loading