From 7a93e8ad8c30cf91a2d2c358cbfa33f4a39b3c1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Mon, 29 Jul 2024 18:36:29 +0200 Subject: [PATCH] new: create user, get config --- pyvulnerabilitylookup/api.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/pyvulnerabilitylookup/api.py b/pyvulnerabilitylookup/api.py index 7fe3274..7313784 100644 --- a/pyvulnerabilitylookup/api.py +++ b/pyvulnerabilitylookup/api.py @@ -34,6 +34,10 @@ class PyVulnerabilityLookup(): if proxies: self.session.proxies.update(proxies) + def set_apikey(self, apikey: str) -> None: + '''Set the API key to use for the requests''' + self.session.headers['X-API-KEY'] = apikey + @property def is_up(self) -> bool: '''Test if the given instance is accessible''' @@ -61,6 +65,11 @@ class PyVulnerabilityLookup(): r = self.session.get(urljoin(self.root_url, 'info')) return r.json() + def get_config_info(self) -> dict[str, Any]: + '''Get more information about the current databases in use and when it was updated''' + r = self.session.get(urljoin(self.root_url, 'configInfo')) + return r.json() + def get_last(self, number: int | None=None, source: str | None = None) -> list[dict[str, Any]]: '''Get the last vulnerabilities @@ -99,13 +108,13 @@ class PyVulnerabilityLookup(): # NOTE: endpoints /api/cve/*, /api/dbInfo, /api/last are alises for backward compat. - def create_comment(self, comment: Dict[str, Any]) -> Dict[str, Any]: + def create_comment(self, comment: dict[str, Any]) -> dict[str, Any]: '''Create a comment. :param comment: The comment ''' r = self.session.post(urljoin(self.root_url, str(PurePosixPath('api', 'comment'))), - json=comment) + json=comment) return r.json() def get_comments(self, uuid: str | None = None, vuln_id: str | None = None, @@ -128,13 +137,13 @@ class PyVulnerabilityLookup(): r = self.session.delete(urljoin(self.root_url, str(PurePosixPath('api', 'comment', comment_uuid)))) return r.status_code - def create_bundle(self, bundle: Dict[str, Any]) -> Dict[str, Any]: + def create_bundle(self, bundle: dict[str, Any]) -> dict[str, Any]: '''Create a bundle. :param bundle: The bundle ''' r = self.session.post(urljoin(self.root_url, str(PurePosixPath('api', 'bundle'))), - json=bundle) + json=bundle) return r.json() def get_bundles(self, uuid: str | None = None, vuln_id: str | None = None, @@ -156,3 +165,15 @@ class PyVulnerabilityLookup(): ''' r = self.session.delete(urljoin(self.root_url, str(PurePosixPath('api', 'bundle', bundle_uuid)))) return r.status_code + + def create_user(self, login: str, name: str, organisation: str, email: str) -> dict[str, Any]: + '''Create a user. + + :param login: The login of the user + :param name: The name of the user + :param organisation: The organisation of the user + :param email: The email of the user + ''' + r = self.session.post(urljoin(self.root_url, str(PurePosixPath('api', 'user'))), + json={'login': login, 'name': name, 'organisation': organisation, 'email': email}) + return r.json()