-
Notifications
You must be signed in to change notification settings - Fork 2
β¨(ingestion) handle webhooks to archive offers #512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ee1ee52
β¨(ingestion-api) call tycho to archive an offer
AntoineAugusti eb3a9da
β¨(ingestion-presentation) add API endpoint to archive an offer
AntoineAugusti fd2f612
β»οΈ(ingestion) refactor to clean archi
AntoineAugusti 3f52f9e
β»οΈ(ingestion-presentation) refactor with a usecase
AntoineAugusti 6ffe903
β¨(ingestion) delete the corresponding vector
AntoineAugusti a2575b7
β¨(ingestion-infrastructure) add rate limit for the API key
AntoineAugusti 32cdec0
β»οΈ(ingestion-presentation) use serializer_class
AntoineAugusti 9c185de
π(ingestion-api) document 401 for ArchiveOffersView
AntoineAugusti 5179a97
β»οΈ(ingestion) use PostgresOffersRepository in tests
AntoineAugusti 5a43cb9
β
(tooling) deprecate get_by_reference for in memory offers repo
AntoineAugusti e86bc41
β»οΈ(ingestion-infrastructure) rename src/tycho to src/web
AntoineAugusti da0ef2d
β»οΈ(ingestion-infrastructure) fix imports order
AntoineAugusti 05f7dad
β
(tooling) revert changes to in memory offer repository
AntoineAugusti 948793d
β
(tooling) the TestArchiveOfferByReferenceUseCase is an integration test
AntoineAugusti 39b320a
π(tooling) add WEB_INGESTION_API_KEY to example env
AntoineAugusti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 +1,5 @@ | ||
| TALENTSOFT_CLIENT_ID= | ||
| TALENTSOFT_CLIENT_SECRET= | ||
|
|
||
| WEB_BASE_URL= | ||
| WEB_API_KEY= |
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
| from collections.abc import AsyncGenerator | ||
|
|
||
| import httpx | ||
| from fastapi import Depends, HTTPException | ||
|
|
||
| from api.config import Settings, get_settings | ||
| from application.use_cases.archive_offer import ArchiveOfferUseCase | ||
|
|
||
|
|
||
| async def get_http_client() -> AsyncGenerator[httpx.AsyncClient, None]: | ||
| async with httpx.AsyncClient() as client: | ||
| yield client | ||
|
|
||
|
|
||
| def get_archive_offer_use_case( | ||
| settings: Settings = Depends(get_settings), | ||
| client: httpx.AsyncClient = Depends(get_http_client), | ||
| ) -> ArchiveOfferUseCase: | ||
| if not settings.web_base_url or not settings.web_api_key: | ||
| raise HTTPException(status_code=500, detail="Web service not configured") | ||
| return ArchiveOfferUseCase( | ||
| client=client, | ||
| web_base_url=settings.web_base_url, | ||
| web_api_key=settings.web_api_key, | ||
| ) |
This file contains hidden or 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
File renamed without changes.
Empty file.
This file contains hidden or 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,21 @@ | ||
| import httpx | ||
|
|
||
|
|
||
| class ArchiveOfferUseCase: | ||
| def __init__( | ||
| self, | ||
| client: httpx.AsyncClient, | ||
| web_base_url: str, | ||
| web_api_key: str, | ||
| ) -> None: | ||
| self._client = client | ||
| self._web_base_url = web_base_url | ||
| self._web_api_key = web_api_key | ||
|
|
||
| async def execute(self, reference: str) -> None: | ||
| url = f"{self._web_base_url}/api/offers/{reference}/archive" | ||
| response = await self._client.post( | ||
| url, | ||
| headers={"Authorization": f"Api-Key {self._web_api_key}"}, | ||
| ) | ||
| response.raise_for_status() |
Empty file.
Empty file.
This file contains hidden or 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,17 @@ | ||
| from pydantic import BaseModel, ConfigDict, Field | ||
|
|
||
| _TS_ARCHIVED = "_TS_Archived" | ||
|
|
||
|
|
||
| class TalentsoftWebhookPayload(BaseModel): | ||
|
AntoineAugusti marked this conversation as resolved.
|
||
| model_config = ConfigDict(populate_by_name=True) | ||
|
|
||
| event_type: str | ||
| reference: str | None = None | ||
| status_id: str | None = Field(None, alias="statusId") | ||
|
|
||
|
|
||
| def should_archive(payload: TalentsoftWebhookPayload) -> bool: | ||
| if payload.event_type == "vacancy_deleted": | ||
| return True | ||
| return payload.event_type == "vacancy_status" and payload.status_id == _TS_ARCHIVED | ||
This file contains hidden or 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 hidden or 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,116 @@ | ||
| import base64 | ||
| import hashlib | ||
| import hmac | ||
| import json | ||
| import time | ||
| import urllib.parse | ||
|
|
||
| import pytest | ||
| from fastapi.testclient import TestClient | ||
| from httpx import Response | ||
|
|
||
| from api.main import create_app | ||
|
|
||
| TALENTSOFT_CLIENT_ID = "test_client_id" | ||
| TALENTSOFT_CLIENT_SECRET = "test_client_secret" | ||
| WEB_BASE_URL = "https://web.example.com" | ||
| WEB_API_KEY = "test-web-api-key" | ||
| WEBHOOK_PATH = "/webhooks/talentsoft" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def test_client(monkeypatch): | ||
| monkeypatch.setenv("TESTING", "true") | ||
| monkeypatch.delenv("TALENTSOFT_CLIENT_ID", raising=False) | ||
| monkeypatch.delenv("TALENTSOFT_CLIENT_SECRET", raising=False) | ||
| app = create_app() | ||
| return TestClient(app) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def talentsoft_client(monkeypatch): | ||
| monkeypatch.setenv("TESTING", "true") | ||
| monkeypatch.setenv("TALENTSOFT_CLIENT_ID", TALENTSOFT_CLIENT_ID) | ||
| monkeypatch.setenv("TALENTSOFT_CLIENT_SECRET", TALENTSOFT_CLIENT_SECRET) | ||
| monkeypatch.setenv("WEB_BASE_URL", WEB_BASE_URL) | ||
| monkeypatch.setenv("WEB_API_KEY", WEB_API_KEY) | ||
| app = create_app() | ||
| return TestClient(app) | ||
|
|
||
|
|
||
| def make_signature( | ||
| path: str, | ||
| query_items: list[tuple[str, str]], | ||
| content_type: str = "", | ||
| body: bytes = b"", | ||
| ts_rec_headers: dict[str, str] | None = None, | ||
| ) -> str: | ||
| all_ts_rec = ts_rec_headers or {} | ||
| expires = next(v for k, v in all_ts_rec.items() if k.lower() == "x-ts-rec-expires") | ||
|
|
||
| content_md5 = ( | ||
| base64.b64encode(hashlib.md5(body).digest()).decode() # noqa: S324 | ||
| if body | ||
| else "" | ||
| ) | ||
|
|
||
| canonicalized_headers_list = sorted( | ||
| (name.lower(), value.strip()) | ||
| for name, value in all_ts_rec.items() | ||
| if name.lower().startswith("x-ts-rec-") | ||
| ) | ||
| canonicalized_headers = "".join( | ||
| f"{name}:{value}\n" for name, value in canonicalized_headers_list | ||
| ) | ||
|
|
||
| params_no_sig = [(k, v) for k, v in query_items if k != "signature"] | ||
| query_string = urllib.parse.urlencode(params_no_sig) | ||
| canonicalized_resource = f"{path}?{query_string}" if query_string else path | ||
|
|
||
| string_to_sign = ( | ||
| "POST\n" | ||
| + content_md5 | ||
| + "\n" | ||
| + content_type | ||
| + "\n" | ||
| + expires | ||
| + "\n" | ||
| + canonicalized_headers | ||
| + canonicalized_resource | ||
| ) | ||
|
|
||
| secret = TALENTSOFT_CLIENT_SECRET.encode("utf-8") | ||
| digest = hmac.new(secret, string_to_sign.encode("utf-8"), hashlib.sha1).digest() | ||
| return base64.b64encode(digest).decode("utf-8") | ||
|
|
||
|
|
||
| def valid_query_items() -> list[tuple[str, str]]: | ||
| return [("client_id", TALENTSOFT_CLIENT_ID)] | ||
|
|
||
|
|
||
| def valid_ts_rec_headers(expires: int | None = None) -> dict[str, str]: | ||
| if expires is None: | ||
| expires = int(time.time()) + 300 | ||
| return {"X-TS-REC-Expires": str(expires)} | ||
|
|
||
|
|
||
| def make_signed_request(client: TestClient, body: dict) -> Response: | ||
| body_bytes = json.dumps(body).encode() | ||
| content_type = "application/json" | ||
| ts_rec_headers = valid_ts_rec_headers() | ||
|
|
||
| query_items = valid_query_items() | ||
| signature = make_signature( | ||
| WEBHOOK_PATH, | ||
| query_items, | ||
| content_type=content_type, | ||
| ts_rec_headers=ts_rec_headers, | ||
| ) | ||
| query_items.append(("signature", signature)) | ||
|
|
||
| return client.post( | ||
| WEBHOOK_PATH, | ||
| params=dict(query_items), | ||
| content=body_bytes, | ||
| headers={"Content-Type": content_type, **ts_rec_headers}, | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.