This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix threat * Fix tests * Fix return type
- Loading branch information
1 parent
de1a188
commit 80b4c4b
Showing
5 changed files
with
39 additions
and
29 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 |
---|---|---|
@@ -1,33 +1,41 @@ | ||
# Copyright 2024 Cisco Systems, Inc. and its affiliates | ||
from typing import Any, Dict, List, Literal, Optional | ||
from typing import List, Literal, Optional | ||
|
||
from pydantic import BaseModel, ConfigDict, Field, SerializationInfo, SerializerFunctionWrapHandler, model_serializer | ||
from pydantic import BaseModel, ConfigDict, Field | ||
|
||
Region = Literal["nam", "eur"] | ||
|
||
|
||
class ThreadGridApiEntires(BaseModel): | ||
class ThreatGridApiEntires(BaseModel): | ||
model_config = ConfigDict(extra="forbid") | ||
region: Region | ||
apikey: Optional[str] = Field(default="") | ||
|
||
|
||
class ThreatGridApi(BaseModel): | ||
class ThreatGridApiData(BaseModel): | ||
model_config = ConfigDict(extra="forbid") | ||
entries: List[ThreadGridApiEntires] = Field( | ||
entries: List[ThreatGridApiEntires] = Field( | ||
default_factory=lambda: [ | ||
ThreadGridApiEntires(region="nam"), | ||
ThreadGridApiEntires(region="eur"), | ||
] | ||
ThreatGridApiEntires(region="nam"), | ||
ThreatGridApiEntires(region="eur"), | ||
], | ||
) | ||
|
||
|
||
class ThreatGridApi(BaseModel): | ||
model_config = ConfigDict(extra="forbid") | ||
data: List[ThreatGridApiData] = Field(default_factory=lambda: [ThreatGridApiData()]) | ||
|
||
def set_region_api_key(self, region: Region, apikey: str) -> None: | ||
for entry in self.entries: | ||
for entry in self.data[0].entries: | ||
if entry.region == region: | ||
entry.apikey = apikey | ||
return | ||
raise ValueError(f"Region {region} not found in ThreatGridApi") | ||
|
||
@model_serializer(mode="wrap") | ||
def envelope_data(self, handler: SerializerFunctionWrapHandler, info: SerializationInfo) -> Dict[str, Any]: | ||
return {"data": [handler(self)]} | ||
def get_region_api_key(self, region: Region) -> str: | ||
for entry in self.data[0].entries: | ||
if entry.region == region: | ||
apikey = entry.apikey | ||
return "" if apikey is None else apikey | ||
raise ValueError(f"Region {region} not found in ThreatGridApi") |
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