Skip to content

Commit d2558f5

Browse files
shixishclaude
andcommitted
feat(ontology): dql_fetch_ontology_text and dql_refresh_ontology_async
Closes the Python/TypeScript ontology asymmetry (PHASE0_LEFTOVERS §4). Raw JSON text fetch on both clients; async refresh on DiffbotAsync (dql_refresh_ontology) and in diffbot.kg (dql_refresh_ontology_async). dql_fetch_ontology reuses the text fetch. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 4a83898 commit d2558f5

5 files changed

Lines changed: 103 additions & 10 deletions

File tree

‎PHASE0_LEFTOVERS.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ The TypeScript SDK corrupted `xls`/`xlsx` exports by re-encoding text. Python's
2727
byte path (`response.content`) was already correct; no code change was needed.
2828
Unchanged by the move to POST. Covered by the existing raw-mode tests.
2929

30-
## 4. Ontology parity: scheduled for S2
30+
## 4. Ontology parity: landed in S2
3131

32-
Adding `dql_fetch_ontology_text()` and `dql_refresh_ontology_async()` to close the
33-
Python/TypeScript asymmetry is scheduled for **S2** (Python 3.4.0).
32+
`dql_fetch_ontology_text()` (sync + async) and `dql_refresh_ontology_async()` landed in
33+
**S2** (Python 3.4.0, branch `feat/sdk-s2`). `dql_fetch_ontology()` reuses the text
34+
fetch. On the `DiffbotAsync` client the refresh is named `dql_refresh_ontology` (the
35+
async client drops the `_async` suffix, as for `dql_fetch_ontology`); the module-level
36+
function in `diffbot.kg` is `dql_refresh_ontology_async`.
3437

3538
## 5. Response TypedDicts: landed in S1
3639

