Skip to content

Commit ab161ff

Browse files
committed
support get_closest_nuget_package_name
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent 1fa44ce commit ab161ff

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

vulntotal/ecosystem/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#

vulntotal/ecosystem/nuget.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
from urllib.parse import urljoin
11+
12+
import requests
13+
14+
15+
def get_closest_nuget_package_name(query):
16+
"""
17+
Return case-sensitive NuGet package name using
18+
SearchQueryService provided by NuGet
19+
"""
20+
url_nuget_service = "https://api.nuget.org/v3/index.json"
21+
url_nuget_search = ""
22+
23+
api_resources = requests.get(url_nuget_service).json()
24+
for resource in api_resources.get("resources") or []:
25+
if resource.get("@type") == "SearchQueryService":
26+
url_nuget_search = resource["@id"]
27+
break
28+
29+
if url_nuget_search:
30+
url_query = urljoin(url_nuget_search, f"?q={query}")
31+
query_response = requests.get(url_query).json()
32+
if query_response.get("data"):
33+
return query_response["data"][0]["id"]

0 commit comments

Comments
 (0)