diff --git a/hyperbrowser/models/web/common.py b/hyperbrowser/models/web/common.py index 71f36c60..a022259c 100644 --- a/hyperbrowser/models/web/common.py +++ b/hyperbrowser/models/web/common.py @@ -73,6 +73,12 @@ class PageData(BaseModel): status: PageStatus error: Optional[str] = None metadata: Optional[Dict[str, Any]] = None + # HTTP status of the page the content came from. None when the browser + # received no response. + status_code: Optional[int] = Field(default=None, alias="statusCode") + # URL the page ended on after redirects. For a document such as a PDF, the + # URL of the downloaded file. + final_url: Optional[str] = Field(default=None, alias="finalUrl") markdown: Optional[str] = None html: Optional[str] = None links: Optional[List[str]] = None diff --git a/hyperbrowser/models/web/fetch.py b/hyperbrowser/models/web/fetch.py index b49db836..9488da9c 100644 --- a/hyperbrowser/models/web/fetch.py +++ b/hyperbrowser/models/web/fetch.py @@ -40,6 +40,12 @@ class FetchResponseData(BaseModel): ) metadata: Optional[Dict[str, Any]] = None + # HTTP status of the page the content came from. None when the browser + # received no response. + status_code: Optional[int] = Field(default=None, alias="statusCode") + # URL the page ended on after redirects. For a document such as a PDF, the + # URL of the downloaded file. + final_url: Optional[str] = Field(default=None, alias="finalUrl") html: Optional[str] = None markdown: Optional[str] = None links: Optional[List[str]] = None diff --git a/pyproject.toml b/pyproject.toml index 0f6dfd74..d26836dd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "hyperbrowser" -version = "1.15.5" +version = "1.15.6" description = "Python SDK for hyperbrowser" authors = ["Nikhil Shahi "] license = "MIT" diff --git a/tests/test_web_fetch_status.py b/tests/test_web_fetch_status.py new file mode 100644 index 00000000..cafd1121 --- /dev/null +++ b/tests/test_web_fetch_status.py @@ -0,0 +1,66 @@ +from hyperbrowser.models import ( + BatchFetchJobResponse, + FetchResponse, + WebCrawlJobResponse, +) + + +def test_fetch_response_reports_the_http_status_and_final_url(): + response = FetchResponse( + **{ + "jobId": "job-1", + "status": "completed", + "data": { + "statusCode": 403, + "finalUrl": "https://example.com/blocked", + "markdown": "Access denied", + "metadata": {"sourceURL": "https://example.com/old"}, + }, + } + ) + + assert response.data.status_code == 403 + assert response.data.final_url == "https://example.com/blocked" + assert response.data.model_dump(by_alias=True, exclude_none=True) == { + "statusCode": 403, + "finalUrl": "https://example.com/blocked", + "markdown": "Access denied", + "metadata": {"sourceURL": "https://example.com/old"}, + } + + +def test_fetch_response_without_the_status_fields_still_parses(): + # The API can omit both fields. + response = FetchResponse( + **{"jobId": "job-1", "status": "completed", "data": {"markdown": "# Title"}} + ) + + assert response.data.markdown == "# Title" + assert response.data.status_code is None + assert response.data.final_url is None + + +def test_batch_fetch_and_crawl_pages_report_the_http_status_and_final_url(): + page = { + "url": "https://example.com/old", + "status": "completed", + "statusCode": 404, + "finalUrl": "https://example.com/not-found", + "markdown": "Not found", + } + job = { + "jobId": "job-1", + "status": "completed", + "data": [page, {"url": "https://example.com/", "status": "completed"}], + "totalPages": 2, + "totalPageBatches": 1, + "currentPageBatch": 1, + "batchSize": 100, + } + + for response in (BatchFetchJobResponse(**job), WebCrawlJobResponse(**job)): + reported, omitted = response.data + assert reported.status_code == 404 + assert reported.final_url == "https://example.com/not-found" + assert omitted.status_code is None + assert omitted.final_url is None