Fix errors when winget searches our source

This commit is contained in:
Michael Herrmann
2023-02-17 12:02:43 +01:00
parent 935313f356
commit 60b76909c3
2 changed files with 27 additions and 3 deletions
+12
View File
@@ -191,6 +191,18 @@ class APITest(TestCase):
installer_json, = version_json['Installers']
self._check_vscode_installer_json(installer, installer_json)
def test_nonexistent_manifest_returns_204_not_404(self):
# This is a peculiarity / inconsistency of the winget client. The API
# design docs say that packageManifests should return 404 when a package
# does not exist. But winget doesn't gracefully handle this case.
# Instead, it expects HTTP 204.
# See: https://github.com/microsoft/winget-cli-restsource/issues/170
url = self._reverse('packageManifests', {'identifier': 'nonexistent'})
response = self.client.get(url)
self.assertEqual(204, response.status_code)
def _check_vscode_installer_json(
self, installer, installer_json, scope=None
):
+15 -3
View File
@@ -1,6 +1,6 @@
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_GET, require_POST
@@ -69,9 +69,21 @@ def manifestSearch(_, data, tenant):
@require_GET
@load_tenant
@return_jsonresponse
def packageManifests(request, tenant, identifier):
package = get_object_or_404(Package, tenant=tenant, identifier=identifier)
try:
package = Package.objects.get(tenant=tenant, identifier=identifier)
except ObjectDoesNotExist:
# This is a peculiarity / inconsistency of the winget client. The API
# design docs say that packageManifests should return 404 when a package
# does not exist. But winget doesn't gracefully handle this case.
# Instead, it expects HTTP 204.
# See: https://github.com/microsoft/winget-cli-restsource/issues/170
return HttpResponse(status=204)
return _packageManifests(request, package)
@return_jsonresponse
def _packageManifests(request, package):
return {
'PackageIdentifier': package.identifier,
'Versions': [