‎src/diffbot/client.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,12 @@
8888
dql_async as _dql_async,
8989
dql_fetch_ontology as _dql_fetch_ontology,
9090
dql_fetch_ontology_async as _dql_fetch_ontology_async,
91+
dql_fetch_ontology_text as _dql_fetch_ontology_text,
92+
dql_fetch_ontology_text_async as _dql_fetch_ontology_text_async,
9193
dql_parallel as _dql_parallel,
9294
dql_parallel_async as _dql_parallel_async,
9395
dql_refresh_ontology as _dql_refresh_ontology,
96+
dql_refresh_ontology_async as _dql_refresh_ontology_async,
9497
)
9598
from .ontology import Ontology
9699
from .web_search import (
@@ -425,6 +428,10 @@ def dql_fetch_ontology(self) -> Ontology:
425428
"""Download the ontology and return it as a queryable Ontology (no caching)."""
426429
return _dql_fetch_ontology(self)
427430

431+
def dql_fetch_ontology_text(self) -> str:
432+
"""Download the ontology and return the raw JSON text (no parsing, no caching)."""
433+
return _dql_fetch_ontology_text(self)
434+
428435
def web_search(
429436
self,
430437
text: Union[str, Sequence[str]],
@@ -790,6 +797,14 @@ async def dql_fetch_ontology(self) -> Ontology:
790797
"""Download the ontology and return it as a queryable Ontology (no caching)."""
791798
return await _dql_fetch_ontology_async(self)
792799

800+
async def dql_fetch_ontology_text(self) -> str:
801+
"""Download the ontology and return the raw JSON text (no parsing, no caching)."""
802+
return await _dql_fetch_ontology_text_async(self)
803+
804+
async def dql_refresh_ontology(self, dest: pathlib.Path) -> None:
805+
"""Download the Diffbot Knowledge Graph ontology and write it to dest."""
806+
await _dql_refresh_ontology_async(self, dest)
807+
793808
async def web_search(
794809
self,
795810
text: Union[str, Sequence[str]],

‎src/diffbot/kg.py‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,42 @@ async def _one(q: Dict[str, Any]) -> Any:
196196
return list(await asyncio.gather(*(_one(q) for q in queries)))
197197

198198

199+
def dql_fetch_ontology_text(client: "Diffbot") -> str:
200+
"""Download the ontology and return the raw JSON text, undecoded."""
201+
return client.request("GET", client.ontology_url).text
202+
203+
204+
async def dql_fetch_ontology_text_async(client: "DiffbotAsync") -> str:
205+
"""Async variant of :func:`dql_fetch_ontology_text`."""
206+
return (await client.request("GET", client.ontology_url)).text
207+
208+
209+
def _write_ontology(dest: pathlib.Path, content: bytes) -> None:
210+
dest.parent.mkdir(parents=True, exist_ok=True)
211+
dest.write_bytes(content)
212+
213+
199214
def dql_refresh_ontology(client: "Diffbot", dest: pathlib.Path) -> None:
200215
response = client.request("GET", client.ontology_url)
201-
dest.parent.mkdir(parents=True, exist_ok=True)
202-
dest.write_bytes(response.content)
216+
_write_ontology(dest, response.content)
217+
218+
219+
async def dql_refresh_ontology_async(client: "DiffbotAsync", dest: pathlib.Path) -> None:
220+
"""Async variant of :func:`dql_refresh_ontology`."""
221+
response = await client.request("GET", client.ontology_url)
222+
await asyncio.to_thread(_write_ontology, dest, response.content)
203223

204224

205225
def dql_fetch_ontology(client: "Diffbot") -> Ontology:
206226
"""Download the ontology and return it as a queryable :class:`Ontology`.
207227
208228
Performs no caching — the caller decides whether and where to hold onto the
209-
result. Use :func:`dql_refresh_ontology` instead to persist raw bytes to disk.
229+
result. Use :func:`dql_refresh_ontology` instead to persist raw bytes to disk,
230+
or :func:`dql_fetch_ontology_text` for the raw JSON text.
210231
"""
211-
response = client.request("GET", client.ontology_url)
212-
return Ontology.from_json(response.content)
232+
return Ontology.from_json(dql_fetch_ontology_text(client))
213233

214234

215235
async def dql_fetch_ontology_async(client: "DiffbotAsync") -> Ontology:
216236
"""Async variant of :func:`dql_fetch_ontology`."""
217-
response = await client.request("GET", client.ontology_url)
218-
return Ontology.from_json(response.content)
237+
return Ontology.from_json(await dql_fetch_ontology_text_async(client))

‎tests/test_ontology.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,52 @@ def handler(request: httpx.Request) -> httpx.Response:
117117
]
118118
)
119119
assert [r["hits"] for r in results] == [5, 100]
120+
121+
122+
"""
123+
Raw text fetch and async refresh (S2)
124+
"""
125+
126+
RAW = json.dumps(FIXTURE_ONTOLOGY)
127+
128+
129+
def _raw_handler(request: httpx.Request) -> httpx.Response:
130+
assert request.method == "GET"
131+
assert request.url.path == "/kg/ontology"
132+
return httpx.Response(200, text=RAW)
133+
134+
135+
def test_dql_fetch_ontology_text_returns_raw_json_text():
136+
db = Diffbot(token="t", transport=httpx.MockTransport(_raw_handler))
137+
text = db.dql_fetch_ontology_text()
138+
assert text == RAW
139+
assert json.loads(text) == FIXTURE_ONTOLOGY
140+
# the parsed variant still works on the same payload
141+
assert db.dql_fetch_ontology().types() == ["Organization", "Person"]
142+
143+
144+
@pytest.mark.anyio
145+
async def test_async_dql_fetch_ontology_text_and_parsed():
146+
db = DiffbotAsync(token="t", transport=httpx.MockTransport(_raw_handler))
147+
assert await db.dql_fetch_ontology_text() == RAW
148+
assert (await db.dql_fetch_ontology()).types() == ["Organization", "Person"]
149+
150+
151+
def test_dql_refresh_ontology_writes_bytes(tmp_path):
152+
db = Diffbot(token="t", transport=httpx.MockTransport(_raw_handler))
153+
dest = tmp_path / "nested" / "ontology.json"
154+
db.dql_refresh_ontology(dest)
155+
assert json.loads(dest.read_text()) == FIXTURE_ONTOLOGY
156+
157+
158+
@pytest.mark.anyio
159+
async def test_async_dql_refresh_ontology_writes_bytes(tmp_path):
160+
db = DiffbotAsync(token="t", transport=httpx.MockTransport(_raw_handler))
161+
dest = tmp_path / "nested" / "ontology.json"
162+
await db.dql_refresh_ontology(dest)
163+
assert json.loads(dest.read_text()) == FIXTURE_ONTOLOGY
164+
from diffbot.kg import dql_refresh_ontology_async
165+
166+
other = tmp_path / "b.json"
167+
await dql_refresh_ontology_async(db, other)
168+
assert other.read_bytes() == dest.read_bytes()

‎tests/typing/test_types.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
that the calls themselves work against a mock transport.
55
"""
66
import json
7+
import pathlib
78
from typing import TYPE_CHECKING, Any, AsyncIterator, Dict, Iterator, List, cast
89

910
import httpx
@@ -133,3 +134,9 @@ def _nlp_types() -> None:
133134

134135
async def _nlp_async_types() -> None:
135136
assert_type(await adb.nlp("x", format="plain text with title"), NlpResponse)
137+
138+
139+
async def _ontology_text_types() -> None:
140+
assert_type(db.dql_fetch_ontology_text(), str)
141+
assert_type(await adb.dql_fetch_ontology_text(), str)
142+
assert_type(await adb.dql_refresh_ontology(pathlib.Path("o.json")), None)

0 commit comments

Comments
 (0)