Skip to content

Commit 6a351f3

Browse files
committed
feat: add integration for env variables
1 parent d6e7f40 commit 6a351f3

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

scrapegraph-py/scrapegraph_py/async_client.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,34 @@ def __init__(
5454

5555
logger.info("✅ AsyncClient initialized successfully")
5656

57+
@classmethod
58+
def from_env(
59+
cls,
60+
verify_ssl: bool = True,
61+
timeout: float = 120,
62+
max_retries: int = 3,
63+
retry_delay: float = 1.0,
64+
):
65+
"""Initialize AsyncClient using API key from environment variable.
66+
67+
Args:
68+
verify_ssl: Whether to verify SSL certificates
69+
timeout: Request timeout in seconds
70+
max_retries: Maximum number of retry attempts
71+
retry_delay: Delay between retries in seconds
72+
"""
73+
from os import getenv
74+
api_key = getenv("SGAI_API_KEY")
75+
if not api_key:
76+
raise ValueError("SGAI_API_KEY environment variable not set")
77+
return cls(
78+
api_key=api_key,
79+
verify_ssl=verify_ssl,
80+
timeout=timeout,
81+
max_retries=max_retries,
82+
retry_delay=retry_delay,
83+
)
84+
5785
async def _make_request(self, method: str, url: str, **kwargs) -> Any:
5886
"""Make HTTP request with retry logic."""
5987
for attempt in range(self.max_retries):

scrapegraph-py/scrapegraph_py/client.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,34 @@
1818

1919

2020
class SyncClient:
21+
@classmethod
22+
def from_env(
23+
cls,
24+
verify_ssl: bool = True,
25+
timeout: float = 30,
26+
max_retries: int = 3,
27+
retry_delay: float = 1.0,
28+
):
29+
"""Initialize SyncClient using API key from environment variable.
30+
31+
Args:
32+
verify_ssl: Whether to verify SSL certificates
33+
timeout: Request timeout in seconds
34+
max_retries: Maximum number of retry attempts
35+
retry_delay: Delay between retries in seconds
36+
"""
37+
from os import getenv
38+
api_key = getenv("SGAI_API_KEY")
39+
if not api_key:
40+
raise ValueError("SGAI_API_KEY environment variable not set")
41+
return cls(
42+
api_key=api_key,
43+
verify_ssl=verify_ssl,
44+
timeout=timeout,
45+
max_retries=max_retries,
46+
retry_delay=retry_delay,
47+
)
48+
2149
def __init__(
2250
self,
2351
api_key: str,

0 commit comments

Comments
 (0)