Skip to content

feat(audit): add internal audit API client#455

Merged
ngjunsiang merged 3 commits intoweeklyfrom
feature/audit-phase4-middleware
Apr 10, 2026
Merged

feat(audit): add internal audit API client#455
ngjunsiang merged 3 commits intoweeklyfrom
feature/audit-phase4-middleware

Conversation

@nycomp
Copy link
Copy Markdown
Contributor

@nycomp nycomp commented Apr 10, 2026

Overview

Adds an internal HTTP client for the Campus audit service that follows Campus API schema conventions. This client enables campus.auth and campus.api deployments to send trace spans to campus.audit via HTTP.

Changes

Client Implementation

  • campus/audit/client/__init__.py - AuditClient entry point with environment-based URL resolution
  • campus/audit/client/interface.py - ResourceRoot, ResourceCollection, Resource base classes
  • campus/audit/client/v1/__init__.py - v1 API module
  • campus/audit/client/v1/root.py - AuditRoot resource
  • campus/audit/client/v1/traces.py - Traces, Trace, Spans, Span resources with nested class structure

Unit Tests

  • tests/unit/audit/test_client.py - Client initialization tests
  • tests/unit/audit/test_interface.py - Base resource class tests
  • tests/unit/audit/test_traces.py - Traces resource tests

API Usage

from campus.audit.client import AuditClient

client = AuditClient()

# Ingest spans
client.traces.new(span_data)           # Single
client.traces.new(s1, s2, s3)          # Batch

# List/search traces
client.traces.list(limit=10)
client.traces.search(path="/api/v1/users")

# Get trace tree
tree = client.traces[trace_id].get_tree()

# Get span
span = client.traces[trace_id].spans[span_id].get()

Design

Follows campus-api-python structure with:

  • ResourceCollection for collections
  • Nested classes for individual items
  • Standard verbs: new(), list(), search(), get()
  • Bracket access for IDs: client.traces[trace_id]
  • Property access for named sub-resources: client.traces[trace_id].spans

Related Issues

ngjunsiang and others added 3 commits April 10, 2026 12:57
Add comprehensive unit tests for the audit HTTP client implementation:
- test_interface.py: Tests for base resource classes (ResourceRoot, ResourceCollection, Resource)
- test_client.py: Tests for AuditClient initialization
- test_traces.py: Tests for Traces resource methods and nested classes

These tests verify:
- Path construction with proper URL prefixes
- Client propagation from parent to child resources
- Bracket access patterns for ID-based resources
- Property access for named sub-resources
- Standard verb methods (new, list, search, get)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Fix AttributeError in Trace, Span, and Spans classes by calling super().__init__()
to properly set self.parent and self.path attributes. Also fix make_path() in
ResourceCollection to avoid path duplication when constructing URLs.

Changes:
- Trace.__init__(): Call super().__init__() instead of manually setting path
- Span.__init__(): Call super().__init__() instead of manually setting path
- Spans.make_path(): Override to properly nest under Trace parent path
- ResourceCollection.make_path(): Use root.url_prefix directly instead of root.make_path()

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@ngjunsiang ngjunsiang merged commit 135160c into weekly Apr 10, 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.

2 participants