Skip to content

Commit f5b1de8

Browse files
authored
Merge pull request #19 from FoxNoseTech/feat/truncate-text-and-cross-parent-reads
Feat/truncate text and cross parent reads
2 parents fd36a02 + de21427 commit f5b1de8

17 files changed

Lines changed: 1368 additions & 44 deletions

‎.githooks/pre-push‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/sh
2+
#
3+
# Runs the checks CI gates on, before the push leaves your machine.
4+
#
5+
# Enable once per clone:
6+
# git config core.hooksPath .githooks
7+
#
8+
# Bypass for a genuine emergency:
9+
# git push --no-verify
10+
#
11+
# This hook is a fast feedback loop, not a security boundary — it is opt-in per
12+
# clone and skippable. CI remains the real gate.
13+
14+
set -e
15+
16+
fail() {
17+
echo ""
18+
echo "pre-push: $1 failed."
19+
echo "pre-push: fix with $2"
20+
echo "pre-push: or skip with git push --no-verify"
21+
exit 1
22+
}
23+
24+
if ! command -v ruff >/dev/null 2>&1; then
25+
echo "pre-push: ruff not found on PATH — skipping lint and format checks."
26+
echo "pre-push: install it with pip install ruff==0.13.2"
27+
else
28+
echo "pre-push: ruff check ."
29+
ruff check . || fail "ruff check" "ruff check --fix ."
30+
31+
echo "pre-push: ruff format --check ."
32+
ruff format --check . || fail "ruff format" "ruff format ."
33+
fi
34+
35+
echo "pre-push: pytest"
36+
pytest -q || fail "pytest" "pytest -q"
37+
38+
echo "pre-push: ok"

‎.github/workflows/ci.yml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ jobs:
5555
# with no code change. Bump deliberately alongside a repo-wide reformat.
5656
run: pip install ruff==0.13.2
5757

58+
# Gates the whole repo, not just src/. Linting src/ alone let tests/ drift
59+
# unchecked until it needed a repo-wide reformat to get back in line.
5860
- name: Run ruff check
59-
run: ruff check src/
61+
run: ruff check .
6062

6163
- name: Run ruff format check
62-
run: ruff format --check src/
64+
run: ruff format --check .

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,5 @@ site/
8686
# OS
8787
.DS_Store
8888
Thumbs.db
89+
90+
.claude/

‎CONTRIBUTING.md‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ pip install -e ".[test]"
3838
4. Install linting tools:
3939

4040
```bash
41-
pip install ruff
41+
pip install ruff==0.13.2
4242
```
4343

44+
Pin the version. CI pins it too: an unpinned `ruff` resolves to the latest release, so a new version with changed default rules can turn a green repo red with no code change.
45+
4446
## Development Workflow
4547

4648
### Code Style
@@ -55,6 +57,18 @@ ruff check .
5557
ruff format .
5658
```
5759

60+
Both commands cover the whole repo, including `tests/` — that is what CI gates on.
61+
62+
### Checks before pushing
63+
64+
To run the same checks locally on every `git push`, enable the repo's hook directory once per clone:
65+
66+
```bash
67+
git config core.hooksPath .githooks
68+
```
69+
70+
`.githooks/pre-push` then runs `ruff check`, `ruff format --check` and `pytest`, and aborts the push if any fail. It is opt-in per clone and can be skipped with `git push --no-verify`, so it is a fast feedback loop rather than a guarantee — CI remains the real gate.
71+
5872
### Type Hints
5973

6074
All public APIs should include type hints. We use Python's built-in `typing` module and Pydantic for model definitions.

‎docs/flux-client.md‎

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ for item in results["results"]:
8989
print(item["data"]["title"])
9090
```
9191

