Skip to content

Commit 8491e8f

Browse files
authored
fix: send a User-Agent header on every API request (#103)
Both clients now send `scrapegraph-py/<version>` instead of httpx's default User-Agent, on the pooled client and on the one-off absolute-URL requests, sync and async.
1 parent 07b7cad commit 8491e8f

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/scrapegraph_py/async_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import httpx
1212
from pydantic import BaseModel, TypeAdapter
1313

14+
from .client import USER_AGENT
1415
from .env import env
1516
from .schemas import (
1617
ApiResult,
@@ -262,7 +263,7 @@ def __init__(self, *, api_key: str | None = None):
262263
self._http = httpx.AsyncClient(
263264
base_url=env.base_url,
264265
timeout=env.timeout,
265-
headers={"SGAI-APIKEY": self._api_key},
266+
headers={"SGAI-APIKEY": self._api_key, "User-Agent": USER_AGENT},
266267
)
267268

268269
self.crawl = AsyncCrawlResource(self)
@@ -292,7 +293,7 @@ async def _request[T](
292293
url,
293294
json=json_body,
294295
params=params,
295-
headers={"SGAI-APIKEY": self._api_key},
296+
headers={"SGAI-APIKEY": self._api_key, "User-Agent": USER_AGENT},
296297
)
297298
else:
298299
resp = await self._http.request(method, path, json=json_body, params=params)

src/scrapegraph_py/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77
import time
88
from datetime import datetime
9+
from importlib import metadata
910
from typing import Literal
1011

1112
import httpx
@@ -44,6 +45,7 @@
4445
)
4546

4647
_SERVER_TIMING_RE = re.compile(r"dur=(\d+(?:\.\d+)?)")
48+
USER_AGENT = f"scrapegraph-py/{metadata.version('scrapegraph-py')}"
4749

4850

4951
def _debug(label: str, data: object = None) -> None:
@@ -262,7 +264,7 @@ def __init__(self, *, api_key: str | None = None):
262264
self._http = httpx.Client(
263265
base_url=env.base_url,
264266
timeout=env.timeout,
265-
headers={"SGAI-APIKEY": self._api_key},
267+
headers={"SGAI-APIKEY": self._api_key, "User-Agent": USER_AGENT},
266268
)
267269

268270
self.crawl = CrawlResource(self)
@@ -291,7 +293,7 @@ def _request[T](
291293
url,
292294
json=json_body,
293295
params=params,
294-
headers={"SGAI-APIKEY": self._api_key},
296+
headers={"SGAI-APIKEY": self._api_key, "User-Agent": USER_AGENT},
295297
timeout=env.timeout,
296298
)
297299
else:

tests/test_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from scrapegraph_py import (
7+
AsyncScrapeGraphAI,
78
FetchConfig,
89
HtmlFormatConfig,
910
ImagesFormatConfig,
@@ -14,6 +15,7 @@
1415
ScrapeGraphAI,
1516
ScreenshotFormatConfig,
1617
)
18+
from scrapegraph_py.client import USER_AGENT
1719

1820
API_KEY = "test-sgai-key"
1921
BASE_URL = "https://api.scrapegraphai.com/v2"
@@ -483,6 +485,15 @@ def test_explicit_api_key(self):
483485
sgai = ScrapeGraphAI(api_key="explicit-key")
484486
assert sgai._api_key == "explicit-key"
485487

488+
def test_sends_user_agent(self):
489+
sgai = ScrapeGraphAI(api_key=API_KEY)
490+
assert sgai._http.headers["User-Agent"] == USER_AGENT
491+
assert USER_AGENT.startswith("scrapegraph-py/")
492+
493+
def test_async_sends_user_agent(self):
494+
sgai = AsyncScrapeGraphAI(api_key=API_KEY)
495+
assert sgai._http.headers["User-Agent"] == USER_AGENT
496+
486497

487498
class TestCamelCaseSerialization:
488499
def test_snake_to_camel(self):

0 commit comments

Comments
 (0)