generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: move models to models/webhooks * feat: add models utils methods * chore: improve docstrings * fix: add string payload validation * feat: handle aws sns payload * fix: remove trailing comma * feat: add test for invalid type * fix: test invalid signature version * fix: test all exceptions on handle sns payload * fix: rename to validate sns payload * fix: lint * refactor: break handle webhook with separate string handler * fix: handle upptime payload * fix: add missing module * fix: rate limiting test on webhooks endpoint * fix: dump model before calling bot api * fix: ensure AWS SNS payload properly parsed * fix: pass proper payloads to methods * fix: handle cases where attachements may be None * fix: dump model with exclude none set to True * fix: reorder models to have Webhook as last
- Loading branch information
Showing
9 changed files
with
899 additions
and
336 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from . import utils as model_utils | ||
|
||
__all__ = ["model_utils"] |
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,53 @@ | ||
from typing import Any, Dict, List, Type | ||
from pydantic import BaseModel | ||
|
||
|
||
def get_parameters_from_model(model: Type[BaseModel]) -> List[str]: | ||
return list(model.model_fields.keys()) | ||
|
||
|
||
def get_dict_of_parameters_from_models( | ||
models: List[Type[BaseModel]], | ||
) -> Dict[str, List[str]]: | ||
""" | ||
Returns a dictionary of model names and their parameters as a list. | ||
Args: | ||
models (List[Type[BaseModel]]): A list of models to extract parameters from. | ||
Returns: | ||
Dict[str, List[str]]: A dictionary of model names and their parameters as a list. | ||
Example: | ||
```python | ||
class User(BaseModel): | ||
id: str | ||
username: str | ||
password: str | ||
email: str | ||
class Webhook(BaseModel): | ||
id: str | ||
channel: str | ||
name: str | ||
created_at: str | ||
get_dict_of_parameters_from_models([User, Webhook]) | ||
# Output: | ||
# { | ||
# "User": ["id", "username", "password", "email"], | ||
# "Webhook": ["id", "channel", "name", "created_at"] | ||
# } | ||
``` | ||
""" | ||
return {model.__name__: get_parameters_from_model(model) for model in models} | ||
|
||
|
||
def is_parameter_in_model(model_params: List[str], payload: Dict[str, Any]) -> bool: | ||
return any(param in model_params for param in payload.keys()) | ||
|
||
|
||
def are_all_parameters_in_model( | ||
model_params: List[str], payload: Dict[str, Any] | ||
) -> bool: | ||
return all(param in model_params for param in payload.keys()) |
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
Oops, something went wrong.