workos-python
workos-python copied to clipboard
Default to adding idempotecy-key to audit-logs/events if not present, added in retryability logic to endpoints
Description
Updating python library sdk to always generating an idempotencyKey if not present when POSTing audit-logs events. Also updated to add retryability to endpoints. I set it up to not be enabled by default
/review
Greptile Summary
- Auto-generates UUID v4-based idempotency keys with
workos-python-prefix for audit log events when not provided - Implements retry logic with exponential backoff for status codes 408, 500, 502, 504, network errors, and timeouts (max 3 retries, opt-in via
retry_configparameter) - Changes
create_eventreturn type fromNonetoAuditLogEventResponsewithsuccessfield
Confidence Score: 5/5
- This PR is safe to merge with minimal risk.
- The implementation is well-tested with comprehensive test coverage for both sync and async retry logic, follows existing patterns in the codebase, maintains backward compatibility by defaulting retry to opt-in (None by default), and the idempotency key auto-generation prevents duplicate events while allowing user override.
- No files require special attention.
Important Files Changed
| Filename | Overview |
|---|---|
| workos/audit_logs.py | Modified create_event to auto-generate idempotency keys with workos-python- prefix and enabled retry logic with default RetryConfig() |
| workos/utils/_base_http_client.py | Added RetryConfig dataclass and helper methods _is_retryable_error, _is_retryable_exception, and _get_backoff_delay for retry functionality |
| workos/utils/http_client.py | Implemented retry logic with exponential backoff in both SyncHTTPClient and AsyncHTTPClient.request() methods |
Sequence Diagram
sequenceDiagram
participant User
participant AuditLogs
participant HTTPClient
participant API
User->>AuditLogs: "create_event(org_id, event)"
AuditLogs->>AuditLogs: "Generate idempotency key if not provided"
AuditLogs->>HTTPClient: "request with retry configuration"
loop Retry up to 3 times
HTTPClient->>API: "POST audit logs events"
alt Status 200
API-->>HTTPClient: "Response body"
HTTPClient-->>AuditLogs: "Return JSON"
else Status 408, 500, 502, or 504
API-->>HTTPClient: "Error response"
HTTPClient->>HTTPClient: "Wait with exponential backoff"
else Network or Timeout Error
HTTPClient->>HTTPClient: "Wait with exponential backoff"
end
end
AuditLogs->>AuditLogs: "Parse to AuditLogEventResponse"
AuditLogs-->>User: "Return response object"
@greptile re-review please