Skip to content

feat(audit): Internal audit API client for service-to-service communication#454

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

feat(audit): Internal audit API client for service-to-service communication#454
ngjunsiang merged 3 commits intoweeklyfrom
feature/audit-phase4-middleware

Conversation

@nycomp
Copy link
Copy Markdown
Contributor

@nycomp nycomp commented Apr 10, 2026

Summary

Adds internal HTTP client campus.audit.client for communication between campus.auth/campus.api deployments and the audit service. This enables the tracing middleware (issue #428) to send trace spans via HTTP.

Changes

New Files

  • campus/audit/client/__init__.py - AuditClient entry point with environment-based URL resolution
  • campus/audit/client/interface.py - Base classes: ResourceRoot, ResourceCollection, Resource
  • 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

Modified Files

  • campus/audit/middleware/tracing.py - Updated to use new client API (new() instead of ingest())

API Design

Follows Campus API schema conventions matching campus-api-python:

  • ResourceCollection for collections with trailing slash paths (path = "traces/")
  • Nested classes for individual items (Traces.Trace, Traces.Spans.Span)
  • 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

Usage Example

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 specific span
span = client.traces[trace_id].spans[span_id].get()

Related Issues

Create campus.audit.client for service-to-service HTTP communication
between campus.auth/campus.api and campus.audit deployments.

Implements ResourceCollection pattern with nested classes following
campus-api-python conventions:
- Traces (ResourceCollection) with new(), list(), search()
- Traces.Trace (Resource) with get_tree()
- Traces.Spans (ResourceCollection) with list()
- Traces.Spans.Span (Resource) with get()

Uses bracket access for IDs and property access for named
sub-resources. Uses campus.common.http.DefaultClient for HTTP.

Related: #453, #428
@nycomp nycomp mentioned this pull request Apr 10, 2026
20 tasks
@ngjunsiang ngjunsiang linked an issue Apr 10, 2026 that may be closed by this pull request
11 tasks
- Fix AuditRoot to inherit from ResourceRoot
- Fix params dict type annotation to use dict[str, Any]
- Fix forward references for nested classes (Traces.Spans, Traces.Spans.Span)
- Add trace_id attribute declaration to Spans class
- Add type narrowing casts in end_span function
- Fix response body extraction with proper type casts

All type checks now pass (pyright).

Related: #453
@ngjunsiang ngjunsiang merged commit 872b95d 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.

Internal Audit API Client for Service-to-Service Communication

2 participants