|
1 | 1 | from json import loads
|
2 | 2 |
|
| 3 | +import requests |
3 | 4 | from celery.result import AsyncResult
|
4 | 5 | from ecoindex.backend.models.dependencies_parameters.id import IdParameter
|
5 | 6 | from ecoindex.backend.utils import check_quota
|
6 | 7 | from ecoindex.config.settings import Settings
|
7 | 8 | from ecoindex.database.engine import get_session
|
8 | 9 | from ecoindex.models import WebPage
|
9 | 10 | from ecoindex.models.enums import TaskStatus
|
10 |
| -from ecoindex.models.response_examples import example_daily_limit_response |
| 11 | +from ecoindex.models.response_examples import ( |
| 12 | + example_daily_limit_response, |
| 13 | + example_host_unreachable, |
| 14 | +) |
11 | 15 | from ecoindex.models.tasks import QueueTaskApi, QueueTaskResult
|
12 | 16 | from ecoindex.worker.tasks import ecoindex_task
|
13 | 17 | from ecoindex.worker_component import app as task_app
|
|
26 | 30 | status.HTTP_201_CREATED: {"model": str},
|
27 | 31 | status.HTTP_403_FORBIDDEN: {"model": str},
|
28 | 32 | status.HTTP_429_TOO_MANY_REQUESTS: example_daily_limit_response,
|
| 33 | + 521: example_host_unreachable, |
29 | 34 | },
|
30 | 35 | description="This submits a ecoindex analysis task to the engine",
|
31 | 36 | status_code=status.HTTP_201_CREATED,
|
@@ -54,6 +59,16 @@ async def add_ecoindex_analysis_task(
|
54 | 59 | detail="This host is excluded from the analysis",
|
55 | 60 | )
|
56 | 61 |
|
| 62 | + try: |
| 63 | + r = requests.head(url=web_page.url, timeout=5) |
| 64 | + r.raise_for_status() |
| 65 | + except Exception: |
| 66 | + print(f"The URL {web_page.url} is not reachable") |
| 67 | + raise HTTPException( |
| 68 | + status_code=521, |
| 69 | + detail=f"The URL {web_page.url} is unreachable. Are you really sure of this url? 🤔", |
| 70 | + ) |
| 71 | + |
57 | 72 | task_result = ecoindex_task.delay(
|
58 | 73 | url=str(web_page.url), width=web_page.width, height=web_page.height
|
59 | 74 | )
|
|
0 commit comments