aboutsummaryrefslogtreecommitdiffstats
path: root/src/nvd/endpoints/cpematch.py
blob: 05758594dd3558e2df884ca671ec54321708db71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""CPE Match Criteria API endpoint."""

from typing import TYPE_CHECKING, AsyncIterator, List, Optional

from ..models import CPEMatchResponse, CPEMatchString

if TYPE_CHECKING:
    from ..client import NVDClient


class CPEMatchEndpoint:
    """CPE Match Criteria API endpoint."""

    def __init__(self, client: "NVDClient") -> None:
        self.client = client

    async def get_match_criteria(self, match_criteria_id: str) -> CPEMatchString:
        """Get specific match criteria by UUID.

        Args:
            match_criteria_id: Match criteria UUID

        Returns:
            Match criteria object
        """
        response = await self.client.request(
            "GET",
            "/cpematch/2.0",
            params={"matchCriteriaId": match_criteria_id},
            response_model=CPEMatchResponse,
        )
        if not response.matchStrings:
            raise ValueError(f"Match criteria {match_criteria_id} not found")
        return response.matchStrings[0]

    async def get_cve_match_criteria(self, cve_id: str) -> List[CPEMatchString]:
        """Get all match criteria for a specific CVE.

        Args:
            cve_id: CVE identifier

        Returns:
            List of match criteria objects
        """
        results: List[CPEMatchString] = []
        async for match in self.search_match_criteria(cve_id=cve_id):
            results.append(match)
        return results

    async def search_match_criteria(
        self,
        cve_id: Optional[str] = None,
        match_criteria_id: Optional[str] = None,
        match_string_search: Optional[str] = None,
        last_mod_start_date: Optional[str] = None,
        last_mod_end_date: Optional[str] = None,
        results_per_page: int = 500,
        start_index: int = 0,
    ) -> AsyncIterator[CPEMatchString]:
        """Search for CPE match criteria.

        Args:
            cve_id: CVE identifier to get match strings for
            match_criteria_id: Specific match criteria UUID
            match_string_search: Match string pattern to search
            last_mod_start_date: Last modified start date (ISO-8601)
            last_mod_end_date: Last modified end date (ISO-8601)
            results_per_page: Results per page (max 500)
            start_index: Starting index for pagination

        Yields:
            Match criteria objects
        """
        params = {
            "cveId": cve_id,
            "matchCriteriaId": match_criteria_id,
            "matchStringSearch": match_string_search,
            "lastModStartDate": last_mod_start_date,
            "lastModEndDate": last_mod_end_date,
            "resultsPerPage": results_per_page,
            "startIndex": start_index,
        }

        current_index = start_index
        while True:
            params["startIndex"] = current_index
            response = await self.client.request(
                "GET",
                "/cpematch/2.0",
                params=params,
                response_model=CPEMatchResponse,
            )

            for match_string in response.matchStrings:
                yield match_string

            # Check if there are more results
            if current_index + response.resultsPerPage >= response.totalResults:
                break

            current_index += response.resultsPerPage