92+
`search()` also accepts a keyword-only `params` mapping, forwarded to the
93+
query string (not the body) — e.g. `params={"truncate_text": 200}`. See
94+
[Truncating Text Fields](#truncating-text-fields) below.
95+
9296
## Writing Resources
9397

9498
Writes require a write-capable key. Creating and updating publish immediately;
@@ -212,17 +216,109 @@ resources = client.list_resources(
212216

213217
### Pagination
214218

219+
`next` in the response is a full absolute URL (e.g.
220+
`https://<env>.fxns.io/<prefix>/posts?limit=10&next=Xk6KmhmHZuAU`), not a bare
221+
cursor. The SDK does not follow it automatically — `list_resources` always
222+
targets the configured `base_url`/`api_prefix`, so a server-returned absolute
223+
URL can't be fed back in directly. Extract the `next` query parameter and
224+
re-pass it (along with any other parameters you want to keep, such as
225+
`truncate_text`) instead:
226+
215227
```python
228+
from urllib.parse import urlparse, parse_qs
229+
216230
# First page
217-
page1 = client.list_resources("posts", params={"limit": 10})
231+
page1 = client.list_resources("posts", params={"limit": 10, "truncate_text": 200})
218232

219-
# Next page (use the cursor from the previous response)
233+
# Next page: pull the cursor out of the absolute `next` URL and re-pass it
234+
# alongside the parameters you want to carry forward.
220235
if page1["next"]:
221-
page2 = client.list_resources("posts", params={"limit": 10, "next": "<cursor>"})
236+
cursor = parse_qs(urlparse(page1["next"]).query)["next"][0]
237+
page2 = client.list_resources(
238+
"posts",
239+
params={"limit": 10, "truncate_text": 200, "next": cursor},
240+
)
222241

223242
print(f"Got {len(page1['results'])} items")
224243
```
225244

245+
If you ever build a helper that follows `next` automatically, validate that
246+
the URL is same-origin with your configured `base_url` *before* attaching any
247+
auth header — blindly following a server-supplied absolute URL with
248+
credentials attached is an SSRF and credential-leak vector.
249+
250+
### Truncating Text Fields
251+
252+
Pass `truncate_text` (an integer ≥ 1) on List Resources or Search to cap
253+
every `text`-typed field in the response. It has no effect when `raw=true`.
254+
255+
```python
256+
resources = client.list_resources("blog-posts", params={"truncate_text": 200})
257+
results = client.search(
258+
"blog-posts",
259+
body={"find_text": {"query": "python"}},
260+
params={"truncate_text": 200},
261+
)
262+
```
263+
264+
Truncated fields are marked under `_sys.truncated`:
265+
266+
```json
267+
{
268+
"_sys": {
269+
"key": "Cpa3KebZoqb3",
270+
"truncated": [{"field": "body", "locale": null, "original_length": 210}]
271+
}
272+
}
273+
```
274+
275+
`locale` is `null` for non-localized fields; fields within the limit get no
276+
entry. The SDK does not validate `truncate_text` client-side — there is no
277+
typed request model for query parameters to hang a validator on. An invalid
278+
value (non-integer, or < 1) surfaces as a server `422 validation_error`.
279+
280+
The same parameter works with the typed search wrappers — `vector_search()`,
281+
`vector_field_search()`, `hybrid_search()`, `boosted_search()` — via
282+
`query_params`, e.g. `client.vector_search("blog-posts", query="ml",
283+
query_params={"truncate_text": 200})`. Use `query_params`, not `params`: the
284+
wrappers already collect unrecognized keyword arguments (like `where` and
285+
`sort`) into the JSON body, and `params` is a plausible body field name.
286+
Passing `truncate_text` as a body field raises a clear error naming
287+
`query_params` as the fix.
288+
289+
## Cross-Parent Addressing
290+
291+
A strict-reference collection nested under one or more parents can also be
292+
configured (via the Management API, see
293+
[API Folder Route Descriptions](management-client.md#api-folder-route-descriptions))
294+
to expose additional, read-only addresses that drop some or all of the
295+
ancestor keys from the path:
296+
297+
```python
298+
# Normal, fully-nested address:
299+
client.list_resources("realty/accounts/acc_1/listings/lst_1/photos")
300+
301+
# Fully-flat (level 0): every ancestor key is dropped.
302+
client.list_resources("realty/accounts/listings/photos")
303+
304+
# Partially-flat (level >= 1): the root-most ancestor key(s) are retained.
305+
client.list_resources("realty/accounts/acc_1/listings/photos")
306+
```
307+
308+
This works today for List Resources, Get Resource, and Schema — the folder
309+
path is an opaque, slash-trimmed string with no segment parsing or ancestor
310+
validation on the client side. These addresses are read-only; the server
311+
rejects writes on a flat path.
312+
313+
Not every configured level necessarily serves every read method. Before
314+
relying on a flat address, check `enabled`, `available`, and `read_methods`
315+
on the connection's `flat_routes` (see
316+
[API Folder Route Descriptions](management-client.md#api-folder-route-descriptions))
317+
rather than assuming every read method is available at every level — the
318+
docs and the live API disagree on this in at least two ways that are still
319+
open with the API team (Get Resource at level 0; Search never appears in
320+
`read_methods` at any level).
321+
226322
## Error Handling
227323

228324
```python

‎docs/management-client.md‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,58 @@ updated = client.update_api_folder(
133133
)
134134
```
135135

136+
### Cross-Parent (Flat) Read Addressing
137+
138+
For a strict-reference collection nested under one or more parents, pass
139+
`unscoped_levels` and `unscoped_ancestors` to expose additional, read-only
140+
addresses that drop some or all of the ancestor keys from the Flux path:
141+
142+
```python
143+
connection = client.add_api_collection(
144+
api_key="api-key",
145+
collection_key="photos",
146+
unscoped_levels=[0], # 0 = fully flat; 1+ retains that many root ancestors
147+
unscoped_ancestors=[
148+
"01debe0d-0325-42b1-9bfd-ef52046cd785", # accounts connection (UUID)
149+
"432880c9-ae43-4462-b4f3-f16c18068ea5", # listings connection (UUID)
150+
],
151+
)
152+
```
153+
154+
The two must be sent together — the API rejects a level set with an empty
155+
ancestor chain. This is not validated client-side; the server owns the rule
156+
and may relax it.
157+
158+
`unscoped_ancestors` and the `omitted_ancestors` / `retained_ancestors`
159+
entries below are connection UUIDs, **not** the short collection keys used
160+
elsewhere (e.g. `folder`). Do not conflate the two.
161+
162+
The connection object returned by `add_api_collection`, `update_api_collection`,
163+
`get_api_collection`, and `list_api_collections` carries these fields,
164+
**read-only** — configure the addresses via `unscoped_levels` /
165+
`unscoped_ancestors` above, not by constructing `flat_routes` yourself:
166+
167+
- `unscoped_levels: list[int]`, `unscoped_ancestors: list[str]` — echo what
168+
was configured.
169+
- `expose_owner: bool` — present on the wire; semantics are unconfirmed, so
170+
the SDK does not expose a way to set it.
171+
- `flat_routes: list[FlatRoute] | None` — one entry per configured level,
172+
each with `level`, `path`, `omitted_ancestors`, `retained_ancestors`,
173+
`enabled`, `read_methods`, `available`, `unavailable_reason`,
174+
`published_generation`, and `router_generation`. `None` when the
175+
connection has no key-bearing ancestor to flatten (not `[]`).
176+
- `flat_route: FlatRouteSummary | None` — a single, undocumented field that
177+
appears to mirror whichever `flat_routes` entry is enabled. No behavior is
178+
built on it in the SDK.
179+
180+
Before relying on a flat address, check `enabled`, `available`, and
181+
`read_methods` per entry — `read_methods` is typed exactly as the server
182+
sends it, including two known discrepancies from the docs (raised with the
183+
API team, not worked around here): Get Resource appears in `read_methods` at
184+
level 0 even though the docs say it needs `level >= 1`, and `search` never
185+
appears in `read_methods` at any level even though the docs list Search as
186+
available on flat addresses.
187+
136188
## Folder Operations
137189

138190
### List Folders

‎src/foxnose_sdk/__init__.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
EnvironmentSummary,
7171
FieldList,
7272
FieldSummary,
73+
FlatRoute,
74+
FlatRouteSummary,
7375
FolderList,
7476
FolderSummary,
7577
FluxAPIKeyList,
@@ -157,6 +159,8 @@
157159
"SchemaVersionList",
158160
"FieldSummary",
159161
"FieldList",
162+
"FlatRoute",
163+
"FlatRouteSummary",
160164
"ProjectSummary",
161165
"ProjectList",
162166
"EnvironmentSummary",

0 commit comments

Comments
 (0)