Skip to content

Commit 9902bc3

Browse files
shixishclaude
andcommitted
docs: document per-host bases and retry policy in the README
Updates the Client configuration table and adds examples for api_base/kg_base/ llm_base/nlp_base (pointing the whole SDK at a staging host with one option) and retry/RetryPolicy, matching the TypeScript SDK's README for the same Phase 0 work. Adds the matching tests per AGENTS.md. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent a605945 commit 9902bc3

2 files changed

Lines changed: 59 additions & 13 deletions

File tree

‎README.md‎

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,19 @@ place to configure the SDK.
7575
|----------|---------|---------|
7676
| `token` | — (required) | all |
7777
| `timeout` | `30.0` (seconds) | all |
78+
| `retry` | on, 3 attempts | all; `False` disables, or pass a `RetryPolicy` to tune it |
7879
| `transport` | httpx default | all |
79-
| `analyze_url` | `https://api.diffbot.com/v3` | `extract` |
80-
| `crawler_url` | `https://api.diffbot.com/v3/crawl` | `crawl`, `crawl_list_jobs`, `crawl_get_job`, `crawl_delete_job` |
81-
| `llm_url` | `https://llm.diffbot.com/rag/v1/chat/completions` | `ask`, `ask_json` |
82-
| `web_search_url` | `https://llm.diffbot.com/api/v1/web_search` | `web_search` |
83-
| `nlp_url` | `https://nl.diffbot.com/v1/` | `entities` |
84-
| `dql_url` | `https://kg.diffbot.com/kg/v3/dql` | `dql`, `dql_parallel` |
85-
| `ontology_url` | `https://kg.diffbot.com/kg/ontology` | `dql_fetch_ontology`, `dql_refresh_ontology` |
80+
| `api_base` | `https://api.diffbot.com` | `extract`, `crawl`, `crawl_list_jobs`, `crawl_get_job`, `crawl_delete_job` |
81+
| `kg_base` | `https://kg.diffbot.com` | `dql`, `dql_parallel`, `dql_fetch_ontology`, `dql_refresh_ontology` |
82+
| `llm_base` | `https://llm.diffbot.com` | `ask`, `ask_json`, `web_search` |
83+
| `nlp_base` | `https://nl.diffbot.com` | `entities` |
84+
| `analyze_url` | derived from `api_base` | `extract` |
85+
| `crawler_url` | derived from `api_base` | `crawl`, `crawl_list_jobs`, `crawl_get_job`, `crawl_delete_job` |
86+
| `llm_url` | derived from `llm_base` | `ask`, `ask_json` |
87+
| `web_search_url` | derived from `llm_base` | `web_search` |
88+
| `nlp_url` | derived from `nlp_base` | `entities` |
89+
| `dql_url` | derived from `kg_base` | `dql`, `dql_parallel` |
90+
| `ontology_url` | derived from `kg_base` | `dql_fetch_ontology`, `dql_refresh_ontology` |
8691

8792
```python
8893
from diffbot import Diffbot
@@ -94,12 +99,35 @@ db = Diffbot(
9499
)
95100
```
96101

97-
`analyze_url` is a base the SDK appends `/{api}` to, and `crawler_url` is used as given
98-
for job management and with `/data` appended for crawl results. The rest are complete
99-
endpoints, used as given.
102+
Point the whole SDK at a staging environment with one option instead of one per endpoint:
100103

101-
Passing `transport` replaces the httpx transport, which is the hook for retries, proxies,
102-
or mocking in tests. `Diffbot` takes an `httpx.BaseTransport`, `DiffbotAsync` an
104+
```python
105+
staging = Diffbot(token="YOUR_TOKEN", api_base="https://staging.example.com")
106+
```
107+
108+
`api_base`/`kg_base`/`llm_base`/`nlp_base` set the host for every endpoint on that host
109+
at once. The older per-endpoint options (`analyze_url`, `crawler_url`, `llm_url`,
110+
`web_search_url`, `nlp_url`, `dql_url`, `ontology_url`) still work and win over the
111+
matching base when both are set; `analyze_url` is a base the SDK appends `/{api}` to,
112+
`crawler_url` is used as given for job management and with `/data` appended for crawl
113+
results, and the rest are complete endpoints used as given.
114+
115+
Requests retry `429` and `5xx` responses (and connection errors) with backoff and
116+
jitter, honoring a `Retry-After` header, by default. Disable retries or tune the policy:
117+
118+
```python
119+
from diffbot import Diffbot, RetryPolicy
120+
121+
db = Diffbot(token="YOUR_TOKEN", retry=False)
122+
# or: retry=RetryPolicy(max_attempts=5)
123+
```
124+
125+
The transport also accepts a per-call `retryable=False` escape hatch; the SDK uses it
126+
internally for `crawl_delete_job`, a GET-shaped mutation that must not be retried
127+
blindly.
128+
129+
Passing `transport` replaces the httpx transport, which is the hook for proxies or
130+
mocking in tests. `Diffbot` takes an `httpx.BaseTransport`, `DiffbotAsync` an
103131
`httpx.AsyncBaseTransport`:
104132

105133
```python

‎tests/test_readme_examples.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import httpx
55
import pytest
66

7-
from diffbot import CrawlEventType, Diffbot, DiffbotAsync, json_schema_format, resolve_token
7+
from diffbot import CrawlEventType, Diffbot, DiffbotAsync, RetryPolicy, json_schema_format, resolve_token
88

99
SSE_PARIS = 'data: {"choices": [{"delta": {"content": "Paris"}}]}\n'
1010

@@ -48,6 +48,24 @@ def test_readme_client_configuration_transport():
4848
assert db._http._transport is transport
4949

5050

51+
def test_readme_client_configuration_api_base():
52+
staging = Diffbot(token="YOUR_TOKEN", api_base="https://staging.example.com")
53+
assert staging.analyze_url == "https://staging.example.com/v3"
54+
assert staging.crawler_url == "https://staging.example.com/v3/crawl"
55+
56+
57+
def test_readme_client_configuration_retry():
58+
def handler(request: httpx.Request) -> httpx.Response:
59+
return httpx.Response(500, text="boom")
60+
61+
db = Diffbot(token="YOUR_TOKEN", retry=False, transport=httpx.MockTransport(handler))
62+
with pytest.raises(Exception):
63+
db.extract("https://www.example.com")
64+
65+
tuned = Diffbot(token="YOUR_TOKEN", retry=RetryPolicy(max_attempts=5))
66+
assert tuned._retry_policy.max_attempts == 5
67+
68+
5169
# ---------------------------------------------------------------------------
5270
# Sync Usage
5371
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)