Skip to content

Commit

Permalink
Return a link to datagrepper to the caller, for debugging
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Bompard <[email protected]>
  • Loading branch information
abompard committed Aug 22, 2024
1 parent c58ab15 commit 9ef7a8c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ async def app_config(tmp_path):
f"""
DATABASE__SQLALCHEMY__URL = "{database_url}"
LOGGING_CONFIG = "logging.yaml.example"
FASJSON_URL = "http://fasjson.example.com"
DATAGREPPER_URL = "http://datagrepper.example.com/"
"""
)
set_config_file(config_path.as_posix())
Expand Down
7 changes: 6 additions & 1 deletion tests/test_message/test_message_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ async def test_message_create(
assert sent_msg.agent_name == "dummy-fas-username"
assert sent_msg.body["body"] == json.loads(request_data)

assert response.json() == {"data": {"message_id": sent_msg.id}}
assert response.json() == {
"data": {
"message_id": sent_msg.id,
"url": f"http://datagrepper.example.com/v2/id?id={sent_msg.id}&is_raw=true&size=extra-large",
}
}


async def test_message_create_400(client, db_service, request_data, request_headers):
Expand Down
10 changes: 9 additions & 1 deletion webhook_to_fedora_messaging/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from secrets import token_urlsafe
from typing import Any

from pydantic import BaseModel, DirectoryPath
from pydantic import BaseModel, DirectoryPath, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict


Expand Down Expand Up @@ -53,17 +53,25 @@ class CacheModel(BaseModel):
setup_args: dict[str, Any] | None = None


def url_no_trailing_slash(url: str) -> str:
return url.rstrip("/")


class Config(BaseSettings):
model_config = SettingsConfigDict(env_nested_delimiter="__")

database: DBModel = DBModel()
fasjson_url: str = "https://fasjson.fedoraproject.org"
datagrepper_url: str = "https://apps.fedoraproject.org/datagrepper"
logging_config: Path = "/etc/webhook-to-fedora-messaging/logging.yaml"
oidc: OIDCModel = OIDCModel()
cache: CacheModel = CacheModel()
# It's fine if it changes on each startup: it's only used to temporarily store auth sessions
session_secret: str = token_urlsafe(42)

_normalize_fasjson_url = field_validator("fasjson_url")(url_no_trailing_slash)
_normalize_datagrepper_url = field_validator("datagrepper_url")(url_no_trailing_slash)


@cache
def get_config() -> Config:
Expand Down
16 changes: 15 additions & 1 deletion webhook_to_fedora_messaging/endpoints/models/message.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from abc import ABC
from typing import Optional

from pydantic import BaseModel, ConfigDict
from pydantic import (
BaseModel,
ConfigDict,
HttpUrl,
model_validator,
)

from webhook_to_fedora_messaging.config import get_config


class MessageBase(BaseModel, ABC):
Expand All @@ -14,6 +21,13 @@ class MessageBase(BaseModel, ABC):

class MessageExternal(MessageBase):
message_id: Optional[str]
url: Optional[HttpUrl] = None

@model_validator(mode="after")
def build_url(cls, data: "MessageExternal"):
base_url = get_config().datagrepper_url
data.url = HttpUrl(f"{base_url}/v2/id?id={data.message_id}&is_raw=true&size=extra-large")
return data


class MessageResult(BaseModel):
Expand Down

0 comments on commit 9ef7a8c

Please sign in to comment.