Skip to content

asn-risk: add /api/asn-risk and /api/asn/{asn}/risk to the client and CLI - #12

Merged
foospidy merged 1 commit into
masterfrom
feature/340-asn-risk-endpoints
Aug 30, 2026
Merged

foospidy merged 1 commit into
masterfrom
feature/340-asn-risk-endpoints

Conversation

@foospidy

Copy link
Copy Markdown
Collaborator

Closes honeydbio/honeydb-web#340.

The two ASN risk endpoints shipped in honeydb-web#332 are documented in honeydb-api-docs but were absent from this library, so Python consumers had to bypass it entirely — re-implementing the auth headers, the retry policy and the error mapping — to reach a flagship dataset.

Client

  • asn_risk(*, period=None, limit=None, scanners=None) — the monthly ranked report.
  • asn_risk_history(as_number) — one ASN's latest row plus six months of history.
  • ASN_RISK_SCANNERS — the accepted scanners values, exported from all three public surfaces.

Both pass the response body through untouched. Reports come in two generations (methodology.version 1.0 and 2.0) with different row fields; normalizing them here would create a second source of truth for scoring semantics that already live in the API and in honeydb-ops.

CLI

honeydb asn-risk --limit 25 --pretty
honeydb asn-risk --period 2026-07 --limit 25 --scanners exclude
honeydb asn 15169 --risk

--prefixes and --risk are now mutually exclusive members of one view group, mirroring ip --geo|--netinfo|…. --prefixes behaviour is unchanged and its existing test passes unmodified.

Validation

period / limit / scanners are checked client-side against the API's own validator regexes, so a malformed value raises ValueError before any request goes out. Three details worth a reviewer's eye:

  • --limit must accept digit strings, not just int. The parser types it as a string so all stays valid and forwards it unconverted; an int-only validator would make honeydb asn-risk --limit 25 — the most likely invocation of the feature — raise before issuing a request.
  • limit=0 is rejected rather than forwarded. The API accepts it and silently floors it to 1; returning one row to a caller who asked for none would mislead them.
  • bool is excluded explicitly, since it subclasses int and limit=True would otherwise become limit=1.

main() now catches ValueError alongside HoneyDBError, so client-side validation exits 1 with a message instead of a traceback. That also fixes monitors create --json '{bad' (JSONDecodeError subclasses ValueError); there is a test pinning it as intended.

Two hunks that look unrelated — both deliberate

  • README.md's create_monitors example is reformatted. ruff was unpinned, and current ruff formats Python blocks inside Markdown, so ruff format --check . — which CI runs on every PR — already failed on a clean master. The last green run was 2026-07-07 on an older ruff. ruff is now pinned to >=0.16,<0.17 and the tree is actually clean, so this PR's CI is green for its own reasons.
  • ASN_RISK_SCANNERS is added to three __all__ lists, not one. honeydb.api is a documented public surface; a test now covers all three.

Version bumped to 2.1.0 (additive, backward compatible). Not published — make build && make publish stays manual.

Verification

ruff format --check .   ->  9 files already formatted
ruff check .            ->  All checks passed!
pytest                  ->  83 passed

46 pre-existing tests untouched — both test files have zero deleted lines.

Beyond the mocked suite, verified end-to-end against a live stub HTTP server: query-string construction for every parameter combination, the 200 [] no-data contract on both endpoints, the HTML-404 path for a non-numeric ASN, every validation rejection sending no request, and CLI dispatch including the mutual-exclusion rejection. 21/21 passed.

Design docs: design/asn-risk-python-client-{prd,techspec}.md in honeydb-web (PR #342).

🤖 Generated with Claude Code

https://claude.ai/code/session_01WybGWSMuPJEuRG4Pz9EfWN

… CLI (#340)

The two ASN risk endpoints shipped in honeydb-web#332 were documented in
honeydb-api-docs but absent here, so Python consumers had to bypass the
library -- re-implementing the auth headers, the retry policy and the
error mapping -- to reach a flagship dataset.

Client:
  asn_risk(*, period=None, limit=None, scanners=None) -> the monthly
  ranked report; asn_risk_history(as_number) -> one ASN's latest row plus
  six months of history. Both pass the body through untouched: the two
  report generations (methodology.version 1.0 and 2.0) carry different
  fields, and normalizing them here would create a second source of truth
  for scoring semantics that already live in the API and honeydb-ops.

CLI:
  `honeydb asn-risk [--period] [--limit] [--scanners]`, and `--risk` on
  the existing `asn` command. --prefixes and --risk are now mutually
  exclusive members of one view group, mirroring `ip --geo|--netinfo|...`;
  --prefixes behaviour is unchanged and its existing test passes
  unmodified.

period/limit/scanners are validated client-side against the API's own
validator regexes, so a malformed value raises ValueError before any
request goes out. Note --limit must accept digit *strings*, not just int:
the parser types it as a string so that "all" stays valid, and forwards
it unconverted. limit=0 is rejected rather than passed on -- the API
floors it to 1, and returning one row to a caller who asked for none
would mislead them. bool is excluded explicitly, since it subclasses int
and limit=True would otherwise become limit=1.

main() now catches ValueError alongside HoneyDBError so client-side
validation exits 1 with a message instead of a traceback. This also fixes
`monitors create --json '{bad'`, since JSONDecodeError subclasses
ValueError; there is a test pinning that.

Two unrelated-looking hunks, both deliberate:

  - README.md's create_monitors example is reformatted. `ruff` was
    unpinned and current ruff formats Python blocks inside Markdown, so
    `ruff format --check .` -- which CI runs on every PR -- already failed
    on a clean master. ruff is now pinned to >=0.16,<0.17 and the tree is
    actually clean.
  - ASN_RISK_SCANNERS is added to three __all__ lists, not one;
    honeydb.api is a documented public surface.

Version bumped to 2.1.0 (additive, backward compatible). Not published.

83 tests pass (46 pre-existing, untouched -- both test files have zero
deleted lines), ruff check and ruff format --check clean. Verified
end-to-end against a live stub server, not only mocks: query-string
construction, the 200 [] no-data contract, the HTML-404 path, every
validation rejection sending no request, and the CLI dispatch.

Design: honeydb-web design/asn-risk-python-client-{prd,techspec}.md

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01WybGWSMuPJEuRG4Pz9EfWN
@foospidy
foospidy merged commit 945a744 into master Aug 30, 2026
4 checks passed
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