Skip to content

Commit

Permalink
Accept SecretStr as the Slack webhook URL
Browse files Browse the repository at this point in the history
Allow the URL argument to SlackWebhookClient and SlackRouteErrorHandler
to be given as a SecretStr so that applications that get that value
from a SecretStr field of their configuration don't have to convert
it first.
  • Loading branch information
rra committed Jul 18, 2024
1 parent 6b7b99a commit a20dec3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions changelog.d/20240717_223040_rra_DM_45281_queue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### New features

- Allow the Slack webhook URL argument to `SlackWebhookClient` and `SlackRouteErrorHandler` to be given as a Pydantic `SecretStr` instead of a `str`. This simplifies code in applications that get that value from a secret.
10 changes: 7 additions & 3 deletions src/safir/slack/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from fastapi import HTTPException, Request, Response
from fastapi.exceptions import RequestValidationError
from fastapi.routing import APIRoute
from pydantic import SecretStr
from starlette.exceptions import HTTPException as StarletteHTTPException
from structlog.stdlib import BoundLogger

Expand Down Expand Up @@ -56,9 +57,12 @@ class SlackWebhookClient:
"""

def __init__(
self, hook_url: str, application: str, logger: BoundLogger
self, hook_url: str | SecretStr, application: str, logger: BoundLogger
) -> None:
self._hook_url = hook_url
if isinstance(hook_url, SecretStr):
self._hook_url = hook_url.get_secret_value()
else:
self._hook_url = hook_url
self._application = application
self._logger = logger

Expand Down Expand Up @@ -175,7 +179,7 @@ class SlackRouteErrorHandler(APIRoute):

@classmethod
def initialize(
cls, hook_url: str, application: str, logger: BoundLogger
cls, hook_url: str | SecretStr, application: str, logger: BoundLogger
) -> None:
"""Configure Slack alerting.
Expand Down
3 changes: 2 additions & 1 deletion tests/slack/webhook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import structlog
from fastapi import APIRouter, FastAPI
from httpx import ASGITransport, AsyncClient
from pydantic import SecretStr

from safir.datetime import current_datetime, format_datetime_for_logging
from safir.slack.blockkit import SlackException, SlackMessage
Expand Down Expand Up @@ -44,7 +45,7 @@ async def test_post(mock_slack: MockSlackWebhook) -> None:
@pytest.mark.asyncio
async def test_post_exception(mock_slack: MockSlackWebhook) -> None:
logger = structlog.get_logger(__file__)
client = SlackWebhookClient(mock_slack.url, "App", logger)
client = SlackWebhookClient(SecretStr(mock_slack.url), "App", logger)

exc = SlackException("Some exception message")
await client.post_exception(exc)
Expand Down

0 comments on commit a20dec3

Please sign in to comment.