-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: transform api responses to model objects
- Loading branch information
1 parent
eb0aa04
commit 0efdc56
Showing
8 changed files
with
1,202 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# SPDX-FileCopyrightText: 2024-present linuxdaemon <linuxdaemon.irc@gmail.com> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
from collections.abc import Mapping | ||
from typing import Type, cast | ||
|
||
from gvm.transforms import EtreeCheckCommandTransform | ||
|
||
from gvm_sync_targets.models.assets_response import GetAssetsResponse | ||
from gvm_sync_targets.models.model import Model | ||
from gvm_sync_targets.util import Element | ||
|
||
_MODEL_MAP: Mapping[str, type[Model]] = { | ||
"get_assets_response": GetAssetsResponse, | ||
} | ||
|
||
|
||
class ModelTransform(EtreeCheckCommandTransform): | ||
def __init__(self) -> None: | ||
super().__init__() # type: ignore[no-untyped-call] | ||
|
||
def __call__(self, data: str) -> Model: | ||
elem = cast(Element, super().__call__(data)) | ||
return _MODEL_MAP[elem.tag].from_xml_tree(elem) # type: ignore[arg-type] # etree.Element isn't a type but that's what this function expects |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# SPDX-FileCopyrightText: 2024-present linuxdaemon <linuxdaemon.irc@gmail.com> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
import datetime | ||
from typing import Annotated, Optional | ||
|
||
from pydantic import PlainSerializer | ||
from pydantic_xml import attr, element, wrapped | ||
|
||
from gvm_sync_targets.models.model import Model | ||
|
||
IntBoolean = Annotated[ | ||
bool, PlainSerializer(lambda value: 1 if value else 0, return_type=int) | ||
] | ||
|
||
|
||
class Severity(Model, tag="severity"): | ||
value: float = element() | ||
|
||
|
||
class Source(Model, tag="source"): | ||
uuid: str = attr("id") | ||
|
||
type: str = element() | ||
data: Optional[str] = element(default=None) | ||
deleted: IntBoolean = element(default=False) | ||
name: Optional[str] = element(default=None) | ||
|
||
|
||
class Detail(Model, tag="detail"): | ||
name: str = element() | ||
value: str = element() | ||
source: Source = element() | ||
|
||
|
||
class Host(Model, tag="host"): | ||
severity: Severity = element() | ||
details: list[Detail] = element("detail") | ||
|
||
|
||
class Owner(Model, tag="owner"): | ||
name: str = element() | ||
|
||
|
||
class Permission(Model, tag="permission"): | ||
name: str = element() | ||
|
||
|
||
class Permissions(Model, tag="permissions"): | ||
permissions: list[Permission] = element() | ||
|
||
|
||
class OS(Model, tag="os"): | ||
uuid: str = attr("id") | ||
|
||
title: str = element() | ||
|
||
|
||
class Identifier(Model, tag="identifier"): | ||
uuid: str = attr("id") | ||
|
||
name: str = element() | ||
value: str = element() | ||
creation_time: datetime.datetime = element() | ||
modification_time: datetime.datetime = element() | ||
|
||
source: Optional[Source] = None | ||
os: Optional[OS] = None | ||
|
||
|
||
class Identifiers(Model, tag="identifiers"): | ||
identifiers: list[Identifier] = element() | ||
|
||
|
||
class Asset(Model, tag="asset"): | ||
uuid: str = attr("id") | ||
|
||
owner: Owner | ||
name: str = element() | ||
comment: Optional[str] = element(default=None) | ||
creation_time: datetime.datetime = element() | ||
modification_time: datetime.datetime = element() | ||
writable: IntBoolean = element() | ||
in_use: IntBoolean = element() | ||
|
||
permissions: Permissions = element() | ||
identifiers: Identifiers = element() | ||
type: str = element() | ||
host: Host = element("host") | ||
|
||
|
||
class Keyword(Model, tag="keyword"): | ||
column: str = element() | ||
relation: str = element() | ||
value: str = element() | ||
|
||
|
||
class Keywords(Model, tag="keywords"): | ||
keywords: list[Keyword] = element() | ||
|
||
|
||
class Filters(Model, tag="filters"): | ||
uuid: str = attr("id") | ||
|
||
term: str = element() | ||
keywords: Keywords = element() | ||
|
||
|
||
class AssetCount(Model, tag="asset_count"): | ||
total: int | ||
filtered: int = element() | ||
page: int = element() | ||
|
||
|
||
class Pagination(Model): | ||
start: int = attr() | ||
max: int = attr() | ||
|
||
|
||
class SortField(Model, tag="field"): | ||
name: str | ||
order: str = element() | ||
|
||
|
||
class Sort(Model, tag="sort"): | ||
field: SortField | ||
|
||
|
||
class GetAssetsResponse(Model, tag="get_assets_response"): | ||
status: int = attr("status") | ||
status_text: str = attr("status_text") | ||
|
||
assets: list[Asset] = [] | ||
filters: Filters | ||
sort: Sort | ||
pagination: Pagination = element("assets") | ||
asset_count: AssetCount |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# SPDX-FileCopyrightText: 2024-present linuxdaemon <linuxdaemon.irc@gmail.com> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
from pydantic_xml import BaseXmlModel | ||
|
||
|
||
class Model(BaseXmlModel, extra="forbid"): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# SPDX-FileCopyrightText: 2024-present linuxdaemon <linuxdaemon.irc@gmail.com> | ||
# | ||
# SPDX-License-Identifier: MIT |
Oops, something went wrong.