Skip to content

Commit 8c59f38

Browse files
refactor: return Pydantic objects instead of raw dicts
- Add ResponseModel base class with camelCase alias generator - Change all response models to inherit from ResponseModel - Use TypeAdapter for proper generic type parsing - Update all examples to use attribute access (res.data.results) - Fix all test mocks with complete required fields This follows industry standard SDK patterns where typed objects are returned for IDE autocompletion and type safety. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b190d03 commit 8c59f38

26 files changed

Lines changed: 177 additions & 135 deletions

examples/crawl/crawl_basic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
if start_res.status != "success" or not start_res.data:
1212
print("Failed to start:", start_res.error)
1313
else:
14-
print("Crawl started:", start_res.data["id"])
15-
print("Status:", start_res.data["status"])
14+
print("Crawl started:", start_res.data.id)
15+
print("Status:", start_res.data.status)
1616

17-
get_res = sgai.crawl.get(start_res.data["id"])
17+
get_res = sgai.crawl.get(start_res.data.id)
1818
if get_res.status == "success":
19-
print("\nProgress:", get_res.data["finished"], "/", get_res.data["total"])
19+
print("\nProgress:", get_res.data.finished, "/", get_res.data.total)
2020
print("Pages:", [p["url"] for p in get_res.data.get("pages", [])])

examples/crawl/crawl_basic_async.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ async def main():
1212
if start_res.status != "success" or not start_res.data:
1313
print("Failed to start:", start_res.error)
1414
else:
15-
print("Crawl started:", start_res.data["id"])
16-
print("Status:", start_res.data["status"])
15+
print("Crawl started:", start_res.data.id)
16+
print("Status:", start_res.data.status)
1717

18-
get_res = await sgai.crawl.get(start_res.data["id"])
18+
get_res = await sgai.crawl.get(start_res.data.id)
1919
if get_res.status == "success":
20-
print("\nProgress:", get_res.data["finished"], "/", get_res.data["total"])
20+
print("\nProgress:", get_res.data.finished, "/", get_res.data.total)
2121
print("Pages:", [p["url"] for p in get_res.data.get("pages", [])])
2222

2323
asyncio.run(main())

examples/crawl/crawl_with_formats.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
if start_res.status != "success" or not start_res.data:
2121
print("Failed to start:", start_res.error)
2222
else:
23-
crawl_id = start_res.data["id"]
23+
crawl_id = start_res.data.id
2424
print("Crawl started:", crawl_id)
25-
print("Status:", start_res.data["status"])
25+
print("Status:", start_res.data.status)
2626

2727
get_res = sgai.crawl.get(crawl_id)
2828
if get_res.status == "success":
29-
print("\nProgress:", get_res.data["finished"], "/", get_res.data["total"])
29+
print("\nProgress:", get_res.data.finished, "/", get_res.data.total)
3030

3131
for page in get_res.data.get("pages", []):
3232
print(f"\n Page: {page['url']}")

examples/crawl/crawl_with_formats_async.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ async def main():
2121
if start_res.status != "success" or not start_res.data:
2222
print("Failed to start:", start_res.error)
2323
else:
24-
crawl_id = start_res.data["id"]
24+
crawl_id = start_res.data.id
2525
print("Crawl started:", crawl_id)
26-
print("Status:", start_res.data["status"])
26+
print("Status:", start_res.data.status)
2727

2828
get_res = await sgai.crawl.get(crawl_id)
2929
if get_res.status == "success":
30-
print("\nProgress:", get_res.data["finished"], "/", get_res.data["total"])
30+
print("\nProgress:", get_res.data.finished, "/", get_res.data.total)
3131

3232
for page in get_res.data.get("pages", []):
3333
print(f"\n Page: {page['url']}")

examples/monitor/monitor_basic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
))
1111

1212
if res.status == "success":
13-
print("Monitor created:", res.data["cronId"])
14-
print("Status:", res.data["status"])
15-
print("Interval:", res.data["interval"])
13+
print("Monitor created:", res.data.cron_id)
14+
print("Status:", res.data.status)
15+
print("Interval:", res.data.interval)
1616
else:
1717
print("Failed:", res.error)

examples/monitor/monitor_basic_async.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ async def main():
1111
))
1212

1313
if res.status == "success":
14-
print("Monitor created:", res.data["cronId"])
15-
print("Status:", res.data["status"])
16-
print("Interval:", res.data["interval"])
14+
print("Monitor created:", res.data.cron_id)
15+
print("Status:", res.data.status)
16+
print("Interval:", res.data.interval)
1717
else:
1818
print("Failed:", res.error)
1919

examples/monitor/monitor_with_webhook.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
))
1212

1313
if res.status == "success":
14-
print("Monitor created:", res.data["cronId"])
15-
print("Status:", res.data["status"])
16-
print("Interval:", res.data["interval"])
14+
print("Monitor created:", res.data.cron_id)
15+
print("Status:", res.data.status)
16+
print("Interval:", res.data.interval)
1717
print("Webhook configured")
1818
else:
1919
print("Failed:", res.error)

examples/monitor/monitor_with_webhook_async.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ async def main():
1212
))
1313

1414
if res.status == "success":
15-
print("Monitor created:", res.data["cronId"])
16-
print("Status:", res.data["status"])
17-
print("Interval:", res.data["interval"])
15+
print("Monitor created:", res.data.cron_id)
16+
print("Status:", res.data.status)
17+
print("Interval:", res.data.interval)
1818
print("Webhook configured")
1919
else:
2020
print("Failed:", res.error)

examples/scrape/scrape_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
))
99

1010
if res.status == "success":
11-
print("Markdown:", res.data["results"].get("markdown", {}).get("data"))
11+
print("Markdown:", res.data.results.get("markdown", {}).get("data"))
1212
print(f"\nTook {res.elapsed_ms}ms")
1313
else:
1414
print("Failed:", res.error)

examples/scrape/scrape_basic_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async def main():
99
))
1010

1111
if res.status == "success":
12-
print("Markdown:", res.data["results"].get("markdown", {}).get("data"))
12+
print("Markdown:", res.data.results.get("markdown", {}).get("data"))
1313
print(f"\nTook {res.elapsed_ms}ms")
1414
else:
1515
print("Failed:", res.error)

0 commit comments

Comments
 (0)