Skip to content

fix: close slug-resolution gap on create_post / join / leave#46

Merged
ColonistOne merged 3 commits intomainfrom
fix/colony-slug-resolution-create-paths
Apr 28, 2026
Merged

fix: close slug-resolution gap on create_post / join / leave#46
ColonistOne merged 3 commits intomainfrom
fix/colony-slug-resolution-create-paths

Conversation

@ColonistOne
Copy link
Copy Markdown
Collaborator

Summary

Closes the explicitly-out-of-scope loose end from PR #45.

PR #45 fixed the filter call sites (`get_posts`, `search_posts`) by routing unmapped slugs to the API's slug-friendly `?colony=` query param. The body / URL-path call sites couldn't use that workaround — the API only accepts a UUID for `body.colony_id` and `/colonies/{colony_id}/{join,leave}`. So calls like `client.create_post(colony="builds", ...)` still 422'd if `builds` wasn't in the hardcoded `COLONIES` map.

What changed

New `_resolve_colony_uuid(value)` on both `ColonyClient` and `AsyncColonyClient`:

  1. Known slug (in `COLONIES`) → canonical UUID.
  2. UUID-shaped value → passthrough.
  3. Unmapped slug → lazy `GET /colonies?limit=200`, cache the `slug → id` map on the client, look up the slug.
  4. Truly-unknown slug → `ValueError` with the slug name and a sample of available colonies. Distinguishes a typo from a transient API failure.

Cache is populated on first miss against `COLONIES` and never invalidated for the lifetime of the client — sub-communities are stable enough that this is safer than a TTL. A freshly-added colony just triggers one extra fetch on the first call that references it.

Wired into the three call sites:

File Method Was Now
`client.py` `create_post` `COLONIES.get(colony, colony)` `self._resolve_colony_uuid(colony)`
`client.py` `join_colony` same same
`client.py` `leave_colony` same same
`async_client.py` `create_post` same `await self._resolve_colony_uuid(colony)`
`async_client.py` `join_colony` same same
`async_client.py` `leave_colony` same same

Test plan

  • 7 new regression tests in `test_client.py::TestResolveColonyUuid`:
    • known slug → UUID without an API call
    • UUID passthrough → no API call
    • unmapped slug → fetches `/colonies` once, returns UUID
    • cache reused on subsequent calls (`list_colonies` called exactly once)
    • truly-unknown slug → `ValueError` with diagnostic message
    • dict-vs-list response-shape tolerance (`{"items": [...]}` envelope works)
    • async mirror (`AsyncColonyClient._resolve_colony_uuid`) exists and is distinct
  • Full suite passes locally: 423 tests in 4.60s.
  • `ruff check` clean.
  • `ruff format --check` clean.

Notes

  • I also probed the live `GET /api/v1/colonies` to confirm the response shape: array of objects, slug lives in the `name` field, the `slug` field is currently empty (reserved for a future display-name variant). The resolver tolerates both, preferring `name`.
  • This stacks cleanly on top of PR fix: route unmapped colony slugs through ?colony= instead of ?colony_id= #45 (just merged). The two PRs cover non-overlapping call sites — together they make the SDK fully slug-aware.

Re: versioning

Left `pyproject.toml` version untouched and added an "Unreleased" section in `CHANGELOG.md`. Whoever cuts the next release decides whether this lands as a bugfix patch (1.8.2) or rolls into the next minor.

PR #45 fixed the filter call sites (`get_posts`, `search_posts`) by
routing unmapped slugs to the API's slug-friendly `?colony=` query
param. The body/URL-path call sites couldn't use that workaround —
the API only accepts a UUID for `body.colony_id` and
`/colonies/{colony_id}/{join,leave}`.

This change adds `_resolve_colony_uuid(value)` to both `ColonyClient`
and `AsyncColonyClient`:

  1. Known slug (in `COLONIES`) -> canonical UUID.
  2. UUID-shaped value -> passthrough.
  3. Unmapped slug -> lazy `GET /colonies?limit=200`, cache the
     slug->id map on the client, look up the slug.
  4. Truly-unknown slug -> `ValueError` with sample of available
     names. Distinguishes a typo from a transient API failure.

Cache populated on first miss against `COLONIES`; never invalidated
for the lifetime of the client (sub-communities are stable). Each
client gets its own `_colony_uuid_cache` instance attribute.

7 new regression tests (`test_client.py::TestResolveColonyUuid`)
cover known-slug fast path, UUID passthrough, lazy lookup,
cache reuse, ValueError on truly-unknown, dict-vs-list response
shape tolerance, and async-mirror existence.

Closes the "out of scope" loose end from PR #45. With this landed,
the SDK is fully slug-aware across every call site that takes a
colony reference.

Full suite still passes: 423 tests in 4.60s.
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

_raw_request wraps non-dict JSON responses in {"data": parsed} (line
~620 in async_client.py). The /colonies endpoint returns a bare list,
so the resolver's response-shape sniffer never found the items —
it checked for {items: ...} / {colonies: ...} envelopes but missed
the actual {data: [...]} shape used here.

Adds {data: [...]} to the response-shape tolerance list, plus 6 new
async regression tests using httpx.MockTransport (mirror of the sync
TestResolveColonyUuid). Covers known-slug fast path, UUID passthrough,
lazy lookup via list_colonies, cache reuse, ValueError on unknown,
dict-envelope tolerance.

Coverage: 100% on both client.py and async_client.py (1187/1187 lines).
@ColonistOne ColonistOne merged commit 28a577e into main Apr 28, 2026
7 checks passed
@ColonistOne ColonistOne deleted the fix/colony-slug-resolution-create-paths branch April 28, 2026 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant