feat: support per-request API key for runtime inspection clients - #107
Draft
mevinmathew23 wants to merge 1 commit into
Draft
feat: support per-request API key for runtime inspection clients#107mevinmathew23 wants to merge 1 commit into
mevinmathew23 wants to merge 1 commit into
Conversation
Allow callers to override the API key on individual inspection calls via an optional api_key argument, and to construct a client without a key for purely per-request usage. The key is an HTTP header stamped per request, so this is a natural fit; previously the key was pinned at client construction. Auth is now resolved per call: a per-request key takes precedence over the construction-time key, falling back to it when omitted, and raising a clear ValidationError when neither is available. Fully backwards compatible. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds an optional
api_keyargument to every runtime inspection method (Chat sync/async, HTTP, and MCP clients), allowing callers to override the API key on a per-request basis. Clients can also now be constructed without a key for purely per-request usage.Why
The AI Defense API authenticates each call with an HTTP header (
X-Cisco-AI-Defense-API-Key) stamped onto every request — the key is inherently per-request and not bound to a connection or session. The SDK, however, pinned the key at client construction (self.auth), forcing one client (and one connection pool) per key.This is limiting for multi-tenant services and key-rotation scenarios, where a single shared client should be able to authenticate different calls with different keys without rebuilding the client or its connection pool each time. Per-request auth override is a well-established pattern (
requests/httpxper-callheaders=/auth=, Stripe's per-callapi_key=).How
Auth is now resolved per call via a small
_resolve_auth(api_key)helper on the base inspection clients:api_keytakes precedence over the construction-time key.ValidationErrorat call time.The transport layer already accepted per-call
auth, so no request-handler or auth-class changes were needed.Backwards compatibility
Fully backwards compatible — all new parameters default to
Noneand existing call sites are unaffected.Tests
Adds
tests/test_per_request_api_key.pycovering per-call override, fallback, no-key construction, the no-key-anywhere error, and invalid-key validation, parameterized across the sync chat/HTTP/MCP clients plus the async chat client. Full existing suite remains green.🤖 Generated with Claude Code